java - Servlet:HTTP 状态 404 - 未找到
问题描述
我的 Servlet 根本不起作用。我收到 HTTPS 状态 404 - 未找到。以下是相同的完整代码:-
索引.jsp
<!DOCTYPE html>
<html>
<head>
<title>Page title</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="CSS/cssSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<a href="ControlleurSimple?action=apropos" target="_self">A propos</a>
</nav>
<main>
<h1>Welcome</h1>
Text here
</main>
</body>
</html>
web.xml
<servlet>
<servlet-name>ControllerSimple</servlet-name>
<servlet-class>WebApplication1.ControllerSimple</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerSimple</servlet-name>
<url-pattern>/ControllerSimple</url-pattern>
</servlet-mapping>
ControllerSimple.java
package WebApplication1;
import java.io.IOException;
import java.io.PrintWriter;
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 ControllerSimple extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ControllerSimple</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ControllerSimple at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String jspPage = "/index.html";
if ((action == null) || (action.length() < 1)) {
action = "default";
}
if ("apropos".equals(action)) {
jspPage = "/apropos.jsp";
}
RequestDispatcher rd = request.getRequestDispatcher(jspPage);
rd.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
apropos.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="CSS/cssSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main>
Text
</main>
</body>
</html>
我是 servlet 的新手,所以也许我会错过一些东西。当我单击链接时,出现“HTTP 状态 404 - 未找到”错误。我真的不明白我做错了什么。
我正在使用 NetBeans IDE 8.2 和 GlassFish 4.1.1
解决方案
只需检查代码片段,您有一个拼写错误。在那里,您必须更改ControlleurSimple
为ControllerSimple
.
推荐阅读
- gradle - 在 Teamcity CI 代理中重用 Gradle 缓存
- blazor - Blazor:如何通过单击按钮提交表单
- nlp - 使用 encode_plus 方法时的标记索引序列长度错误
- azure-data-factory - 避免顺序数据流的较长计算采集时间
- python - 如何解决 python 人性化错误:ImportError cannot import name on_windows from 'humanfriendly.compat'
- json - 使用 JSON 数组索引 0 或子字符串更新单元格
- docker - 如何从容器访问在本地机器上运行的应用程序?
- javascript - 如何在时刻js中获得N个日期之间的差异?
- internet-explorer-11 - IE11 Script1003:应为“}”
- google-sheets - 谷歌表格从第一个字母拆分/修剪