因此,我是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);
}
}
答案 0 :(得分:0)
您需要允许交叉原点
private void setAccessControlHeaders(HttpServletResponse resp) {
resp.setHeader("Access-Control-Allow-Origin", "http://localhost:9000");
resp.setHeader("Access-Control-Allow-Methods", "GET");
}