Jetty Embedded Jetty running web App
Blog Category:
Jettywar
Reprint Address: http://blog.csdn.net/kongxx/article/details/7237034
To say that the embedded run jetty, the most commonly used should be to run a standard war file or specify a WebApp directory.
0. First you need to add a dependency package for the Jetty Runtime WebApp, here is a complete pom.xml file
Wrote <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. Running a standard war file
1.1 First find a complete war package, here use a struts2 to bring an example application Struts2-blank.war;
1.2 Create your own jetty Server startup class Webappcontextwithwarserver, which specifies the path to the war file and specifies that the context path is "/myapp"
Java code
- 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 access//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, the war package decompression and put into a directory;
2.2 Create your own jetty Server startup class Webappcontextwithfolderserver, which specifies the WebApp directory, and specifies that the context path is "/myapp"
Java code
- 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 access//http://localhost:8080/myapp/to see the example interface of STRUTS2.
Jetty Embedded Jetty running web App