【发布时间】:2022-01-15 01:00:50
【问题描述】:
大家好,我正在尝试制作一个返回 JSON 的简单 Java Servlet。 web.xml 位于 WEB-INF 目录中,如果我输入了链接 false 或类似的东西,我已经检查过了。 这是我的 ServletClass:
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(name = "BetaAuthServlet", value = "/betaAuthServlet")
public class BetaAuthServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String message = "Test";
out.print(message);
out.flush();
}
}
这是我的 web.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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>BetaAuthServlet</servlet-name>
<servlet-class>de.hanimehagen.tridentdevelopment.betaauth.BetaAuthServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BetaAuthServlet</servlet-name>
<url-pattern>/BetaAuthServlet</url-pattern>
</servlet-mapping>
</web-app>
当我在 localhost:8080/betaAuthList 上启动应用程序时,出现 404 错误。我不确定我是否需要 index.jsp,但我认为我不需要它。 Tomcat 服务器应该可以正常工作。如果有人可以帮助我,那就太好了。 问候哈根
我找到了解决问题的方法: 我使用的是 Tomcat 版本 10。在我使用版本 9 之后,它可以工作了!
【问题讨论】:
-
您好,欢迎您! “ROOT”(或哪个)应用程序的 WEB-INF 目录....编译的类在哪里?你是如何将它“介绍给 tomcat”的?
-
..当你做
@WebServlet时,你不需要/没有web.xml。这是一个"nice tutorial/article" ..just “打包”,将@webservlet 放入“战争”并部署它。 -
您的
@WebServlet和 web.xml 将路径显示为/BetaAuthServlet或betaAuthServlet- 末尾的段落说明您正在尝试获取/betaAuthList?