读书人

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

发布时间: 2012-07-22 19:31:17 作者: rapoo

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

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

?

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

?

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

/** * 类说明: 启动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运行此类即可。

1 楼 expone 2011-10-17 没运行成功啊。404错误。

读书人网 >软件开发

热点推荐