jetty快速入門與嵌入使用 jetty

jopen 12年前發布 | 7K 次閱讀 SME Server

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

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

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

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,然后修改如下:

<?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>

  

war可以設置成絕對路徑。然后再重啟jetty試試看。好了, 對jetty的初步認識到這里。

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

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

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

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

同樣把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 可以改為

<Set name="war"><SystemProperty name="jetty.home" default="."/>/../WebContent</Set>

  

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

<!--  
<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>  
-->  

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

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

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

配置好了,現在到E:\workspace\web-demo\jetty,運行java -jar start.jar 可以啟動這個項目了。

原文地址:http://blog.chenlb.com/2009/01/quick-start-jetty-and-embed-in-project.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!