Apache Tomcat error http status 404 [duplicate]

2019-08-21 04:00发布

问题:

This question already has an answer here:

  • Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available” 8 answers

To be honest i am a learner and this is my first ever servlet program. I made the basic servlet and intalled tomcat version 6 and even tomcat version 8. the server starts up correctly and i am able to see the tomcat start up page on going to

http://localhost:8080

but after logging to tomcat manager when i click on my folder name it gives me an error saying

http status 404-/online/ (online is my folder created in webapps)

type Status report

message /online/

description The requested resource is not available.

here's my codes

web.xml-> (in folder online->WEB-INF)

- <web-app>
- <servlet>
  <servlet-name>FirstServlet</servlet-name> 
  <servlet-class>FirstServlet</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>FirstServlet</servlet-name> 
  <url-pattern>/FirstServlet</url-pattern> 
  </servlet-mapping>
  </web-app>

FirstServlet.java->

import javax.servlet.*;
import java.io.*;

class FirstServelet implements Servlet
{
    public void init(ServletConfig config)
    {

    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        PrintWriter out;
        out=response.getWriter();
        out.println("hello");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>MY First Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<marquee>ban ja tar pls</marquee>");
        out.println("</body>");
        out.println("</html>");

    }

    public String getServletInfo()
    {
        return null;
    }

    public ServletConfig getServletConfig()
    {
        return null;
    }

    public void destroy ()
    {

    }
}

please resolve the 404 error

回答1:

The problem is you don't welcome-file-list, I think the default page is index.html which I suppose is not there in you folder. You can provide any html or jsp file as default file but NOT a servlet as below.

<welcome-file-list>
      <welcome-file>myfile.html</welcome-file>  
</welcome-file-list>  

You can access your servlet by hitting http://localhost:8080/online/FirstServlet URL.

You can create a default page which will redirect to FirstServlet i.e.

myfile.html

<meta http-equiv="refresh" content="0; url=http://localhost:8080/online/FirstServlet" />

And also what @Braj said in the comment extend HttpServlet instead of implement Servlet.

Edit

You have a typo in servlet name. change the servlet name to FirstServlet from FirstServelet.