简体   繁体   中英

Java servlets: RequestDispatcher doesn't work doesn't redirect paths

I have a start.jsp , a UserInfo.java servlet and a view.jsp . The start.jsp has a form that takes a username input, send it to the servlet which, in turn, sends it to view.jsp . However, when I press the submit button on the form, no redirect happens. I suspect there's something wrong with my paths here, but can't figure out what's wrong. Here's my directory tree:

AppName
  pages
    projects
      ProjectName
        start.jsp
        view.jsp
  src
    com
      web
        UserInfo.java
  WEB-INF
    classes
      com
        UserInfo.class
    web.xml

UserInfo.java :

public class UserInfo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.getWriter().println("GET");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String userName = request.getParameter("username");

        RequestDispatcher view= request.getRequestDispatcher("/projects/ProjectName/view.jsp");
        view.forward(request, response);

    }

}

web.xml :

<servlet-mapping>
    <servlet-name>UserInfo</servlet-name>
    <url-pattern>/User.do</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>UserInfo</servlet-name>
    <servlet-class>com.web.UserInfo</servlet-class>
</servlet>

start.jsp :

<form method="POST" action="User.do">
    <div class="form-group">
        <label for="usr">username:</label><br/><br/>
        <input type="text" class="form-control"name="username"><br/><br/>

    </div>
</form>

<button type="button" class="btn btn-primary"  type="submit">
   Get info
</button>

view.jsp :

<h3>Hello,
   <%
      out.println(request.getParameter("username"));
   %>
</h3>

Here are couple of points to take note about the example posted:


(1) The Button:

(a) These define a clickable button - mostly used with JavaScript to activate a script. The following two are similar; one has a body and the other do not.

<INPUT TYPE="BUTTON" VALUE="Get Info">

<BUTTON TYPE="BUTTON">
    Get Info
</BUTTON>

(b) To submit a form with its input, as in the case of this example, a submit button is to be clicked. The form is sent to the servlet (the server-side program) specified by the ACTION attribute of the FORM. The following two are similar; one has a body and the other do not.

<INPUT TYPE="SUBMIT" VALUE="Get Info">

<BUTTON TYPE="SUBMIT">
    Get Info
</BUTTON>


(2) The Form:

All the inputs to be submitted (related to the form) are to be defined within that form - that includes the user name text input and the submit button . The corrected markup for start.jsp :

<form method="POST" action="User.do">
    <div>
        <label>username:</label><br/><br/>
        <input type="text" name="username"><br/><br/>
    </div>
    <button type="submit">
        Get info
    </button>
</form>


(3) The Servlet:

UserInfo.java :

public class UserInfo extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.getWriter().println("GET");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String userName = request.getParameter("username");
        getServletContext().log("# User name: " + userName);
        RequestDispatcher view = request.getRequestDispatcher("view.jsp");
        view.forward(request, response);
    }
}


(4) I have structured the directory tree little differently (for convenience):

servlet-1
    start.jsp
    view.jsp
src
    com
        web
            UserInfo.java
WEB-INF
    classes
        com
            web 
                UserInfo.class
     web.xml

There is no other changes in the start.jsp , web.xml and view.jsp . The deployed web app was invoked using the URL (in this case deployed on Apache Tomcat web server): http://localhost:8080/servlet-1/start.jsp .

This shows the start.jsp in the browser. Enter the text "username" and click the "Get Info" button. The result will show in the view.jsp (I guess that's what was expected).

Finally, as already mentioned the RequestDispatcher is used to either forward to another resource or include content from another resource - its not a redirect. NOTE: The request dispatcher can be acquired either from ServletContext or from ServletRequest ; note the difference between the two ways of getting the dispatcher.

Well, you don't say what does happen.

Does the forward work? ie does a new page appear? Because a forward is not a redirect.

A redirect sends an explicit response to the browser that then loads a new page, and is mostly obvious by the fact that the URL changes in the browser.

But a forward does not do that. Rather, it simply changes what page is output for the request currently sent. So, you don't really say in detail what is (or is not) happening here.

But taking your question at face value, you're not getting a redirect because forward does not redirect at all.

Keep the button inside form .

<form method="POST" action="User.do">
    <div>
        <label>username:</label><br/><br/>
        <input type="text" name="username"><br/><br/>
    </div>
    <button type="submit">
        Get info
    </button>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM