Created a project maven webapp in the console, registered dependencies in pom.xml

 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> 

servlet description in web.xml

 <servlet> <servlet-name>Servlet</servlet-name> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-mapping> <url-pattern>/<url-pattern> </servlet-mapping> 

servlet itself

 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class Servlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter pw = resp.getWriter(); pw.println("<h1> Hello Servlets!!!</h1>"); } } 

assembled mvn clean package project, webapps folder to tomcat

I go to the browser localhost:8080/webApp/ output zero.


Here is the structure of my project after assembly. I deliberately do everything from the console to figure out where it came from and where .... enter image description here

  • Welcome to Stack Overflow in English! There is a convenient syntax for formatting code inside a string: `on both sides. - Nick Volynkin
  • http://localhost:8080 or http://localhost:8080/Servlet - user194374
  • Unfortunately not working - fill
  • packaging set to war ? What is the full name of the archive? - user194374
  • I'm still confused by the difference between packages in which your class lies after compiling with where it is located before. Maybe you indicated a package in the beginning of the class and forgot to write it to us? If so, then you probably have the wrong servlet declaration. In any case, try adding a servlet ad to <servlet-class>com.myapps.test.Servlet</servlet-class> - Vartlok

1 answer 1

Here is the code for the simplest test servlet.

pom.xml:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>servlet</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>learning-servlet</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> </plugin> </plugins> </build> </project> 

src / main / java / Servlet.java:

 import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/") public class Servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { PrintWriter writer = resp.getWriter(); writer.println("<h1 align='center'>Hello! I'm servlet!</h1>"); } catch (Exception e) { e.printStackTrace(); } } } 

Build with the mvn package command. It is then copied to $TOMCAT_HOME/webapps .

Opens at http://127.0.0.1:8080/learning-servlet