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!

Was it helpful?

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

Forward

  1. Forward happens on server-side, server forwards the same request to another resource.
  2. In this case the same request is forwarded to another resource so all the request parameters and request attributes will be availables there is no req,ans resp objects of servlet1 you can't get the values of ser1 and ser2
    Cautions :
    Forward need to be call before response is committed to the client otherwise you will end up with an exception.
    Limitations :
    You can't forward the request outside of the context so the request should with in your site.
    Advantages :
    performance wise it will be bit faster as second call is made with out knowledge of the container and there will not be a round trip to the client to server.

redirect

  1. It happens on the browser side, server sends http status code 302 to browser so browser makes new request.Redirect requires one more round trip from browser to server.
  2. its a new request so old parameters and attributes will not be available.
    Advantages:
    You can access the servlet which is out side of the context also, so you can make a call to the servlet which is there in the other web site.
    Limitations:
    There is a round trip performance wise bit slow and a

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top