0

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 :

enter image description here

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

kverma28
  • 57
  • 1
  • 13
  • Does this answer your question? [Embedded tomcat 7 servlet 3.0 annotations not working](https://stackoverflow.com/questions/11669507/embedded-tomcat-7-servlet-3-0-annotations-not-working) – Piotr P. Karwasz Sep 26 '21 at 06:04
  • 1
    BTW: your title mentions Tomcat 8, but you are using Tomcat 10. With Tomcat 10 you need to change `javax.servlet.*` to `jakarta.servlet.*`. – Piotr P. Karwasz Sep 26 '21 at 08:18
  • correction done. Still its not working – kverma28 Sep 26 '21 at 19:22
  • The main problem is described in the aforementioned question or in [this duplicate](https://stackoverflow.com/q/67253024/11748454). – Piotr P. Karwasz Sep 26 '21 at 19:56
  • @PiotrP.Karwasz dude I work on all day today. Your solution is working for me. I am using Tomcat version 10. (I don't know which version before.) Thank you. – Orhan Gazi Jul 10 '23 at 16:10

0 Answers0