当前位置: 首页 > 知识库问答 >
问题:

JavaEE 8、Tomcat 8.5、错误状态404

解博明
2023-03-14

我正在尝试创建我的第一个servlet,但我得到了404状态代码。

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>Hello</servlet-name>
        <servlet-class>Test.Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

你好Java语言

 package Test;

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


public class Hello extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
        PrintWriter pw = res.getWriter();
        pw.println("Hello World");
    }
}

我想运行它:localhost:8080/hello

现在它的状态是404

"说明源服务器没有找到目标资源的当前表示形式,或者不愿意透露目标资源的存在。"

我怎样才能解决这个问题?

共有1个答案

姬念
2023-03-14

很奇怪,我注意到你的网页。xml版本为4.0。。您应该能够通过注释对Servlet使用url映射。

尝试添加WebServlet注释,如下所示:

 package Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; //IMPORT THIS ALSO
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hello") //try adding this here
public class Hello extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
        PrintWriter pw = res.getWriter();
        pw.println("Hello World");
    }
}

然后删除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">

//remove servlet and mapping    

</web-app>

如果这不起作用,你可以尝试的另一件事是更改你的版本。例如,替换web。xml(版本3.1):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

</web-app>
 类似资料:
  • 可以用类 ios 中的位测试流的状态。类ios是输入/输出类 istream、ostream 和 iostream 的基类。 当遇到文件结束符时,输人流中自动设置 eofbit。可以在程序中使用成员函数eof确定是否已经到达文件尾。如果cin遇到了文件结束符,那么函数调用: cin.eof() 返回 true,否则返回 false。 当流中发生格式错误时,虽然会设置failbit,但是字符并末丢失

  • 我正在Eclipse中运行tomcat 7...我得到以下错误当我运行我的html调用一个servlet来执行一个简单的计算…错误为 我的html文件是 我以前做过,但当我启动服务器时,我得到以下错误

  • 我试图在Flink(版本1.4.2)上使用可查询状态,但不幸的是,我一直收到以下错误: 在客户端,我使用flink-queryable-state-client-java_2_11.jar可查询客户端的相关代码部分是 最后,在Flink上运行的作业配置了一个ListState,如下所示。请注意,数据在ListState上由String键控 在我看来,这似乎是一个序列化错误,但我不知道我需要做什么来

  • 如何在SpringMVC中实现自定义http错误处理。 例如:我有一个urlhttp://localhost:8080/demo/canvas 它显示我的画布页面,但如果用户错过了网址并键入http://localhost:8080/demo/canva 它显示了Tomcat呈现的HTTP状态404。我希望它是自定义jsp页面。

  • 我在Openshift上有一个应用程序,几个月来一直运行良好,突然变得503。Haproxy状态表示第7层错误状态内部服务器错误 我已经检查了日志,没有提到任何特别的东西。我已经检查了使用限额,它远远低于1024m的限制。我已经多次重启该应用程序,但没有成功。我已经检查了haproxy.cfg,根齿轮地址是正确的。 怎么会这样呢?似乎一切都失去了,因为没有关于它的文件。

  • 我遇到了从html向servlet提交数据的问题 消息为空 说明服务器遇到意外情况,无法完成请求。 例外情况 注意服务器日志中提供了根本原因的完整堆栈跟踪。