【转】jetty快速入门与嵌入使用
上一篇 / 下一篇 2011-03-24 18:28:26 / 个人分类:jetty
看到开源项目发布的时候都带一个 jsp 容器(jetty)。拿来做 demo、开发、调试的服务器还是很不错的。今天就小试下,主要把它运行起来。
4I*x\ hb-z%U'\g0MA| _/`t0第一步下载:http://dist.codehaus.org/jetty/jetty-6.1.14/jetty-6.1.14.zip 是 目前最新的稳定版。解压到如E:\jetty-6.1.14,其中比较重要的目录是:etc、contexts、webapps。个人认为可以类比 tomcat的conf、conf\Catalina\localhost、webapps目录。contexts是热部署用的。51Testing软件测试网}cSHn,pP{(y"`O
+VZdz"F3XM2P#|0试运行下,可以把一个简单的web项目到到webapps目录下,或是*.war,如:web-demo(或web-demo.war)放到webapps目录下。
6L^F UN'a6c-U0E:\jetty-6.1.14>java -jar start.jar51Testing软件测试网2S%`[aG$yxk]
5| c+f4E4Wbo(yt02009-01-13 15:34:38.937::INFO: Logging to STDERR via org.mortbay.log.StdErrLog51Testing软件测试网?]'q2J/DYw j
2009-01-13 15:34:39.265::INFO: jetty-6.1.14
,Y"RtT:h,qv02009-01-13 15:34:39.421::INFO: Deploy E:\jetty-6.1.14\contexts\javadoc.xml -> org.mortbay.jetty.handler.ContextHandler@15cda3f{/javadoc,file:/E:/jetty-6.1.14/javadoc/}
3k#T1v.S!W0...
X#_V-ro3_D8^02009-01-13 15:35:01.828::INFO: Opened E:\jetty-6.1.14\logs\2009_01_13.request.log
'@0U2t@z;c1?2[02009-01-13 15:35:01.953::INFO: Started SelectChannelConnector@127.0.0.1:808051Testing软件测试网5YTtC2c}
打开:http://localhost:8080/web-demo ,恩有结果了。
UH:a1I(dWY0m _q G*~a~0如果不把web-demo放到webapps目录下也可,在contexts目录下建立一个文件告诉jetty就行了。可以在contexts目录下复制test.xml为web-demo.xml,然后修改如下:51Testing软件测试网!}"Vyhn!{1j0r
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
- <Configure class="org.mortbay.jetty.webapp.WebAppContext">
- <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
- <!-- Required minimal context configuration : -->
- <!-- + contextPath -->
- <!-- + war OR resourceBase -->
- <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
- <Set name="contextPath">/web-demo</Set>
- <Set name="war">e:/workspace/web-demo/WebContent</Set>
- <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
- <!-- Optional context configuration -->
- <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
- <Set name="extractWAR">false</Set>
- <Set name="copyWebDir">false</Set>
- <Set name="defaultsDescriptor">
- <SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>
- <!--
- <Set name="overrideDescriptor"><SystemProperty name="jetty.home"
- default="."/>/contexts/test.d/override-web.xml</Set>
- -->
- </Configure>
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure class="org.mortbay.jetty.webapp.WebAppContext"> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Required minimal context configuration : --> <!-- + contextPath --> <!-- + war OR resourceBase --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <Set name="contextPath">/web-demo</Set> <Set name="war">e:/workspace/web-demo/WebContent</Set> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Optional context configuration --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <Set name="extractWAR">false</Set> <Set name="copyWebDir">false</Set> <Set name="defaultsDescriptor"> <SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set> <!-- <Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/contexts/test.d/override-web.xml</Set> --> </Configure>51Testing软件测试网8P;PVzJ&F#ii,y
war可以设置成绝对路径。然后再重启jetty试试看。好了, 对jetty的初步认识到这里。51Testing软件测试网g/Q3?)P?7h%^J,?U
51Testing软件测试网8EB#@/SR [下面再来看下,怎么把它放到项目中。现我有一示例项目 e:/workspace/web-demo(称为project_home),里面的Web根目录是WebContent。51Testing软件测试网"f3OSiPUV+Ml0N&t
gaY k'nu!Z4v0在project_home建一个jetty目录,子目录如:contexts、etc、lib。51Testing软件测试网 iS6| ?f)S
51Testing软件测试网GYp2FMi)bH把${jetty_home}/etc目录下的jetty.xml、webdefault.xml文件复制到${project_home}/jetty/etc目录中。51Testing软件测试网!`7U n/]3N`
k]rf4B0把${jetty_home}/lib/jsp-2.1目录复制到${project_home}/jetty/lib目录下(如果不复制jsp-2.1或jsp-2.0也可以正常启动,只是不能解析jsp,打开主页时提示 JSP not support)。
!jaV5YS3I051Testing软件测试网`5Bgi\(}&`同样把jetty-6.1.14.jar、jetty-util-6.1.14.jar、servlet-api-2.5-6.1.14.jar复制到${project_home}/jetty/lib目录下。
a`\"uj*wF_a051Testing软件测试网C*[.rpy+p把${jetty_home}/start.jar复制到${project_home}/jetty目录下。51Testing软件测试网R4iW`V!]]#t,s
51Testing软件测试网L+d}cgGN b+W+O接下来在${project_home}/jetty/contexts目录下加一个文件。可以是上面给出的web-demo.xml。不过war 可以改为
y3t$c6QO#pi8D5N0- <Set name="war"><SystemProperty name="jetty.home" default="."/>/../WebContent</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/../WebContent</Set>51Testing软件测试网h5GnG|+o4e*VrmM
注释${project_home}/jetty/etc/jetty.xml文件中的:
G `yNlVpS+T0- <!--
- <Item>
- <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler" />
- </Item>
- -->
- <!--
- <Call name="addLifeCycle">
- <Arg>
- <New class="org.mortbay.jetty.deployer.WebAppDeployer">
- <Set name="contexts"><Ref id="Contexts" /></Set>
- <Set name="webAppDir"><SystemProperty name="jetty.home" default="." />/webapps</Set>
- <Set name="parentLoaderPriority">false</Set>
- <Set name="extract">true</Set>
- <Set name="allowDuplicates">false</Set>
- <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>
- </New>
- </Arg>
- </Call>
- -->
- <!--
- <Item>
- <New class="org.mortbay.jetty.security.HashUserRealm">
- <Set name="name">Test Realm</Set>
- <Set name="config"><SystemProperty name="jetty.home" default="." />/etc/realm.properties</Set>
- <Set name="refreshInterval">0</Set>
- </New>
- </Item>
- -->
- <!--
- <Ref id="RequestLog">
- <Set name="requestLog">
- <New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
- <Set name="filename"><SystemProperty name="jetty.logs" default="./logs" />/yyyy_mm_dd.request.log</Set>
- <Set name="filenameDateFormat">yyyy_MM_dd</Set>
- <Set name="retainDays">90</Set>
- <Set name="append">true</Set>
- <Set name="extended">false</Set>
- <Set name="logCookies">false</Set>
- <Set name="LogTimeZone">GMT</Set>
- </New>
- </Set>
- </Ref>
- -->
<!-- <Item> <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler" /> </Item> --> <!-- <Call name="addLifeCycle"> <Arg> <New class="org.mortbay.jetty.deployer.WebAppDeployer"> <Set name="contexts"><Ref id="Contexts" /></Set> <Set name="webAppDir"><SystemProperty name="jetty.home" default="." />/webapps</Set> <Set name="parentLoaderPriority">false</Set> <Set name="extract">true</Set> <Set name="allowDuplicates">false</Set> <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set> </New> </Arg> </Call> --> <!-- <Item> <New class="org.mortbay.jetty.security.HashUserRealm"> <Set name="name">Test Realm</Set> <Set name="config"><SystemProperty name="jetty.home" default="." />/etc/realm.properties</Set> <Set name="refreshInterval">0</Set> </New> </Item> --> <!-- <Ref id="RequestLog"> <Set name="requestLog"> <New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog"> <Set name="filename"><SystemProperty name="jetty.logs" default="./logs" />/yyyy_mm_dd.request.log</Set> <Set name="filenameDateFormat">yyyy_MM_dd</Set> <Set name="retainDays">90</Set> <Set name="append">true</Set> <Set name="extended">false</Set> <Set name="logCookies">false</Set> <Set name="LogTimeZone">GMT</Set> </New> </Set> </Ref> -->
Sh!N]*^g4Y7`N0RequestLog 是访问记录,不注释也可,前提就是有logs目录。
di%W C5Y O051Testing软件测试网!W lT5PT;ZMQ,Z*]3PaddLifeCycle 不注释也行,前提是有webapps目录。51Testing软件测试网o T%Y~ka
51Testing软件测试网a8cJ4inY,GTest Realm 不注释也是,前提是etc目录下有realm.properties。
5_LZ2MEMY051Testing软件测试网KT@(yJ}_配置好了,现在到E:\workspace\web-demo\jetty,运行java -jar start.jar 可以启动这个项目了。51Testing软件测试网r5Yu,W*P9[@7T/V
TAG: jetty
生活在互联网领域,从事WEB测试工作.
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | |||||||
4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
11 | 12 | 13 | 14 | 15 | 16 | 17 | |||
18 | 19 | 20 | 21 | 22 | 23 | 24 | |||
25 | 26 | 27 | 28 | 29 |
我的存档
数据统计
- 访问量: 1685828
- 日志数: 556
- 图片数: 1
- 文件数: 3
- 书签数: 8
- 建立时间: 2007-06-08
- 更新时间: 2023-01-07