Embedded Jetty with Servlet and Annotations

How do I programmatically configure Jetty that I can use Servlet Annotations?

If you doing just servlets with jetty, this article could be interesting for you.

I show you how I did it. Here comes the configuration:

[code]

server = new Server(port);
server.addBean(LOG);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
String webxmlLocation = JettyServer.class.getResource("/webapp/WEB-INF/web.xml").toString();
webAppContext.setDescriptor(webxmlLocation);
String resLocation = JettyServer.class.getResource("/webapp").toString();
webAppContext.setResourceBase(resLocation); webAppContext.setParentLoaderPriority(true);
webAppContext.getMetaData().addContainerResource(Resource.newResource(
new File(JettyServer.class.getProtectionDomain().getCodeSource().getLocation().getPath())));
webAppContext.setConfigurations(new Configuration[] { new WebInfConfiguration(), new WebXmlConfiguration(),
new MetaInfConfiguration(), new FragmentConfiguration(), new EnvConfiguration(), new PlusConfiguration(),
new AnnotationConfiguration(), new JettyWebXmlConfiguration() });
server.setHandler(webAppContext);
server.start();

[/code]

And I defined the following dependencies in build.gradle.

[code]

def jettyVersion = ‚9.4.7.v20170914‘
dependencies {
providedCompile ‚javax.servlet:javax.servlet-api:3.1.0‘
compile group: ‚org.eclipse.jetty‘, name: ‚jetty-servlet‘, version: jettyVersion
compile group: ‚org.eclipse.jetty‘, name: ‚jetty-webapp‘, version: jettyVersion
compile group: ‚org.eclipse.jetty‘, name: ‚jetty-annotations‘, version: jettyVersion
compile group: ‚org.eclipse.jetty‘, name: ‚jetty-plus‘, version: jettyVersion
compile group: ‚org.eclipse.jetty‘, name: ‚apache-jsp‘, version: jettyVersion
compile group: ‚org.ow2.asm‘, name: ‚asm‘, version: ‚5.1‘
compile group: ‚org.slf4j‘, name: ’slf4j-api‘, version: ‚1.7.25‘
compile group: ‚org.slf4j‘, name: ’slf4j-log4j12′, version: ‚1.7.25‘
compile group: ‚log4j‘, name: ‚log4j‘, version: ‚1.2.17‘

}

[/code]

That’s all. Hope this helps!

GitHub-Mark-32pxHere is the link to an working github project:
jetty-embedded-webannotations-gradle

 

 

 

 

 

 

 

Consent Management Platform von Real Cookie Banner