`

深入浅出Jetty嵌入式服务器教程

阅读更多

引言: 在使用artifactory搭建Maven私服时,发现artifactory使用了jetty做为嵌入式中间件,配合内存数据库,将JEE的web应用程序做得像windows程序一样,只要双击一个.bat脚本就可以启动服务了,用老罗的话说就是:“太方便了”,但其归功于jetty的使用,下面就介绍下怎样把web程序做的像本地程序一样。

 

此篇教程需有Maven使用基础,如不太了解可以参考我的Blog中关于Maven使用的部分。

 

归正传,在开始之前,首先需要将Jetty资源加入到应用程序中,在工程的pom.xml文件的<dependencies>中添加如下依赖:

<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jetty</artifactId>
			<version>${jetty-version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.7.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-el</groupId>
			<artifactId>commons-el</artifactId>
			<version>1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.3.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.3.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xercesImpl</artifactId>
			<version>2.6.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>xerces</groupId>
			<artifactId>xmlParserAPIs</artifactId>
			<version>2.6.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>tomcat</groupId>
			<artifactId>jasper-compiler</artifactId>
			<version>5.5.15</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>tomcat</groupId>
			<artifactId>jasper-compiler-jdt</artifactId>
			<version>5.5.15</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>tomcat</groupId>
			<artifactId>jasper-runtime</artifactId>
			<version>5.5.15</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl104-over-slf4j</artifactId>
			<version>1.3.1</version>
			<scope>provided</scope>
		</dependency>

 在<dependencies>外添加properties值来指定jetty的版本

	<properties>
		<jetty-version>6.1.7</jetty-version>
	</properties>

然后在工程路径下执行mvn eclipse:eclipse命令来添加jetty依赖。

 

接下来在工程根目录下添加jetty文件夹,存放jetty的服务配置。文件为jetty.xml和realm.properties(文件包jetty配置文件.rar可在结尾处下载)

其中在jetty.xml文件中配置应用信息,如:

 

    <!-- ======================================================= -->
    <!-- Configure application                                   -->
    <!-- ======================================================= -->
    <New id="application" class="org.mortbay.jetty.webapp.WebAppContext">
		<Set name="contextPath">/BISP</Set>
		<Set name="war">src/main/webapp</Set>
        <Get name="SessionHandler">
            <Set name="SessionManager">
                <New class="org.mortbay.jetty.servlet.HashSessionManager">
                    <Set name="maxInactiveInterval">1600</Set>
                </New>
            </Set>
        </Get>
    </New>

 contextPath为服务的根路径,war为web应用路径。

 

然后在工程中添加jetty的启动类,在此类的main方法中启动服务。此类内容如下:

 

/**
 * 类说明: 启动Jetty服务器<br>
 * 创建时间: 2008-6-4 上午11:27:06<br>
 * 
 * @author seraph<br>
 * @email: seraph115@gmail.com<br>
 */
public class Main {

	/**
	 * 功能说明:<br>
	 * 创建者: seraph<br>
	 * 创建时间: 2008-6-4 上午11:27:07<br>
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		Server server = null;
		try {
			String applicationHome = System.getProperty("application.home");

			if (applicationHome == null) {
				applicationHome = System.getProperty("user.dir");
			}
			if (applicationHome != null) {
				applicationHome = applicationHome.replace('\\', '/');
			}

			URL configUrl = new URL(new StringBuffer().append("file:").append(
					applicationHome).append("/jetty/jetty.xml").toString());

			XmlConfiguration xmlConfiguration = new XmlConfiguration(configUrl);
			server = new Server();
			xmlConfiguration.configure(server);
			server.start();
			
		} catch (Exception e) {

			System.err.println(new StringBuffer().append(
					"Could not start the Jetty server: ").append(e).toString());

			if (server != null) {
				try {
					server.stop();
				} catch (Exception e1) {
					System.err.println(new StringBuffer().append(
							"Unable to stop the jetty server: ").append(e1)
							.toString());
				}
			}
		}

	}

}

 

至此,在开发调试或应用发布时只需使用java运行此类即可。

4
0
分享到:
评论
1 楼 expone 2011-10-17  
没运行成功啊。404错误。

相关推荐

Global site tag (gtag.js) - Google Analytics