这个是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配置
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
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启动程序:
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 }