2

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>
skaffman
  • 398,947
  • 96
  • 818
  • 769
Meow
  • 18,371
  • 52
  • 136
  • 180
  • I'd place JSP in `/WEB-INF` folder and use `/register` all the way. – BalusC Sep 10 '10 at 11:27
  • @BalusC: What is the reason for putting jsp files under /WEB-INF ? I thought /WEB-INF contains classes and libs and jsp files goes to something like webapps/webContent/foo.jsp ? – Meow Sep 10 '10 at 11:32
  • 1
    To hide JSP from direct access. This way you end up with nicer URL's. You're already dispatching the request to JSP file on GET request. So why not just use the servlet as controller all the way then? See also ["Hidden features" of JSP/Servlet](http://stackoverflow.com/questions/2523430/hidden-features-of-jsp-servlet/2525995#2525995). More useful info and links can be found in [Servlets tag info page](http://stackoverflow.com/tags/servlets/info). – BalusC Sep 10 '10 at 11:37
  • Awesome, I no longer have to expose .jsp on my site! thanks! – Meow Sep 10 '10 at 11:45
  • 1
    In real, you should not be delegating `doGet()` requests to `doPost()`. Maybe at highest from `doPost()` to `doGet()` if all `doGet()` does is dispatching the request to JSP. After all, GET should be used only to preprocess request and POST to postprocess request. Anyway, the links contains lot of useful information and hints and other useful links. Good luck. By the way, it surprises me that you seemingly didn't try the example I posted in [this answer](http://stackoverflow.com/questions/3560746/should-session-be-used-to-handle-error-message-for-form-validation). – BalusC Sep 10 '10 at 11:48
  • I noticed that you put the jsp file under WEB-INF but thought it was typo or something.. I've so far read dao tutorial and servlet tutorial you provided (which has approx 1000 power point slides!) Still many more to go. – Meow Sep 10 '10 at 12:00
  • Typo? :) In future, if you don't understand something, say/ask that so in the comment! – BalusC Sep 10 '10 at 12:28

2 Answers2

5

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
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    @masato-san, you might find it useful to watch the HTTP headers send by browser and server in an exchange like this. – matt b Sep 10 '10 at 11:51
0

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
  • 15,390
  • 15
  • 77
  • 125