JSP와 서블릿을 처음 사용합니다.경로의 서블릿 매핑 오류
두 개의 JSP 페이지 Index.jsp와 Edit.jsp와 하나의 Controller.java가 있습니다.
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>
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>
컨트롤러
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); }}
Web 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 페이지를 실행하고 "편집"버튼을 클릭하면 오류가 발생합니다.
! [I 나타나는 오류] [4]
친절 추천! 당신이 HREF에서 edit.jsp를을 참조하고 있기 때문에
1), 두 JSP로의이 같은 폴더에 있는지 확인하십시오 :
수야시에 감사합니다. 작동하지 않습니다. – user1947627
수와시 고맙습니다.하지만 작동하지 않습니다. 기본적으로 내 컨트롤러는 Classes.ch2.servletController 폴더 이고 색인 및 편집은 WEB-INF 폴더에 있습니다. 또한 web.xml의 경로를/ch2/servletController/Controller로 바 꾸었습니다. 제안하십시오 ... –
user1947627
web.xml에서 URL 패턴을 "/ controller"로 변경하면 Jsp의 양식 작업 속성에서 다음과 같이 업데이트됩니다. action = "<% = request.getContextPath() %>/controller" 이 작업을 수행 할 수 있습니다. – suyash