different url after calling a servlet [duplicate] - java

This question already has answers here:
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
(9 answers)
Closed 5 months ago.
I have A servlet that does some stuff, when it has done puts some things in the request and after calls another servlet which in turn calls a jsp page.
I write the code:
first servlet (InserisciLezione)
request.getRequestDispatcher("/TakeDates").forward(request, response);
second servlet (TakeDates)
RequestDispatcher dispatcher = request
.getRequestDispatcher("GestioneCalendario.jsp");
dispatcher.forward(request, response);
This works properly but the problem is that in the url of the page I have yet:
http://localhost:8080/Spinning/InserisciLezione?data=20-02-2013
and If I refresh the page the first servlet is called again and I don't want this.
I would like to have
http://localhost:8080/Spinning/GestioneCalendario.jsp
Why?
thanks in advance!

If my memories are good (it's been a long time I didn't use raw servlets), you should use a redirect rather than a forward.
You can use the response.sendRedirect(url) method.

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.This interface can also be used to include the content of antoher resource also. It is one of the way of servlet collaboration.
In your case ,you are getting this
http://localhost:8080/Spinning/GestioneCalendario.jsp
because of this servlet
RequestDispatcher dispatcher = request
.getRequestDispatcher("GestioneCalendario.jsp");
dispatcher.forward(request, response);
Page refresh will always redirect you to that url by which servlet you have used. Its like calling an ajax event.
Anyway I can see you dont need to use forward method,try to use
response.sendRedirect(your_url);

Related

Why write a servlet filter for a DispatcherType other than REQUEST?

I've seen plenty of servlet filters in my time for doing all kinds of things but always on REQUEST never anything else. Does anyone have any use cases or examples of why it might be useful to write a servlet filter for another dispatcherType, other than REQUEST?
The DispatcherTypes as in javax.servlet package.
public enum DispatcherType {
FORWARD, INCLUDE, REQUEST, ASYNC, ERROR
}
Well the names are rather self-explaining. The REQUEST is indeed the most often used one and it's applied on the incoming request chain. Here is when you may need to use the others (because the REQUEST only dispatched filters will not be applied):
FORWARD - if you want to filter request/response when one servlet forwards the request to another servlet. Often this is used when a servlet forwards to a JSP page.
INCLUDE - if you want to filter request/response when one servlet calls another servlet in order to include it's response in the own response. Often this is used when a JSP page includes JSP page.
ASYNC - This is something I personally never used but AFAIK it's needed to be able to filter the asynchronous requests introduced in Servlet 3 spec
ERROR - if you want to filter request/response when the servlet call results in error.
See https://sling.apache.org/documentation/the-sling-engine/filters.html#filter-chains for some more details.

How to redirect in Asynchronous Servlet?

According to this document, I understand a Request could be dispatch forward to another servlet in one Asynchronous Servlet which also makes container to call complete() and trigger another Servlet or JSP automatically.
But, what if I want to redirect the page under async Servlet? Should I call complete() and response.redirect("") both? do we have any example of redirection?
Thanks.
What you can do in this situation is that give specific json response to client (browser) using response.getWriter.write(jsonObject) and in JavaScript you write code for changing the location i.e location.assign("/newPage").

How to redirect a HTTPRequest from one controller class to other controller class?

I have two controller. One is main controller and other is intermediate controller. In intermediate controller I should add header to HTTPRequest. After Adding I should redirect it to main controller where I should check the header I added. How can I do this? Can anyone help me for this?pls........
Seems like a good place to use Servlet Filter . If you want to pre-process an incoming request you could do this with servlet filters and then simply chain it to the appropriate servlet .
Refer BalusC Answer for details .
If this is not the case your can merely forward the request like :
request.getRequestDispatcher("/yourServlet").forward(request, response); // forward to the main servlet
Forward Dispatching
getServletContext().getRequestDispatcher().forward("second page");
The request forwarding is done internally by the JSF Controller Servlet to another resource. The browser is unaware of what has happened in the server side at the web container. So it still thinks it is tending to the original request and displays the original URL in its address bar. However, the page content displayed is from the second page.
Redirect Dispatching
response.sendRedirect("second page");
In this case, the JSF Controller Servlet instructs the client browser (via HTTP response header) to fetch another URL. So the browser fetches entirely a new URL and displays the second URL in its address bar. This could cause slight performance delay
from here
I think you need to forward the request rather than redirecting.
RequestDispatcher dispatcher= request.getRequestDispatcher("servlet-mapped-url");
dispatcher.forward(request, response);

How servlet handle multiple request from same jsp page

Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.

Sending redirect to another servlet/JSP without loosing the request parameters.

How do i specify a redirection to another servlet, in the doPost() method of a servlet.
at the moment I'm using
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
which is wrong since, my parameters in the doGet() method of products are not being called and initialized.
So I'm left with an empty products page after logging in :/
You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of /products:
response.sendRedirect("/products");
This way the webbrowser will be instructed to fire a new HTTP GET request on the given URL and thus the doGet() method of the servlet instance will be called where you can in turn load the products and forward to a JSP which displays them the usual way.
In your doPost you can always call:
return doGet(request, response);

Categories