从servlet转发到jsp的请求

时间:2014-02-01 18:56:46

标签: java html jsp servlets model-view-controller

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

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

申请流程:

  1. 从html提交。
  2. controller从html获取名称。
  3. 控制器应该将请求移动到jsp文件。
  4. 谢谢!

    项目层次结构: 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:

       @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:

    <%@ 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:

        <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>
    

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

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

3 个答案:

答案 0 :(得分:7)

试试这个

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

答案 1 :(得分:0)

的web.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>

答案 2 :(得分:0)

您的代码在我的本地运行正常,最后使用欢迎列表定义了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>