I m trying to use web application with Embedded tomcat 8 without web.xml.
But I am not able to access the application by hitting the url http://localhost:8085/Servlet_Basic/index.jsp from browser.
Neither I am able to access the servlet by hitting the url http://localhost:8085/Servlet_Basic/hello from browser.
Here is my source code :
pom.xml dependencies
<properties>
<tomcat.version>8.5.23</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
HelloServlet.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(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloServlet() {
super();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n";
out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>My First Servlet</H1>\n" + "</BODY></HTML>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
App.java
public class App {
public static void main(String[] args) throws LifecycleException {
int port = 8085;
Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
StandardServer server = (StandardServer) tomcat.getServer();
AprLifecycleListener listener = new AprLifecycleListener();
server.addLifecycleListener(listener);
String docBase = "D:\\Servlet_Basic\\src\\main\\webapp";
Context context = tomcat.addWebapp("/Servlet_Basic", docBase);
context.setAddWebinfClassesResources(true);
tomcat.start();
System.out.println("Tomcat server started at port " + port);
tomcat.getServer().await();
}
}
src/main/webapp contents :
index.jsp
<html>
<body>
<h2>Hello World</h2>
</body>
</html>
How can I access the application and invoke servlet annotated with @WebServlet
annotation in embedded tomcat without using web.xml ?
Basically I want to use web application with embedded tomcat without web.xml and without explicitly configuring servlets using tomcat.addServlet() method.
Instead I would like to configure servlet using @WebServlet annotation