Home:ALL Converter>Migration from Jetty 6 to Jetty 8

Migration from Jetty 6 to Jetty 8

Ask Time:2012-02-14T22:47:53         Author:NullPointer

Json Formatter

I use jetty6 in simple application as embedded servlet container. I decided to update it to Jetty 8. In jetty 6 it was pretty simple to start the server:

Server server = new Server(8080);
Context context = new Context(server, "/", Context.SESSIONS);
context.addServlet(MyServlet.class, "/communication-service");
server.start();

but it doesn't work in Jetty8. Unfortunately I can't find any simple example for this version. Can't instantiate Context with error

an enclosing instance that contains
    org.eclipse.jetty.server.handler.ContextHandler.Context is required

because now it is an inner class and also no such constructor.

Most examples are for jetty 6 and 7. Could you please provide simple example how to start servlet at jetty 8?

Author:NullPointer,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9278912/migration-from-jetty-6-to-jetty-8
Tim :

This is the Jetty 8 equivalent to your code. It's still just as simple as it was before, however the API has changed slightly.\n\nIf this isn't working for you, then you probably have a classpath issue - Jetty 8 is separated into a lot of independent jar files, and you will need a number of them. At the very least you need:\n\n\njetty-continuation\njetty-http\njetty-io\njetty-security\njetty-server\njetty-servlet\njetty-util\nservlet-api\n\n\nIf you have those jars, then this code should work fine:\n\npackage test;\n\nimport java.io.IOException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport org.eclipse.jetty.server.Server;\nimport org.eclipse.jetty.servlet.ServletContextHandler;\n\npublic class Jetty8Server {\n public static class MyServlet extends HttpServlet {\n protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {\n response.setContentType(\"text/plain\");\n response.getWriter().write(getClass().getName() + \" - OK\");\n }\n }\n public static void main(String[] args) throws Exception {\n Server server = new Server(8080);\n ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);\n handler.setContextPath(\"/\"); // technically not required, as \"/\" is the default\n handler.addServlet(MyServlet.class, \"/communication-service\");\n server.setHandler(handler);\n server.start();\n }\n}\n",
2012-02-17T05:47:40
yy