0

I am running a basic servlet code using annotations.. My servlet code is as follows:

package servlettest;

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;

/**
 * Servlet implementation class HelloServlet
 */


@WebServlet(name = "HS", urlPatterns = {"/t2"})
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public HelloServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
//      response.getWriter().append("Served at: ").append(request.getContextPath());
            
        response.setContentType("text/html");
        
        PrintWriter out = response.getWriter();
        
        out.print("<html> <body>");
        out.print("<h3>Hello World</h3>");
        out.print(" </body></html>");
        
        out.close();
        
        
    }

}


But when I run the code on server, I get the error HTTP Status 404 – Not Found

I am using Tomcat v9.0 server

Since web.xml file is not needed while using annotations, i have deleted it from ServletEx/WebContent/WEB-INF folder

Below is the error I am getting in the browser while accessing the url http://localhost:8080/ServletEx/t2 and ServletEx is the project name.

HTTP Status 404 – Not Found

Type Status Report

Message The requested resource [/ServletEx/t2] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

user2755407
  • 348
  • 1
  • 3
  • 15
  • Everything seems fine in your code. You can set `org.apache.catalina.core.ContainerBase.level = FINE` in `conf/logging.properties` to check whether your servlet has been added to the context. Look for lines like `Add child StandardWrapper[HS]...` in `catalina..log`. – Piotr P. Karwasz Jun 05 '21 at 18:31
  • i set `org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = FINE`.But I couldn't fine any lines like `Add child StandardWrapper[HS]... in catalina..log. –`.Can you please look into it..i can share screen share using google meet if you want..I am new to servlet – user2755407 Jun 05 '21 at 18:54
  • Change the level of the `org.apache.catalina.core.ContainerBase` logger as proposed, not the `org.apache.catalina.core.ContainerBase.[Catalina].[localhost]` logger. You can edit the question and add the logs to it. – Piotr P. Karwasz Jun 05 '21 at 22:58
  • 1
    i got it resolved ..it has already been discussed here https://stackoverflow.com/questions/17982240/java-lang-classnotfoundexception-for-servlet-in-tomcat-with-eclipse...In my case enabling enable automatic builds `Project > Build Automatically` does the thing..@ Piotr P. Karwasz Thanks for your time – user2755407 Jun 06 '21 at 12:44

0 Answers0