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?