Easiest Way To Embed Jetty With A Spring Web Project

So, you have your spring project that works great inside of something like Tomcat or Resin, but you want to distribute it like a normal executable .jar file.  Look around the Internet, and you'll find some extremely complex directions.  They don't have to be.  As an example, you want to be able to access the pages at http://localhost:8080/cruise/, and you have a standard WebContent folder that contains WEB-INF in it.   Just add the jetty jar files to your classpath, and then create a main class with the following:

package yourProject; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class MainClass public static void main(String[] args) throws Exception{ Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setDescriptor("/WEB-INF/web.xml"); context.setResourceBase("WebContent"); context.setContextPath("/cruise"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); server.join(); } }

No comments

Post a Comment