0

我是 JSP 和 Servlet 的新手。

我有两个 JSP 页面 Index.jsp 和 Edit.jsp 以及一个 Controller.java。

  1. 索引.jsp

     <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Index</title>
    </head>
    <body>
        <Form action="/ch2/servletController/Controller">
        <h1>Hello World!</h1>
        <a href="Edit.jsp"> Click here </a>
        <input type="submit" value="Edit" name="gotoEdit" />
    
        </Form>
    </body>
    </html>
    
  2. 编辑.jsp

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Edit</title>
    </head>
    <body>
        <form action="Controller">
        <h3>This is a simple HTML page that has a form in it.</h3>
        <h3>If there is a value for the hobby in the query string, then it is used to initialize the hobby element. 
    
        </h3>
        <p>
        Hobby:    
        <input type="text" name="hobby" value="${param.hobby}" />
        <input type="submit" value="Confirm" name="processButton" />
        </p>
        </form>
    </body>
    </html>
    
  3. 控制器

    package ch2.servletController;
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
     public class Controller extends HttpServlet
     {
     protected void doGet (HttpServletRequest request,
      HttpServletResponse response)
     throws ServletException, IOException
     {
         String address;
      if (request.getParameter("processButton") !=null)
      {
     address = "Process.jsp";
     }
     else if (request.getParameter("confirmButton") !=null)
     {
        address = "Confirm.jsp";
     }
     else
     {
      address = "Edit.jsp";
        }
        RequestDispatcher dispatcher = 
        request.getRequestDispatcher(address);
       dispatcher.forward(request, response);
       }}
    

网页 XML

<servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>ch2.servletController.Controller</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/ch2/servletController/Controller</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

所以问题是当我运行 index.jsp 页面并单击“编辑”按钮时,它会出错。

![我收到的错误][4]

请推荐!!

4

2 回答 2

0

这里有几个建议:

1)由于您在href中引用Edit.jsp,请确保两个jsp都在同一个文件夹下。最好的方法是 make href="<%= request.getContextPath()%>/Edit.jsp"

2)对表单操作应用相同的操作,即

action="<%= request.getContextPath()%>/ch2/servletController/Controller"

希望这对您的事业有所帮助。

于 2013-05-09T09:31:58.617 回答
0

在Edit.jsp中,表单动作为Controller,对应web.xml文件中servlet的url-pattern。在您的情况下,如果您将 Edit.jsp 表单操作更改为 form action="/ch2/servletController/Controller",jsp 代码片段将找到 Servlet。

于 2013-05-09T18:17:12.533 回答