0

I created a simple maven-archetype-webapp in Visual Studio Code to run a simple servlet on the tomcat server.

Here is my project tree- enter image description here

Servlet is a simple folder that has my servlets. I have also added javax.servlet-api in dependencies in pom.xml file. MyServlet.java -

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
    
    System.out.println("this is get method....");
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<h1> this is get method of servlet</h2>");
    
    
}
}

web.xml file -

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>Archetype Created Web Application</display-name>

 
  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>Servlets.MyServlet</servlet-class>
  </servlet>

  <!-- creating mapping -->
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/myservlet</url-pattern>
  </servlet-mapping>


</web-app>

I have also added tomcat server and tried to run webapp using tomcat server but it is showing : HTTP Status 404 – Not Found

I tried to run http://localhost:8080/servlet/myservlet in chrome browser but error message is HTTP Status 404 – Not Found

Also

Mayank Kumar Thakur
  • 588
  • 1
  • 8
  • 23
  • 1
    As stated in abovelinked duplicate, Java classes belong in main/java not in main/webapp. You would also already have figured out that when you let your build tool generate a WAR file as instructed in the abovelinked duplicate. You'd have discovered that the servlet class is completely absent. – BalusC Feb 24 '21 at 13:28
  • 1
    That said, your archetype is jurassic. Servlet 2.3 is already two decades old. Nearly from the previous century. Technology moves quickly. Make sure your learning resources are up to date. Anything older than about a year must be carefully reviewed if there isn't already a newer version available. – BalusC Feb 24 '21 at 13:30

0 Answers0