Servlet 3.0 without WAR file?

Is it possible to run webapp with:

1) There is no war file:

http://stephenh.imtqy.com/2009/01/10/war-less-dev-with-jetty.html http://www.jamesward.com/2011/08/23/war-less-java-web -apps

2) No web.xml (i.e. Servlet-3.0)

3) From an embedded web container (e.g. Tomcat or Jetty ...)

+4
source share
4 answers

How I did it (built-in SpringMVC + Jetty, no web.xml no war files):

Use Spring @WebAppConfiguration to load WebApplicationContext using MockServletContext ,

then just register your new DispatcherServlet(WebApplicationContext) through the Jetty ServletContextHandler / ServletHolder mechanism. Easy!

+2
source

Project Example: https://github.com/jetty-project/embedded-servlet-3.0

You will still need WEB-INF/web.xml , but it may be empty . This means that the level of servlet support and metadata can be known.

Example: empty servlet 3.0 web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="false" version="3.0"> </web-app> 

Then you can follow EmbedMe.java for an example on how to install this.

 public class EmbedMe { public static void main(String[] args) throws Exception { int port = 8080; Server server = new Server(port); String wardir = "target/sample-webapp-1-SNAPSHOT"; WebAppContext context = new WebAppContext(); context.setResourceBase(wardir); context.setDescriptor(wardir + "WEB-INF/web.xml"); context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebXmlConfiguration(), new WebInfConfiguration(), new TagLibConfiguration(), new PlusConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration(), new EnvConfiguration() }); context.setContextPath("/"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); server.join(); } } 
+2
source

It is possible to create an embedded server with Jetty that does not require WAR or any XML. You just need to specify where your annotated classes will be by adding an extra class path.

This method should be called from the main one, you can call Server.java :

 private static void startServer() throws Exception { final org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(7070); final WebAppContext context = new WebAppContext("/", "/"); context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration() }); context.setExtraClasspath("build/classes/main/com/example/servlet"); server.setHandler(context); server.start(); server.join(); } 

My src structure:

 -main -java -com.example -json -servlet -filter -util Server.java 

I would like to see a similar solution with Tomcat.

0
source

All Articles