tags:

views:

84

answers:

3

All the online references and head first JSP&Servlet book I'm reading state the characteristic of RequestDispatcher and Redirect (i.e. resoponse.sendRedirect() ) like:

"Request Dispatcher" - URL in the browser bar does not change.

"Redirect" - The user sees the new URL in the browser.

But according to my test, for RequestDispatcher, I am seeing the URL changes so I don't understand what they really mean.

With the code below,

  1. I'm on http://whatever.com/tmp3.jsp and that's what URL in the browser says.

  2. Click on the button to call servlet which in return it forwards the data to server then the server sends response back to the browser, so URL in the browser now says http://whatever.com/register

So.. URL changed! (from .../tmp3.jsp to .../register)

Can anyone explain to me what they mean by "URL in the browser does not change"?

Example:

(tmp3.jsp)

<html>
<head>
</head>
<body>
   ${message}
   <!-- click button to send request to servlet -->
   <form method="POST" action="register">
       <input type="submit" value="click!">
   </form>
</body>
</html> 

(servlet)

package com.masatosan.tmp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Tmp extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("message", "processed!");

        String address = "/tmp3.jsp";
        request.getRequestDispatcher(address).forward(request, response);
    }//end doPost()

}//end class

(web.xml snippet) - mapping the servlet and URL

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>com.masatosan.tmp.Tmp</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/register</url-pattern>
</servlet-mapping>
A: 

Read This

FosterZ
+4  A: 

Two things happen here:

  • first you submit the form to the servlet, which always changes the URL
  • then you choose to redirect or to forward to a view - i.e. another (or the same) JSP

For the second step:

  • redirect sends a special header to the browser, and it makes a new request to the page to which the redirect points
  • forward makes this internally, within the same request, and the browser never understands that it is not the resource it has requested

In your example, the URL stays /register because you use forward. If you use redirect, it will first change to /register, and then it will change back to tmp.jsp

Bozho
Thanks! I finally understand after reading the answer like 5 times...
masato-san
@masato-san, you might find it useful to watch the HTTP headers send by browser and server in an exchange like this.
matt b
@matt b: That's actually great point since I have firebug.
masato-san
A: 

Url didn't change from .../register to tmp3.jsp, while the response was from the latter. With Redirect you would see .../tmp3.jsp in browser URL bar.

amorfis