4

Is it possible to launch a webapp altogether with:

1) No war file:

http://stephenh.github.io/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...)

ManRow
  • 1,563
  • 4
  • 21
  • 40

4 Answers4

2

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

You'll still need a WEB-INF/web.xml, but it can be empty. This is so that the servlet support level and metadata-complete flags 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 the EmbedMe.java for an example on how to set this up.

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();
    }
}
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Doesn't work, I'm getting a 404 Error trying to access localhost:8080 (I'm running this from Eclipse, simply as "Java Application"). I'm also using the latest http://github.com/jetty-project/embedded-servlet-3.0 :( – ManRow Apr 09 '13 at 20:59
  • Try command line on that project ... $ mvn clean install exec:exec – Joakim Erdfelt Apr 09 '13 at 21:36
  • Will I still have to apply this workaround: https://bugs.eclipse.org/bugs/show_bug.cgi?id=404176 to use the "AnnotationConfig()" you've shown outside of WEB-INF/? – ManRow Apr 12 '13 at 00:35
  • This solution packages as a WAR – fionbio Oct 21 '15 at 17:52
  • @fionbio its an exploded webapp directory. a WAR is a web archive. – Joakim Erdfelt Oct 21 '15 at 18:55
2

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

Use Spring's @WebAppConfiguration to bootstrap your WebApplicationContext with a MockServletContext,

then simply register your new DispatcherServlet(WebApplicationContext) via Jetty's ServletContextHandler/ServletHolder mechanism. Easy!

ManRow
  • 1,563
  • 4
  • 21
  • 40
1

It's possible to achieve an embedded server with Jetty that doesn't require a WAR or any XML. You just have to specify where your annotated classes will be, adding an extra class path.

This method should be called from a main you can name 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 was:

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

I would like to see a similar solution with Tomcat.

0

See the answer of BalusC for simple HTTP server in Java using only Java SE API. The example is using the class com.sun.net.httpserver.HttpServer.

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Not quite what I'm seeking; I'm essentially trying to programmatically load a webapp (not a simple manual HttpServer) into an embedded web container without having to deploy a "war file" or even having a web.xml. Specifically, a Spring WebApplicationContext... – ManRow Apr 09 '13 at 08:00