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,
I'm on
http://whatever.com/tmp3.jsp
and that's what URL in the browser says.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>