As a green hand, I created a HelloWorld servlet:
package com.rx.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/helloworld")
public class HelloWorld extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
writer.write("<html><body>Hello World</body</html>");
}
}
Then there is the following configuration in my pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rx.helloworld</groupId>
<artifactId>simpleservlet</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simpleservlet Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<war.name>simpleservlet</war.name>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<servlet.version>4.0.1</servlet.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${war.name}</finalName><!-- name of the bundled project when it is finally built -->
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.9.v20160517</version>
<configuration>
<httpConnector>
<!--host>localhost</host-->
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
</project>
I read from servlet specification document, where told that
A web application is NOT required to contain a web.xml if it does NOT contain any Servlet, Filter, or Listener components or is using annotations to declare the same. In other words an application containing only static files or JSP pages does not require a web.xml to be present.
I indeed used the @WebServlet
, so did not include the web.xml
.
The full code base is here
Question: But when I try to run it with jetty with command mvn jetty:run
, I can't find my servlet at all. How to resolve this problem?