这个是http://www.codeproject.com/Articles/128145/Run-Jetty-Web-Server-Within-Your-Application上的描述
Introduction
The purpose of this article is to give you a quick idea of how to build and deploy your web application without really running a web server outside your application and without creating a WAR file for the deployment. At present, I am working on a project which is a background calculation engine and it provides a simple web interface for the support and business users to monitor the ongoing activities. While deploying our application, we just start Jetty web server within our application and we can access the web application easily.
If you visit the Jetty wiki, it is mentioned that Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." In this article, we illustrate this slogan in a nice and simple application.
以下是我自己的实现:
1、项目结构如下:
2、/WEB-INF/web.xml配置
web.xml配置
1 <?xml version="1.0" encoding="ISO-8859-1" ?>
2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
4 metadata-complete="false" version="3.0">
5
6 <welcome-file-list>
7 <welcome-file>page/index.jsp</welcome-file>
8 </welcome-file-list>
9 </web-app>
3、/page/index.jsp
html jsp 文件
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2 pageEncoding="ISO-8859-1"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h1>my first jetty web jsp program</h1>
11
12 <h2>Running Jetty web server from our application!!</h2>
13 </body>
14 </html>
4、servlet启动程序:
java 启动程序
1 package my.project.web.app.jetty;
2
3 import org.eclipse.jetty.server.Server;
4 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
5 import org.eclipse.jetty.webapp.WebAppContext;
6
7 public class AppContextBuilder {
8
9 public static void main(String[] args) throws Exception {
10
11 // 调用jetty服务
12 Server server = new Server(9500);
13
14 // 加载项目集合
15 ContextHandlerCollection contexts = new ContextHandlerCollection();
16
17 // jsp内容
18 WebAppContext webAppContext = new WebAppContext();
19 webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
20 webAppContext.setResourceBase(".");
21
22 // 访问路径
23 webAppContext.setContextPath("/");
24
25 // 将 webAppContext项目加载到项目集合中
26 contexts.addHandler(webAppContext);
27
28 // 将项目集合加载到服务器中
29 server.setHandler(contexts);
30
31 // 启动服务器
32 server.start();
33 server.join();
34 }
35 }
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com