Embedded Jetty 9

I do not understand how I can rewrite this code used with berth 6 for berth 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 above code works fine and responds to static content from a web folder and servlets displayed in web.xml. Here are my attempts to use the built-in pier 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 starts, but index.html asks for an error:

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

I tried to find a working example on Google, but did not find anything useful. The official samples and documentation are very confusing, and I don’t understand how I can use the built-in version of berth 9.

+6
source share
3 answers

The error message clearly indicates that you have the wrong version of the servlet API in your class path.

Check your dependencies, you probably have an API servlet up to 3.0, it should be removed.

+9
source

Adding what @axtavt said: if you are using maven, add the following degree:

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

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

0
source

If you are using Gradle, do

 gradle dependencies 

analyze the dependency tree and exclude "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 may be several dependencies that also include servlet-api-2.x. Exclude all of these

0
source

All Articles