【发布时间】:2019-09-29 01:33:42
【问题描述】:
所以我是 servlet 和 jsp 的新手,并且正在关注 https://stackoverflow.com/tags/servlets/info 中的 Hello World 后处理示例。当我尝试在 Eclipse 中使用 Tomcat v9.0 运行它时,我得到了
在做了一些额外的研究并四处寻找之后,我还没有找到可行的解决方案或对正在发生的事情的解释。我基本上已经从示例中复制了代码,所以我很困惑为什么它不起作用。我还没有真正熟练地弄清楚到底是什么问题,所以任何帮助都会很棒。到目前为止,我唯一的预感是我可能弄乱了目录或其他东西。这是一张图片:
我能找到的唯一差异是我的 HelloServlet.class 所在的位置,它在
apache-tomcat-9.0.19/webapps/hello/build/classes/com/example/controller/HelloServlet.class
而不是
/WEB-INF/classes/com/example/controller/HelloServlet.class
如示例中所述。我认为这是因为默认情况下 Eclipse 已将类文件编译到现在所在的位置,但为了确定,我将类文件夹复制到 WEB-INF 中,以便与示例匹配,但它仍然不起作用。所以这就是我卡住的地方。如果有人能指出我的错误甚至提供帮助,那将不胜感激。我在下面包含了我的 hello.jsp、web.xml 和 HelloServlet.java 文件,以防它们出现任何问题。
hello.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Servlet Hello World</title>
<style>.error { color: red; } .success { color: green; }</style>
</head>
<body>
<form action="hello" method="post">
<h1>Hello</h1>
<p>
<label for="name">What's your name?</label>
<input id="name" name="name" value="${fn:escapeXml(param.name)}">
<span class="error">${messages.name}</span>
</p>
<p>
<label for="age">What's your age?</label>
<input id="age" name="age" value="${fn:escapeXml(param.age)}">
<span class="error">${messages.age}</span>
</p>
<p>
<input type="submit">
<span class="success">${messages.success}</span>
</p>
</form>
</body>
</html>
web.xml
<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_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
HelloServlet.java
package com.example.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Preprocess request: we actually don't need to do any business stuff, so just display JSP.
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Postprocess request: gather and validate submitted data and display the result in the same JSP.
// Prepare messages.
Map<String, String> messages = new HashMap<String, String>();
request.setAttribute("messages", messages);
// Get and validate name.
String name = request.getParameter("name");
if (name == null || name.trim().isEmpty()) {
messages.put("name", "Please enter name");
} else if (!name.matches("\\p{Alnum}+")) {
messages.put("name", "Please enter alphanumeric characters only");
}
// Get and validate age.
String age = request.getParameter("age");
if (age == null || age.trim().isEmpty()) {
messages.put("age", "Please enter age");
} else if (!age.matches("\\d+")) {
messages.put("age", "Please enter digits only");
}
// No validation errors? Do the business job!
if (messages.isEmpty()) {
messages.put("success", String.format("Hello, your name is %s and your age is %s!", name, age));
}
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
}
}
【问题讨论】:
标签: java jsp servlets http-status-code-404