jetty 嵌入式web服务器使用

要说嵌入式运行Jetty,最常用的还应该是运行一个标准的war文件或者指定一个webapp目录。

0. 首先需要添加Jetty运行时webapp的依赖包,下面是一个完整的pom.xml文件

[html] view
plain
 copy

 print?

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.google.code.garbagecan.jettystudy</groupId>  
  5.     <artifactId>jettystudy</artifactId>  
  6.     <packaging>jar</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>jettystudy</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <build>  
  11.         <plugins>  
  12.             <plugin>  
  13.                 <artifactId>maven-compiler-plugin</artifactId>  
  14.                 <inherited>true</inherited>  
  15.                 <version>2.3.1</version>  
  16.                 <configuration>  
  17.                     <source>1.6</source>  
  18.                     <target>1.6</target>  
  19.                     <debug>true</debug>  
  20.                 </configuration>  
  21.             </plugin>  
  22.         </plugins>  
  23.     </build>  
  24.     <dependencies>  
  25.         <!-- Spring support -->  
  26.         <dependency>  
  27.             <groupId>org.springframework</groupId>  
  28.             <artifactId>spring</artifactId>  
  29.             <version>2.5.6</version>  
  30.         </dependency>  
  31.           
  32.         <!-- Jetty -->  
  33.         <dependency>  
  34.             <groupId>org.eclipse.jetty.aggregate</groupId>  
  35.             <artifactId>jetty-all</artifactId>  
  36.             <version>8.0.4.v20111024</version>  
  37.         </dependency>  
  38.   
  39.         <!-- Jetty Webapp -->  
  40.         <dependency>  
  41.             <groupId>org.eclipse.jetty</groupId>  
  42.             <artifactId>jetty-webapp</artifactId>  
  43.             <version>8.0.4.v20111024</version>  
  44.         </dependency>  
  45.   
  46.         <!-- JSP Support -->  
  47.         <dependency>  
  48.             <groupId>org.glassfish.web</groupId>  
  49.             <artifactId>javax.servlet.jsp</artifactId>  
  50.             <version>2.2.3</version>  
  51.         </dependency>  
  52.   
  53.         <!-- EL Support -->  
  54.         <dependency>  
  55.             <groupId>org.glassfish.web</groupId>  
  56.             <artifactId>javax.el</artifactId>  
  57.             <version>2.2.3</version>  
  58.         </dependency>  
  59.   
  60.         <!-- JSTL Support -->  
  61.         <dependency>  
  62.             <groupId>org.glassfish.web</groupId>  
  63.             <artifactId>javax.servlet.jsp.jstl</artifactId>  
  64.             <version>1.2.1</version>  
  65.             <exclusions>  
  66.                 <exclusion>  
  67.                     <artifactId>jstl-api</artifactId>  
  68.                     <groupId>javax.servlet.jsp.jstl</groupId>  
  69.                 </exclusion>  
  70.             </exclusions>  
  71.         </dependency>  
  72.     </dependencies>  
  73. </project>  

其实不需要这么多依赖,maven的依赖只需要一个依赖就ok了

<dependency>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>8.1.16.v20140903</version>
</dependency>

1. 运行标准的war文件

1.1 首先找一个完整的war包,这里使用了struts2自带的一个例子应用程序struts2-blank.war;

1.2 创建自己的Jetty Server启动类WebAppContextWithWarServer,其中指定了war文件的路径,并指定context路径为"/myapp"

[java] view
plain
 copy

 print?

  1. package com.google.code.garbagecan.jettystudy.sample6;  
  2.   
  3. import org.eclipse.jetty.server.Server;  
  4. import org.eclipse.jetty.webapp.WebAppContext;  
  5.   
  6. public class WebAppContextWithWarServer {  
  7.     public static void main(String[] args) throws Exception {  
  8.         Server server = new Server(8080);  
  9.   
  10.         WebAppContext context = new WebAppContext();  
  11.         context.setContextPath("/myapp");  
  12.         context.setWar("E:/share/test/struts2-blank.war");  
  13.         server.setHandler(context);  
  14.   
  15.         server.start();  
  16.         server.join();  
  17.     }  
  18. }  

1.3 运行WebAppContextWithWarServer类,然后访问// http://localhost:8080/myapp/就可以看到struts2的例子界面了。

2. 运行一个webapp目录

2.1 还是用上面的struts2-blank.war,将这个war包解压后放到一个目录下;

2.2 创建自己的Jetty Server启动类WebAppContextWithFolderServer,其中指定了webapp目录,并指定context路径为"/myapp"

[java] view
plain
 copy

 print?

  1. package com.google.code.garbagecan.jettystudy.sample6;  
  2.   
  3. import org.eclipse.jetty.server.Server;  
  4. import org.eclipse.jetty.webapp.WebAppContext;  
  5.   
  6. public class WebAppContextWithFolderServer {  
  7.     public static void main(String[] args) throws Exception {  
  8.         Server server = new Server(8080);  
  9.   
  10.         WebAppContext context = new WebAppContext();  
  11.         context.setContextPath("/myapp");  
  12. //web.xml可以改成其他名字,如web1.xml。
  13. //如果不指明配置文件的位置,会根据项目资源setResourceBase路径找下面的web.xml。如果找不到,默认页面index.html优先于index.jsp
  14.         context.setDescriptor("E:/HLZT/chat/src/main/webapp/WEB-INF/web.xml");
     //maven项目web.xml路径
  15.  
    context.setResourceBase("E:/HLZT/chat/src/main/webapp/");//maven项目资源路径
  16. //context.setDescriptor("E:/HLZT/chat/WEB-INF/web.xml");
    //普通项目web.xml路径 
  17. //context.setResourceBase("E:/HLZT/chat/WebRoot");//普通项目资源路径 
  18.          
  19.         context.setParentLoaderPriority(true);  
  20.         server.setHandler(context);  
  21.   
  22.         server.start();  
  23.         server.join();  
  24.     }  
  25. }  

2.3 运行WebAppContextWithFolderServer类,然后访问// http://localhost:8080/myapp/就可以看到struts2的例子界面

时间: 2024-08-31 19:00:21

jetty 嵌入式web服务器使用的相关文章

嵌入式web服务器数据存储

问题描述 嵌入式web服务器数据存储 如题,嵌入式web服务器的数据存储方式,还有就是类似于路由器设备,其登陆 界面的数据存储,是存入数据库,还是文件存储呢,存数据库是哪种数据库,文件是什么格式 解决方案 根据硬件条件和需求选择,简单数据可以存储为文件,或自定义格式,嵌入式数据库的话通常用SQLLITE

wifi-有关STM32嵌入式WEB服务器的问题。

问题描述 有关STM32嵌入式WEB服务器的问题. 各位大神,小弟想问一下,使用平常的串口Wifi模块(如ESP8266),搭配STM32系列单片机,能实现WifiDog功能吗,就像市面卖的路由器那样,访问别的网址会跳转到验证页面那样. 顺便弱弱地问一下,在局域网的条件下能实现么? 解决方案 只要你的soc方案可以支持运行稍微完整一些的linux内核,就可以支持.软件大多数都是现成的. 解决方案二: 没有问题,这有个例子 stm32 ,Linux ,Wifi ,嵌入式智能控制:http://pa

Embedthis Goahead-Webserver 2.5发布 嵌入式Web服务器

Embedthis Goahead-Webserver 是一款小巧的嵌入式http://www.aliyun.com/zixun/aggregation/17117.html">Web服务器,能够嵌入到各种操作系统和CPU. Embedthis Goahead-Webserver 2.5该版本增加了清单文件验证和MatrixSSL支持,几个安全问题已得到解决. 软件信息:http://embedthis.com/products/webserver/goahead-webserver.ht

Embedthis Appweb 3.3.3发布 嵌入式Web服务器

Embedthis Appweb 是一款快速小型的http://www.aliyun.com/zixun/aggregation/17117.html">Web服务器.它主要用于嵌入式应用.设备和Web服务. Embedthis Appweb 功能包括:支持同步和异步两种范例.SSL.TLS.基本和摘要式身份验证.虚拟主机.可装载模块.Apache样式配置的文件.PHP.ESP.Ejscript.CGI.sandbox资源限制.日志记录.过程监测,以及配置和编译控制的扩展. Embedth

嵌入式设备web服务器比较

目录(?)[-] Boa Thttpd Mini_httpd Shttpd Lighttpd Goahead AppWeb Apache 开发语言和开发工具 结论 备注   现在在嵌入式设备中所使用的web服务器主要有:boa.thttpd.mini_httpd.shttpd.lighttpd.goaheand.appweb和apache等. Boa 1.介绍 Boa诞生于1991年,作者Paul Philips.是开源的,应用很广泛,特别适合于嵌入式设备,网上流行程度很广.它的官方网站说boa

嵌入式设备web服务器

操作系统:ubuntu10.04 前言:    为了提高对设备的易操作性,很多设备中提供pc机直接通过浏览器操作设备的功能.这就需要在设备中实现web服务器.    现在在嵌入式设备中所使用的web服务器主要有:boa.thttpd.mini_httpd.shttpd.lighttpd.goaheand.appweb和apache等. 一,比对             Boa 1.介绍 Boa诞生于1991年,作者Paul Philips.是开源的,应用很广泛,特别适合于嵌入式设备,网上流行程度

利用GoAhead构建嵌入式web应用

1 简介 廉价的硬件,功能强大的32操作系统,以及无处不在的因特网,它们一起促成了网络应用和设备的飞速增长.大量的设备连接到网络上,于是人们希望通过一种通用.熟悉.快捷的方式来访问和控制它们.嵌入式web服务器正好迎合了这种需求,它们嵌入在网络设备之中,使用标准的浏览器就可以远程访问和控制它们. 然而,并不是所有的web服务器都可以担当如此重任,我们需要的是一个强大,安全,标准的,而且最好是久经考验的嵌入式web服务器.这里将要介绍的GoAhead嵌入式web服务器能够满足所有这些需求,包括西门

KLone v2.4.0发布 嵌入式HTTP服务器和Web开发框架

KLone 是一个全功能的支持多平台的嵌入式HTTP服务器软件和Webhttp://www.aliyun.com/zixun/aggregation/13435.html">开发框架,主要用在一些嵌入式设备中. KLone特点: Multiplatform HTTP and HTTP/S KLone allows the creation of HTTP and HTTP/s servers which run on a wide range of platforms, even with

嵌入式Web视频点播系统实现方法

web 关键字 RealPlayer,嵌入式.流媒体,网络传输,PHP,DHTML,ActiveX 近年来,视频点播VOD(Video on Demand)的名字在媒体上出现得越来越多.VOD技术使人们可以根据自己的兴趣,不用借助录像机.影碟机.有线电视而在电脑或电视上自由地点播节目库中的视频节目和信息,是可以对视频节目内容进行自由选择的交互式系统. VOD的本质是信息的使用者根据自己的需求主动获得多媒体信息,它区别于信息发布的最大不同:一是主动性.二是选择性.从某种意义上说这是信息的接受者根据