Java 尝试运行 servlet 时出现 HTTP 状态 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20871886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:54:59  来源:igfitidea点击:

Getting HTTP Status 404 error when trying to run servlet

javaeclipsetomcatservletshttp-status-code-404

提问by Darbininkai Broliai

I have a problem with my simple servlet that I am trying to run, Hello.java. I made it in eclipse, then placed the file it in the webapps/ServletTest/WEB-INF/classesfolder and compiled it, creating the file Hello.classin the same folder. I then modified my web.xml file to map the servlet and tried to run it through the following address

我尝试运行的简单 servlet Hello.java 有问题。我在 eclipse 中创建了它,然后将它放在文件webapps/ServletTest/WEB-INF/classes夹中并编译它,Hello.class在同一文件夹中创建文件。然后我修改了我的 web.xml 文件来映射 servlet 并尝试通过以下地址运行它

http://localhost:8080/ServletTest/Hello

However, this did not work, giving the following error

但是,这不起作用,出现以下错误

HTTP Status 404 -

HTTP 状态 404 -

type Status report

类型状态报告

message

信息

description The requested resource is not available. Apache Tomcat/7.0.42

描述 请求的资源不可用。Apache Tomcat/7.0.42

The mapping in the web.xml file looks like this:

web.xml 文件中的映射如下所示:

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Main.Hello</servlet-class>
</servlet>  

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping> 

The code of the servlet:

servlet的代码:

package Main;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {
super();
 }

protected void doGet(HttpServletRequest request, HttpServletResponse                   

response)  throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    String path = request.getContextPath();
    String ip = request.getRemoteAddr();
    out.print("<html>" +
    "<title>Hello</title>" +
            "Hello World"+ "<br>" +
    "Your ip is: " + ip + "<br>" +
    "Your path is: " + path
    + "</html>");       
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)   

throws     ServletException, IOException {

}

}

采纳答案by Sotirios Delimanolis

The compiled Hello.classfile should be in the folder

编译后的Hello.class文件应该在文件夹中

webapps/ServletTest/WEB-INF/classes/Main

since it's declared to be in package Main.

因为它被声明在 package 中Main

Also, you can see Tomcat's startup logs in /logs/catalina.outor /logs/catalina.log, depending.

此外,您可以在/logs/catalina.out或 中看到 Tomcat 的启动日志/logs/catalina.log,具体取决于。



Also, Suresh is right in the comments, use either a <servlet>declaration or @WebServlet. Don't use both.

此外,Suresh 在评论中是正确的,使用<servlet>声明或@WebServlet. 不要两个都用。

回答by Max Chernopolsky

Try to delete web.xml. Annotation @WebServlet("/Hello")is enough for Tomcat 7+

尝试删除 web.xml。@WebServlet("/Hello")Tomcat 7+ 的注解就够了

回答by Arun Attukal

If the root folder not having proper permission you will face this issue. So please check the root folder property and remove read only permission and add user to full access permission in security tab.

如果根文件夹没有适当的权限,您将面临此问题。因此,请检查根文件夹属性并删除只读权限并将用户添加到安全选项卡中的完全访问权限。