Question

I dont understand how i can rewrite this code used with jetty 6 for jetty 9 :

import org.mortbay.jetty.*;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class ApplLauncher {
    public static void main(String[] args) {
        Server server = new Server();

        Connector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.addConnector(connector);

        WebAppContext root = new WebAppContext("C:\\Users\\OZKA\\IdeaProjects\\projectname\\projectname\\web", "/");
        root.setWelcomeFiles(new String[]{"index.html"});
        //root.addServlet(new ServletHolder(new TestServlet()), "/test");

        server.setHandlers(new Handler[]{root});

        try {
            server.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

The code above works fine and response the static content from the web folder and servlets, mapped in web.xml. Here is my attempts to use embeded jetty 9:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.Handler;


public class ApplLauncher {
    public static void main(String[] args) {
        System.out.println("Hello from ScalaSbt Web Project");
        Server server = new Server(8080);
        WebAppContext webapp = new WebAppContext("D:\\Dev\\Scala\\ScalaTestProject\\web\\", "/");
        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setWelcomeFiles(new String[]{ "index.html" });
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resource_handler, webapp});
        server.setHandler(handlers);
        try {
            server.start();
            server.join();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }

}

The server is starting, but index.html request throws error:

"java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z"

I tried to find a working example in Google, but found nothing useful. Official samples and documentation is very confusing and I do not understand how i can use embeded jetty version 9.

Était-ce utile?

La solution

Error message clearly indicates that you have wrong version of Servlet API in your classpath.

Check your dependencies, you probably have pre-3.0 Servlet API somewhere, it should be removed.

Autres conseils

If you are using Gradle the execute

gradle dependencies

analyze the dependency tree and exclude the 'servlet-api' dependencies with version less than 3.0. You can do the following to exclude

compile ('javax.servlet:jsp-api:2.0'){
    exclude module : 'servlet-api'
}

There can be multiple dependencies which further include servlet-api-2.x. Exclude all those

Adding on what @axtavt said: if you are using maven add the following depedency:

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0-alpha-1</version>
        <scope>provided</scope>
 </dependency>

Also do a mvn dependency:tree |grep servlet and double-check that you do not have servlet-api:2.x imported.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top