Jetty combat (6) embedded Jetty running web app

When it comes to running Jetty embedded, the most commonly used should be to run a standard war file or specify a webapp directory.

0. First you need to add the dependency package of the Jetty runtime webapp, the following is a complete pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.google.code.garbagecan.jettystudy</groupId>  
    <artifactId>jettystudy</artifactId>  
    <packaging>jar</packaging>  
    <version>1.0-SNAPSHOT</version>  
    <name>jettystudy</name>  
    <url>http://maven.apache.org</url>  
    <build>  
        <plugins>  
            <plugin>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <inherited>true</inherited>  
                <version>2.3.1</version>  
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                    <debug>true</debug>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
    <dependencies>  
        <!-- Spring support -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring</artifactId>  
            <version>2.5.6</version>  
        </dependency>  
          
        <!-- Jetty -->  
        <dependency>  
            <groupId>org.eclipse.jetty.aggregate</groupId>  
            <artifactId>jetty-all</artifactId>  
            <version>8.0.4.v20111024</version>  
        </dependency>  
  
        <!-- Jetty Webapp -->  
        <dependency>  
            <groupId>org.eclipse.jetty</groupId>  
            <artifactId>jetty-webapp</artifactId>  
            <version>8.0.4.v20111024</version>  
        </dependency>  
  
        <!-- JSP Support -->  
        <dependency>  
            <groupId>org.glassfish.web</groupId>  
            <artifactId>javax.servlet.jsp</artifactId>  
            <version>2.2.3</version>  
        </dependency>  
  
        <!-- EL Support -->  
        <dependency>  
            <groupId>org.glassfish.web</groupId>  
            <artifactId>javax.el</artifactId>  
            <version>2.2.3</version>  
        </dependency>  
  
        <!-- JSTL Support -->  
        <dependency>  
            <groupId>org.glassfish.web</groupId>  
            <artifactId>javax.servlet.jsp.jstl</artifactId>  
            <version>1.2.1</version>  
            <exclusions>  
                <exclusion>  
                    <artifactId>jstl-api</artifactId>  
                    <groupId>javax.servlet.jsp.jstl</groupId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
    </dependencies>  
</project>

 1. Run the standard war file

 

1.1 First find a complete war package, here is an example application struts2-blank.war that comes with struts2;

1.2 Create your own Jetty Server startup class WebAppContextWithWarServer, which specifies the path to the war file and specifies the context path as "/myapp"

package com.google.code.garbagecan.jettystudy.sample6;  
  
import org.eclipse.jetty.server.Server;  
import org.eclipse.jetty.webapp.WebAppContext;  
  
public class WebAppContextWithWarServer {  
    public static void main(String[] args) throws Exception {  
        Server server = new Server(8080);  
  
        WebAppContext context = new WebAppContext();  
        context.setContextPath("/myapp");  
        context.setWar("E:/share/test/struts2-blank.war");  
        server.setHandler(context);  
  
        server.start();  
        server.join();  
    }  
}  

 1.3 Run the WebAppContextWithWarServer class, and then visit // http://localhost:8080/myapp/ to see the example interface of struts2.

 

 

2. Run a webapp directory

2.1 Or use the above struts2-blank.war, decompress the war package and put it in a directory;

2.2 Create your own Jetty Server startup class WebAppContextWithFolderServer, which specifies the webapp directory and specifies the context path as "/myapp"

package com.google.code.garbagecan.jettystudy.sample6;  
  
import org.eclipse.jetty.server.Server;  
import org.eclipse.jetty.webapp.WebAppContext;  
  
public class WebAppContextWithFolderServer {  
    public static void main(String[] args) throws Exception {  
        Server server = new Server(8080);  
  
        WebAppContext context = new WebAppContext();  
        context.setContextPath("/myapp");  
        context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml");  
        context.setResourceBase("E:/share/test/struts2-blank");  
        context.setParentLoaderPriority(true);  
        server.setHandler(context);  
  
        server.start();  
        server.join();  
    }  
}  

 2.3 Run the WebAppContextWithFolderServer class, and then visit // http://localhost:8080/myapp/ to see the example interface of struts2.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390378&siteId=291194637