2013-05-09 87 views
0

我是JSP和Servlets的新手。路徑中的Servlet映射錯誤

我有兩個JSP頁面Index.jsp和Edit.jsp以及一個Controller.java。

  1. 的index.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. edit.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]

請客氣推薦!!

回答

0

這裏有一些建議:

1)既然你是在HREF指edit.jsp文件,確保兩個JSP的相同文件夾下。 最好的辦法是使HREF = 「<%= request.getContextPath()%>/edit.jsp文件」

2)申請表單動作相同即

行動=」 <%= request.getContextPath ()%>/ch2/servletController/Controller「

希望這可以幫助你的事業。

+0

謝謝Suyash,但它不工作。 – user1947627 2013-05-09 10:20:35

+0

謝謝Suyash,但它不起作用。 基本上,我的控制器位於Classes.ch2.servletController文件夾中 ,而索引和編輯位於WEB-INF文件夾中。 我也改變了web.xml中的路徑 as/ch2/servletController/Controller 請建議... – user1947627 2013-05-09 10:28:00

+0

在web.xml中,將URL模式更改爲「/ controller」,更新內容與Jsp的表單action屬性相同action =「<%= request.getContextPath()%>/controller」 這將起作用。 – suyash 2013-05-09 11:00:17

0

在Edit.jsp中,表單動作是Controller,它應該是web.xml文件中servlet的相應url-pattern。在你的情況下,如果你改變Edit.jsp表單動作來形成action =「/ ch2/servletController/Controller」,那麼jsp代碼片段就會找到Servlet。