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.
Cheers,
Question
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!
Solution
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.
Cheers,
OTHER TIPS
You can use sendRedirect
instead of forward
http
status code 302 to browser so browser makes new request.Redirect requires one more round trip from browser to server.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);