24.7.04
12:22 Embedded web server: jetty (
,
,
,
)
Java: ysesterday I looked for embedded java web browser and discovered jetty. Compared to other java web server, the advantages of jetty are:
- contains a ServletHandler which allows you to write standard servlets and run them in jetty
- embedded: you don't have to install (and maintain, configure) a web server. The web server (HttpContext) is in your application, you just bind it on a port, register a servlet and that's it. Your application runs like any other Java application.
- Apache license: good for distribution
- Small memory consumption: my test application (web server, one servlet) runs with 8MB memory.
- large installatiton base
Code extracts:
// Create HttpServer and listen on port 8181
HttpServer server = new HttpServer();
HttpListener listener= server.addListener(new InetAddrPort("localhost",8181));
// Create a HTTP context and add it to the serverHttpContext context = new HttpContext();
context.setContextPath("/test/*");
server.addContext(context);
// Create a servlet container and add to the context
ServletHandler servlets = new ServletHandler();
context.addHandler(servlets);
// Map a servlet onto the container
// "/servletX/*" is the URL path under which the servlet will be reachable
// "org.package.MyServlet" is the servlet which will be called
servlets.addServlet("ServletName","/servletX/*","org.package.MyServlet"); That's it. Write your servlet, map it onto the container and run your application. The servlet will be reachable under "http://localhost:8181/servletX".
Few handlers come with jetty: RessourceHandler (serve static content), DumpHandler (dump request and response headers), SecurityHandler (for authentication), ForwardHandler, NotFoundHanlder ...
posted by Jean-Marc Autexier |
3 comments | Permalink | Send to Friends | Google it!
|