Getting HTTP Status 404 error when trying to run servlet [duplicate]

喜你入骨 提交于 2019-12-18 06:59:56

问题


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/classes folder and compiled it, creating the file Hello.class in the same folder. I then modified my web.xml file to map the servlet and tried to run it through the following address

http://localhost:8080/ServletTest/Hello

However, this did not work, giving the following error

HTTP Status 404 -

type Status report

message

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

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

<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:

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 {

}

}

回答1:


The compiled Hello.class file should be in the folder

webapps/ServletTest/WEB-INF/classes/Main

since it's declared to be in package Main.

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


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/20871886/getting-http-status-404-error-when-trying-to-run-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!