How do I deploy a Servlet in Jetty?

Jonas picture Jonas · Nov 8, 2010 · Viewed 14.6k times · Source

I have created a simple Servlet that I want to deploy in Jetty 7.2. Jetty is running and is able to serve JSP pages on http://localhost:8080/jonas/test.jsp. I started Jetty with the java -jar start.jar command.

I saved my compiled Servlet MyServlet.class at <my_jetty_directory>/webapps/jonas/WEB-INF/classes/MyServlet.class and then tried to access that Servlet on http://localhost:8080/jonas/servlets/MyServlet but I get a HTTP 404 error.

HTTP ERROR 404

Problem accessing /jonas/servlet/MyServlet. Reason:

    Not Found

Is there something more I have to do? Where in the Jetty file structure should I place MySerlvet.class?


I have now created a simple web.xml file and saved it in <my_jetty_directory>/webapps/jonas/WEB-INF/web.xml and restarted my Jetty, but it doesn't work. Here is my simple web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
  </servlet>
</web-app>

I had a similar problem with JSP, that is solved now: How do I deploy a JSP file in the Jetty webserver?

Answer

Bozho picture Bozho · Nov 8, 2010

You have to map your servlet in web.xml, using <servlet> and <servlet-mapping>, or annotate it with @WebServlet if using servlet 3.0.