1

I want to use Jetty embedded in a java main application to do some quick development. Totally new to Jetty I started by looking at some tuts and examples and here is what I put together:

public class JettyTest {

  public static void main(String[] args) throws Exception {

    Server server = new Server(8080);
    server.dumpStdErr();
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(HelloServlet.class, "/*");        
    server.setHandler(handler);
    server.start();
    server.join();
  }
}

class HelloServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  @Override
  public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    HttpServletResponse httpRes = (HttpServletResponse) res;
    httpRes.setContentType("text/html");
    httpRes.setStatus(HttpServletResponse.SC_OK);
    httpRes.getWriter().println("<h1>Hello..</h1>");
  }
}

And this is the pom's dependencies:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <!-- it is 3.1.0 version -->
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.15.v20160210</version>
    </dependency>
</dependencies>

When I run that I got this not so promising output on console:

org.eclipse.jetty.server.Server@58651fd0 - STOPPED
 +? qtp1355316001{STOPPED,8<=0<=200,i=0,q=0} - STOPPED
 +? ServerConnector@31ef45e3{HTTP/1.1}{0.0.0.0:8080} - STOPPED
 |   +~ org.eclipse.jetty.server.Server@58651fd0 - STOPPED
 |   +~ qtp1355316001{STOPPED,8<=0<=200,i=0,q=0} - STOPPED
 |   +? 
 org.eclipse.jetty.util.thread.ScheduledExecutorScheduler@754ba872 - STOPPED
 |   +- org.eclipse.jetty.io.ArrayByteBufferPool@598067a5
 |   +? HttpConnectionFactory@343f4d3d{HTTP/1.1} - STOPPED
 |   |   +- HttpConfiguration@53b32d7{32768/8192,8192/8192,https://:0,[]}
 |   += org.eclipse.jetty.server.ServerConnector$ServerConnectorManager@589838eb - STOPPED
 |       +- null
 |       +- null
 |       +- null
 |       +- null
 |
 +> sun.misc.Launcher$AppClassLoader@18b4aac2
...

and when I try to hit the localhost:8080 I get this:

javax.servlet.ServletException: edu.jetty.exp.HelloServlet-6fc6f14e@23282359==edu.jetty.exp.HelloServlet,-1,false

What am I missing?

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
Francesco
  • 1,742
  • 5
  • 44
  • 78

1 Answers1

1

Don't use ServletHandler directly like that, use a ServletContextHandler and add the servlets to that.

As for code examples, there are thousands of examples available on stackoverflow.com alone, even more on other websites.

Some highlights:

Also, the service(ServletRequest, ServletResponse) method is a very low level API (its not even HTTP!) and is rarely used outside of complex libraries.

If you are just starting out with the Servlet API, consider using the various do*() methods instead, such as doGet() and doPost().

See the longer answers at doGet and doPost in Servlets

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136