Getting HTTP Status 404 error when trying to run s

2020-02-12 14:29发布

问题:

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.