pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" < modelVersion >4.0.0</ modelVersion > < groupId >org.fredy</ groupId > < artifactId >exec-war</ artifactId > < version >0.1</ version > < packaging >war</ packaging > < name >exec-war</ name > < dependencies > < dependency > < groupId >org.mortbay.jetty</ groupId > < artifactId >jetty</ artifactId > < version >6.1.25</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < configuration > < source >1.6</ source > < target >1.6</ target > </ configuration > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-assembly-plugin</ artifactId > < configuration > < archive > < manifest > < mainClass >myproject.Main</ mainClass > </ manifest > </ archive > < descriptorRefs > < descriptorRef >jar-with-dependencies</ descriptorRef > </ descriptorRefs > </ configuration > < executions > < execution > < id >jar-with-dependencies</ id > < phase >package</ phase > < goals > < goal >single</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > </ project > |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? xml version = "1.0" encoding = "UTF-8" ?> < web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > < display-name >exec-war</ display-name > < session-config > < session-timeout >30</ session-timeout > </ session-config > < servlet > < servlet-name >MyServlet</ servlet-name > < servlet-class >myproject.servlet.MyServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >MyServlet</ servlet-name > < url-pattern >/MyServlet</ url-pattern > </ servlet-mapping > </ web-app > |
MyServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package myproject.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } private void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType( "text/html" ); PrintWriter pw = resp.getWriter(); try { pw.print( "<html><body><h1>Hello World</h1></body></html>" ); } finally { pw.close(); } } } |
Main.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package myproject; import java.net.URL; import org.mortbay.jetty.Connector; import org.mortbay.jetty.Server; import org.mortbay.jetty.nio.SelectChannelConnector; import org.mortbay.jetty.webapp.WebAppContext; public class Main { public static void main(String[] args) throws Exception { Server server = new Server(); Connector connector = new SelectChannelConnector(); connector.setPort( 8888 ); connector.setHost( "127.0.0.1" ); server.addConnector(connector); URL warUrl = Main. class .getClassLoader().getResource( "WEB-INF" ); System.out.println(warUrl); String warUrlString = warUrl.toExternalForm(); server.setHandler( new WebAppContext(warUrlString, "/exec-war" )); server.setStopAtShutdown( true ); server.start(); } } |
1. Package a WAR file
mvn package2. Rename the package name from JAR to WAR. The JAR still has WAR structure. The file has a JAR extension because of the maven-assembly-plugin. So, it's basically a JAR file with a WAR structure inside.
3. Run it.
java -jar exec-war.war4. Or deploy it in a web container, such as Tomcat.