引言: 在使用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运行此类即可。
分享到:
相关推荐
jetty嵌入式服务器开发所必须的jar包,本人使用jetty版本为6.1.3,jar包总数为9个,来自jetty:commons-el-1.0.jar,jasper-compiler-5.5.15,jasper-compiler-jdt-5.5.15.jar,jasper-runtime-5.5.15.jar,jetty-...
java jetty嵌入式服务器实例大全,包括文件资源、servlet、xml配置服务器、war包、多端口监听等实例。
jetty 嵌入式开发源码
shell脚本启动jetty嵌入式server,shell脚本启动jetty嵌入式server
NULL 博文链接:https://seraph115.iteye.com/blog/436300
jetty嵌入式服务器开发所必须的全部jar包。本人使用jetty版本为9.0.4,这个版本比较稳定些。jar包数据很多,最后集中在jetty-all中,再外引入一下servlet-api-这个包,是与9.0.4配套的。 好东西,大家分享。
嵌入式jetty服务器开发的所有用到的jar包 包括基础jetty使用的和编译使用的ant的jar全都全了 一次性导入再也不用为找jetty依赖jar包而麻烦了
jetty嵌入式web容器jar包 示例: public static void main(String[] args) { // 服务器的监听端口 Server server = new Server(6666); // 关联一个已经存在的上下文 WebAppContext context = new WebAppContext...
jetty嵌入式服务.rar
NULL 博文链接:https://yangq.iteye.com/blog/1399621
jetty实例,嵌入式jetty,java接收json数据,取特定字符,展示到浏览器,
在javaweb项目中嵌入式使用jetty作为web服务器,下载即可运行
NULL 博文链接:https://kanpiaoxue.iteye.com/blog/2110496
基于jetty嵌入式容器的java性能分析工具,内嵌H2 database,以图表形式直观展
jetty是什么 jetty配置 jetty使用 jetty嵌入 jetty启动 jetty部署 jetty教程 jetty嵌入式 jetty
资源名称:Jetty6入门教程资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
引入到eclipse可以直接使用Starter类运行。 包含必备jar包:jetty-6.1.26.jar,jetty-util-6.1.26,servlet的相关jar包。 jetty入门级源代码。
包括org.eclipse.jetty.servlet_7.1.6.v20100715.jar、org.eclipse.jetty.server.jar、org.eclipse.jetty.servlet.source_7.1.6.v20100715.jar三个jar包,在进行jetty嵌入式编程时需要用到。
Jetty 是一个开源的servlet容器... Jetty 可以作为嵌入式服务器使用,Jetty的运行速度较快,而且是轻量级的,可以在Java中可以从test case中控制其运行。从而可以使自动化测试不再依赖外部环境,顺利实现自动化测试。
Jetty是一个100%由Java实现的、开源的HTTP服务器和javax.servlet容器,它不仅仅作为一个独立服务软件(如Tomcat) 被使用,而且其 优良的组件(Componet)设计、高内聚低耦合、高扩展性等特性使得Jetty非常易于作为...