简体   繁体   中英

Running servlet with tomcat

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 .

Try localhost:8080/myApp/HelloWorld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM