Java 将请求从 servlet 转发到 jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21502149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:02:15  来源:igfitidea点击:

Forward request from servlet to jsp

javahtmljspservletsmodel-view-controller

提问by Itay Avraham

I have a small application (HTML form, servlet as controller and jsp files) and i try to figure out why i cannot to forward the request from servlet to jsp files.

我有一个小应用程序(HTML 表单,servlet 作为控制器和 jsp 文件),我试图弄清楚为什么我不能将请求从 servlet 转发到 jsp 文件。

the problem is after submit from html, showed up "HTTP Status 404"

问题是从 html 提交后,显示“HTTP 状态 404”

Application flow:

申请流程:

  1. submit from html.
  2. controller get the name from html.
  3. controller supposed to move the request to jsp files.
  1. 从 html 提交。
  2. 控制器从 html 中获取名称。
  3. 控制器应该将请求移动到 jsp 文件。

thanks!

谢谢!

project hierarchy: http://s23.postimg.org/kgt7r7lwb/Capture.jpg

项目层次:http: //s23.postimg.org/kgt7r7lwb/Capture.jpg

main.html:

主文件:

<html>
<title>Coupons categories</title>
<body>
  <h1 align="center">Coupons categories</h1>
  <form method="GET" action="Controller">
    Select category 
    Type:
    <select name="type" size=1>
      <option value="restaurants">Restaurants</option>
      <option value="electrics">Electrics</option>
      <option value="hotels">Hotels</option>
    </select>
    <br><br>
      <input type="Submit">
   </form>
</body>
<html>

controller.java:

控制器.java:

   @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //PrintWriter out = response.getWriter();
        //out.write(request.getPathInfo());

        String path = request.getParameter("type");
        if(path.equals("electrics"))
        {
            request.setAttribute("timestamp", new Date());
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/electrics.jsp");
            dispatcher.forward(request, response);
        }
        else if(path.equals("hotels"))
        {
            request.setAttribute("timestamp", new Date());
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/hotels.jsp");
            dispatcher.forward(request, response);          
        }
        else if(path.equals("restaurants"))
        {
            request.setAttribute("timestamp", new Date());
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/view/restaurants.jsp");
            dispatcher.forward(request, response);          
        }
    }

electrics.jsp:

电气.jsp:

<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
    <h2>products list...</h2>
    <%
    Object ob = request.getAttribute("timestamp");
    out.println(ob);
    %>
</body>
</html>

web.xml:

网页.xml:

    <description>
      CouponsServer
    </description>
    <display-name>Controller for CouponsServer</display-name>

    <servlet>
      <servlet-name>Controller</servlet-name>
      <servlet-class>uses.server.Controller</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/Controller</url-pattern>
    </servlet-mapping>


</web-app>

update: Probably the problem is in the controller.java. When i try the following code, i got HTTP Status 500. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

更新:可能问题出在 controller.java 中。当我尝试以下代码时,我得到了 HTTP 状态 500。 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.write(request.getPathInfo());
    }

回答by Ashish Ratan

Try this

尝试这个

 request.getRequestDispatcher("/view/electrics.jsp").forward(req,res);

回答by Ashish Ratan

web.xml

网页.xml

<servlet>
    <servlet-name>Controller</servlet-name>
    <servlet-class>uses.server.Controller</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>Controller</servlet-name>
     <url-pattern>/Controller</url-pattern>
</servlet-mapping>

<welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

回答by Minnie

Your code is running fine in my local with the web.xml defined at the end with welcome list or even without it. Try your code running with the dynamic Web content version 2.5 where the web.xml is generated automatically.

您的代码在我的本地运行良好,最后定义的 web.xml 带有欢迎列表,甚至没有它。尝试使用动态 Web 内容版本 2.5 运行您的代码,其中自动生成 web.xml。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <description>
      CouponsServer
    </description>
  <display-name>Controller for CouponsServer</display-name>


  <servlet>

    <display-name>Controller</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>uses.server.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/Controller</url-pattern>
  </servlet-mapping>
</web-app>