xuan xue
xuan xue

Reputation: 21

how to deploy a webapp in tomcat using IDEA

I'm a new javaer. I am trying to develop a webapplication and deploy in tomcat. I just use the template created by idea, but it works not well.

I'm using tomcat9.0.41.

HelloServlet.java


import java.io.*;

import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

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

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}

when I open http://localhost:8080/serv01_war_exploded/ in chrome, it works well and shows the content in index.jsp. But when I try to open http://localhost:8080/serv01_war_exploded/hello-servlet, it returns 404 error.

All the above happens in windows10 system.

So what caused this problem, idea or tomcat or some other things?

Upvotes: 2

Views: 74

Answers (1)

Peter Knall
Peter Knall

Reputation: 86

I usually find when I run into this error that it's one of two things: I either don't have the servlet mapped in "webapp\WEB-INF\web.xml" to that URL, or something I need in the .war file is missing (like the web.xml file)...which forces me to look at the how I'm building the artifact in IDEA.

Upvotes: 2

Related Questions