(已解决!)
我正在学习有关 servlet 的教程,但我很早就陷入困境。出于某种原因,一旦调用 Controller (下面的 Controller.java),我就会收到 404。
想法是 index.jsp 中的表单应该调用一个 Controller (form action="Controller"),以便根据按下表单中的哪个按钮来确定表单中的信息应该发送到哪个页面(目前只有一个,confirmButton)。
但是,当调用 Controller 时,我总是收到 404。
...帮助?
索引.jsp
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Simple Edit Page</title>
</head>
<body>
<p>This is a simple HTML page that has a form in it.
<form action="Controller">
<p>
If there is a value for the hobby in the query
string, then it is used to initialize the hobby
element.
<p>
Hobby: <input type="text" name="hobby"
value="${param.hobby}">
<input type="submit" name="confirmButton"
value="Confirm">
</form>
</body>
</html>
网络.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>controller.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/controller/Controller</url-pattern>
</servlet-mapping>
</web-app>
Controller .java
package controller;
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 = "index.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
放置文件:
src/controller/Controller.java
web/WEB-INF/classes/controller/Controller.class
最佳答案
我从昨天开始就一直在看这个问题,但是当然,我一发布问题就自己找到了答案。
变化:
(1)文件的放置
src/servletController/Controller.java
web/servletController/index.jsp
web/WEB-INF/classes/servletController/Controller.class
(2) web.xml 的小改动
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>servletController.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/servletController/Controller</url-pattern>
</servlet-mapping>
编辑:实际上不是 100% 确定它为什么有效,但我已经决定我不是 servlet 的粉丝。
关于java - 学习servlets : HTTP Status 404 -/Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968990/