简体   繁体   English

Java Servlet:RequestDispatcher无法正常工作不会重定向路径

[英]Java servlets: RequestDispatcher doesn't work doesn't redirect paths

I have a start.jsp , a UserInfo.java servlet and a view.jsp . 我有一个start.jsp ,一个UserInfo.java servlet和一个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 . start.jsp具有接受用户名输入的形式,然后将其发送到servlet,然后将其发送到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 : 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 : 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 : 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 : 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: (1)按钮:

(a) These define a clickable button - mostly used with JavaScript to activate a script. (a)这些定义了一个可单击的按钮-通常与JavaScript一起使用以激活脚本。 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. (b)如本例所示,要提交带有其输入内容的表单,请单击“提交”按钮。 The form is sent to the servlet (the server-side program) specified by the ACTION attribute of the FORM. 将该表单发送到FORM的ACTION属性指定的Servlet(服务器端程序)。 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: (2)表格:

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 . 所有要提交的输入(与表单相关)都将在该表单中定义-包括用户名文本输入Submit按钮 The corrected markup for start.jsp : 更正后的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: (3)Servlet:

UserInfo.java : 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): (4)我对目录树的结构稍有不同(为方便起见):

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 . start.jspweb.xmlview.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 . 已使用以下URL(在本例中为部署在Apache Tomcat Web服务器上)调用了已部署的Web应用程序: http://localhost:8080/servlet-1/start.jsp

This shows the start.jsp in the browser. 这将在浏览器中显示start.jsp 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). 结果将显示在view.jsp (我想这是预期的结果)。

Finally, as already mentioned the RequestDispatcher is used to either forward to another resource or include content from another resource - its not a redirect. 最后,如前所述, RequestDispatcher用于转发到另一个资源或包括来自另一个资源的内容-它不是重定向。 NOTE: The request dispatcher can be acquired either from ServletContext or from ServletRequest ; 注意:可以从ServletContext或从ServletRequest获取请求分配器。 note the difference between the two ways of getting the dispatcher. 请注意获得调度程序的两种方法之间的区别。

Well, you don't say what does happen. 好吧,你不说会发生什么。

Does the forward work? forward工作吗? ie does a new page appear? 即是否出现新页面? Because a forward is not a redirect. 因为forward不是重定向。

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. 重定向将明确的响应发送到浏览器,然后加载新页面,这在浏览器中URL发生更改的事实中尤为明显。

But a forward does not do that. 但是forward并不能做到这一点。 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. 但是从表面上看您的问题,您不会得到重定向,因为forward根本不会重定向。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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