java - 404 请求的资源在非常简单的 servlet 上不可用

标签 java tomcat servlets

<分区>

整个早上我都被这个问题困住了,我似乎不明白为什么。

这是我的 servlet。很简单:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class hello
 */
@WebServlet("/hello")
public class hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public hello() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().write("test");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

当我启动我的 tomcat 服务器时,转到 http://localhost:8080/testh/hello我收到错误消息: HTTP 状态 404 -/testh/hello

为什么?服务器已启动,我在“testh”项目下运行了这个特定的 servlet/hello。我不明白。请帮忙。

最佳答案

尝试调用http://localhost:8080/hello

因为你所有的项目都在根目录下

/

然后你有映射/hello

此外,如果您确实需要 http://localhost:8080/testh/hello,您应该使用路径 /testh 部署应用程序,然后 HTTP 调用会将您路由到 < strong>/testh/hello 因为你的 servlet 路径是 /hello + app path /testh

关于java - 404 请求的资源在非常简单的 servlet 上不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506612/

相关文章:

java - Apache Tomcat 无法加载 web.xml - 连接超时

java - 使用 Maven、Glassfish 和 JRebel 开发 servlet 的最快方法是什么?

Java CMS 需要帮助请

java - 用Android测量音量

jsp - 是否可以缓存 JSP 字节码以避免使用 Tomcat 重新编译?

java - 限制 Apache Tomcat 7.0 中的日志大小

java - 如何让特定线程进入休眠状态()?

JAVA属性加载静态变量inputstream返回空值

jakarta-ee - 任何 tomcat 版本都支持在 web.xml 中用于 URL 映射的 <url-regex> 标记

servlets - 如何在 GlassFish 中为自动传送的文件设置 Content-Type?