Home > Blockchain >  Running servlet with tomcat
Running servlet with tomcat

Time:04-18

I'm new to servlets and I've created a simple hello world app and deployed it but when I run tomcat and navigate to the url I mapped for my servlet I get tomcat error 404.

My servlet code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
 
    private String survayHtml;

    public void init() throws ServletException {
        survayHtml = "<html><body><h1>Hello World</h1></body></html>";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println(survayHtml);
    }
}

I deployed my app under the webapps directory inside tomcat main directory. The app directory structure is:

myApp ->
    src ->
        HelloWorld.java
    WEB-INF ->
        classes ->
            HelloWorld.class
        web.xml

The web.xml looks like that:

<web-app>
<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>

I run tomcat and navigate to localhost:8080/HelloWorld and get Tomcat error 404 resource [/HelloWorld] is not available.

Thanks in advanced for any help.

CodePudding user response:

Try localhost:8080/myApp/HelloWorld

  • Related