Jetty快速入門與嵌入使用

原文出處:http://blog.chenlb.com/2009/01/quick-start-jetty-and-embed-in-project.htmljavascript

看到開源項目發佈的時候都帶一個 jsp 容器(jetty)。拿來作 demo、開發、調試的服務器仍是很不錯的。今天就小試下,主要把它運行起來。html

第一步下載: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是熱部署用的。java

試運行下,能夠把一個簡單的web項目到到webapps目錄下,或是*.war,如:web-demo(或web-demo.war)放到webapps目錄下。web

啓動 寫道
E:\jetty-6.1.14>java -jar start.jar
2009-01-13 15:34:38.937::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2009-01-13 15:34:39.265::INFO: jetty-6.1.14
2009-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/}
...
2009-01-13 15:35:01.828::INFO: Opened E:\jetty-6.1.14\logs\2009_01_13.request.log
2009-01-13 15:35:01.953::INFO: Started SelectChannelConnector@127.0.0.1:8080
 打開:http://localhost:8080/web-demo ,恩有結果了。

若是不把web-demo放到webapps目錄下也可,在contexts目錄下創建一個文件告訴jetty就好了。能夠在contexts目錄下複製test.xml爲web-demo.xml,而後修改以下:api

Xml代碼    收藏代碼
  1. <?xml version="1.0" encoding="ISO-8859-1"?>    
  2. <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">    
  3.     
  4. <Configure class="org.mortbay.jetty.webapp.WebAppContext">    
  5.     
  6.     <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->    
  7.     <!-- Required minimal context configuration : -->    
  8.     <!-- + contextPath -->    
  9.     <!-- + war OR resourceBase -->    
  10.     <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->    
  11.     <Set name="contextPath">/web-demo</Set>    
  12.     <Set name="war">e:/workspace/web-demo/WebContent</Set>    
  13.     
  14.     <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->    
  15.     <!-- Optional context configuration -->    
  16.     <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->    
  17.     <Set name="extractWAR">false</Set>    
  18.     <Set name="copyWebDir">false</Set>    
  19.     <Set name="defaultsDescriptor">    
  20.         <SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>    
  21.     <!--    
  22.         <Set name="overrideDescriptor"><SystemProperty name="jetty.home"    
  23.         default="."/>/contexts/test.d/override-web.xml</Set>    
  24.     -->    
  25. </Configure>    

 war能夠設置成絕對路徑。而後再重啓jetty試試看。好了, 對jetty的初步認識到這裏。tomcat

下面再來看下,怎麼把它放到項目中。現我有一示例項目 e:/workspace/web-demo(稱爲project_home),裏面的Web根目錄是WebContent。服務器

在project_home建一個jetty目錄,子目錄如:contexts、etc、lib。app

把${jetty_home}/etc目錄下的jetty.xml、webdefault.xml文件複製到${project_home}/jetty/etc目錄中。webapp

把${jetty_home}/lib/jsp-2.1目錄複製到${project_home}/jetty/lib目錄下(若是不復制jsp-2.1或jsp-2.0也能夠正常啓動,只是不能解析jsp,打開主頁時提示 JSP not support)。jsp

一樣把jetty-6.1.14.jar、jetty-util-6.1.14.jar、servlet-api-2.5-6.1.14.jar複製到${project_home}/jetty/lib目錄下。

把${jetty_home}/start.jar複製到${project_home}/jetty目錄下。

接下來在${project_home}/jetty/contexts目錄下加一個文件。能夠是上面給出的web-demo.xml。不過war 能夠改成

Xml代碼    收藏代碼
  1. <Set name="war"><SystemProperty name="jetty.home" default="."/>/../WebContent</Set>    

 註釋${project_home}/jetty/etc/jetty.xml文件中的:

Xml代碼    收藏代碼
  1. <!--    
  2. <Item>    
  3.     <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler" />    
  4. </Item>    
  5. -->    
  6. <!--    
  7. <Call name="addLifeCycle">    
  8.     <Arg>    
  9.         <New class="org.mortbay.jetty.deployer.WebAppDeployer">    
  10.             <Set name="contexts"><Ref id="Contexts" /></Set>    
  11.             <Set name="webAppDir"><SystemProperty name="jetty.home" default="." />/webapps</Set>    
  12.             <Set name="parentLoaderPriority">false</Set>    
  13.             <Set name="extract">true</Set>    
  14.             <Set name="allowDuplicates">false</Set>    
  15.             <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>    
  16.         </New>    
  17.     </Arg>    
  18. </Call>    
  19. -->    
  20. <!--    
  21. <Item>    
  22.     <New class="org.mortbay.jetty.security.HashUserRealm">    
  23.         <Set name="name">Test Realm</Set>    
  24.         <Set name="config"><SystemProperty name="jetty.home" default="." />/etc/realm.properties</Set>    
  25.         <Set name="refreshInterval"></Set>    
  26.     </New>    
  27. </Item>    
  28. -->    
  29. <!--    
  30. <Ref id="RequestLog">    
  31.     <Set name="requestLog">    
  32.         <New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">    
  33.             <Set name="filename"><SystemProperty name="jetty.logs" default="./logs" />/yyyy_mm_dd.request.log</Set>    
  34.             <Set name="filenameDateFormat">yyyy_MM_dd</Set>    
  35.             <Set name="retainDays">90</Set>    
  36.             <Set name="append">true</Set>    
  37.             <Set name="extended">false</Set>    
  38.             <Set name="logCookies">false</Set>    
  39.             <Set name="LogTimeZone">GMT</Set>    
  40.         </New>    
  41.     </Set>    
  42. </Ref>    
  43. -->    
 

RequestLog 是訪問記錄,不註釋也可,前提就是有logs目錄。

addLifeCycle 不註釋也行,前提是有webapps目錄。

Test Realm 不註釋也是,前提是etc目錄下有realm.properties。

配置好了,如今到E:\workspace\web-demo\jetty,運行java -jar start.jar 能夠啓動這個項目了。

相關文章
相關標籤/搜索