I have a .jsp page where I've coded a form that submits some input to a servlet. The servlet manipulates the input and then sends a message to the same page. This is working fine, however, when I get back to the .jsp page, or in other words, when I get the answer from the servlet, the url that is visible in the browser contains the path to the servlet (e.g http:/index.jsp/servlet_name ), so, when I refresh the page, the browser redirects to the servlet and it get stuck because there was no get/post submission, so doGet() or doPost() are never active.. Is it possible to handle no get/post request in the servlet ? If not, how should I handle this problem ?
Ps: I'm using jquery mobile to build the pages (in the event of a possible solution with this framework)
I think you got it wrong. Even when you refresh, normally either doGet() / doPost() is activated. Your browser often asks you first if you're refreshing post requests.
What could be happening is your servlet is serving another request without form data, hence you get the impression it's not doing anything
Related
I'm writing some not very complicated web application using servlets and jsp but there's some issue I still couldn't resolve.
On the jsp page I've got multiple forms filled out with information retrieved from a database. Also there's several commands that modify this information (add, delete, submit, clear). If I click the "Add" button a request is sent to the servlet which invokes some of DAO methods and forwards the request to the same jsp page with updated "dishes list". It seems that all works just fine but what the hell should I do with the other forms (like the menu) cause this information is being removed after refreshing.
What is the best way to maintain all that information on the page? Should I send all that stuff with every command?
You can use Post/Redirect/Get pattern. More information can be found here: http://www.javacodebook.com/2013/08/20/post-redirect-get-pattern-in-spring-mvc/
Post
Initiate POST request with modified data.
Servet is processing request and updating database.
Redirect
Servlet is sending redirect to user browser.
Get
User browser is creating http get request.
Servlet is processing request and getting required data from database.
Response with latest data is sent back to the user.
So, yeah, I summed up all what I understood and drew a simple diagram.
If I am not wrong, the servlet is the CGI (Common Gateway Interface) because the servelet is THE ONLY way you can get access to the resources on the server. So, in short, it is the COMMON GATEWAY.
The CONTAINER, like Apache Tomcat, is responsible for capturing the request sent by the user and sending it to the servlet.
What the user perceives is a dynamic webpage called web app.
This is what I learnt so far.
Have I learnt it correctly ?
You are almost right. Here are typical workflows you can follow when working with plain servlets:
Servlet renders page
Servlet container find servlet that matches request URL
doGet() or doPost() is called depending on HTTP method requested
Servlet does some processing
Response (HTML, XML, JSON, image...) is generated directly in the servlet and sent to the client using getOutputStream() or getWriter()
PrintWriter out = response.getWriter();
out.println("Hello World");
JSP handles request
Servlet container finds JSP matching request. You must understand that underneath each JSP is translated to some internal servlet
This JSP is interpreted. Raw text is sent directly, Java code in scriptlets is executed
JSP ends, request is done
Servlet forwards to JSP
Same as 1-3 in first scenario
Servlet chooses JSP file and forwards to that JSP
JSP file is then evaluated, it has access to some context (request attributes, session) that was passed by servlet
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher("foo.jsp");
dispatcher.forward(request, response);
The last scenario is considered the best one as it does not mix business logic (servlet) and presentation (JSP).
A Servlet processes a request and generates a response.
A JSP gets compiled into a servlet, so JSPs are a subset of servlets.
It is not a Servlet who looks for the correct JSP, this is the container's job.
I am new to servlets, and would like to follow the Model2 paradigm by keeping all my "code" in servlets, and html/beans in jsp pages. But, is there a way to run a servlet when I access a jsp page without using a form submission. For example, I have a login page. If the user logs in and then somehow goes back to the login page I want to check for the existance of their session and automatically move them on to their welcome page. This is one real world example, but it seems it would come in handy to run code without having to submit a form for a multitude of reasons.
you dont have to submit a form to invoke a servlet. All you have to do is have the browser hit the url that is mapped to the servlet. That could happen when submitting a form, clicking a link, invoking an xhr, using curl or wget from the command line, etc.
Also, keeping all code in servlets is not good design. Your servlets should handle incoming requests, invoke business logic implemented in separate classes (for good modularity and testing purposes), and return the appropriate response.
If I recall correctly, in Model2, the user never navigates to (JSP) pages - only controllers (servlets). Trying to access a lower layer of code (a servlet) direcltly from a view (the page) is a violation of MVC/Model2.
I need know to execute one ajax function before than one jsp:include puts in same jsp.
I try put one session parameter with this ajax function, this parameter is used for include, but first jsp execute include and parameter is not put and imposible to use.
Someone know fix my problem?, thanks guys!
AJAX and JSP are completely different technologies and are run on different systems and at different times.
JSP processors are executed when the page is generated, i.e. on the server side.
AJAX functions are triggered in the browser by Javascript (hence AJAX) which is available after the page has been generated (using JSP) and delivered.
If the AJAX function triggers a JSP on the server side, the JSP or Servlet should read the needed parameter from the request first (the AJAX call would put it there), e.g. by accessing the implicit request object in an expression within the <jsp:include> tag.
I have a java web application running on WebSphere 7. The application uses form authentication method and part of the application accesses some protected resources using ajax requests. However, when the user's session expires, I am getting the login page in place of the content that is supposed to be refreshed by the ajax request.
Is there a good way to handle this problem? WebSphere returns a response status 200 with the login page so I cannot rely on that.
Maybe there is a way to tell the server that basic authentication should be used in certain circumstances but I don't know how.
I also thought of checking first if the session is new by making a request to unprotected resources first then return a certain status but it looks like a code smell solution...
This is how I handled it in a similar situation. In our case, the AJAX response is always JSON. When the login expires, the authentication filter always sends a login form in HTML. So I check the content-type like this,
if ((this.getHeader('Content-type') || '').include('application/json'))
If it's not JSON, I simply redirect to another protected page, which will trigger a full screen login and then that page will direct user back to the AJAX page.
You can send back some unique response or some error code(make sure you wont get this error code as valid response in any case) when the user session is not there to the Ajax call from WebSphere. And in the Ajax call method, on process response, check whether its error code.If its error code, redirect him to login page or do what ever and other case will be the valid data.