博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jetty快速入门与嵌入使用 jetty
阅读量:6228 次
发布时间:2019-06-21

本文共 4300 字,大约阅读时间需要 14 分钟。

hot3.png

jetty快速入门与嵌入使用 jetty
发表于:
2009年1月13日 | 分类:  | 标签:  ,   |  views(41,589)

原文出处:

看到开源项目发布的时候都带一个 jsp 容器(jetty)。拿来做 demo、开发、调试的服务器还是很不错的。今天就小试下,主要把它运行起来。

第一步下载: 是目前最新的稳定版。解压到如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.jar2009-01-13 15:34:38.937::INFO: Logging to STDERR via org.mortbay.log.StdErrLog2009-01-13 15:34:39.265::INFO: jetty-6.1.142009-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.log2009-01-13 15:35:01.953::INFO: Started SelectChannelConnector :8080

打开:http://localhost:8080/web-demo ,恩有结果了。

如果不把web-demo放到webapps目录下也可,在contexts目录下建立一个文件告诉jetty就行了。可以在contexts目录下复制test.xml为web-demo.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的初步认识到这里。

下面再来看下,怎么把它放到项目中。现我有一示例项目 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 可以改为

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

注释${project_home}/jetty/etc/jetty.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">0</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 可以启动这个项目了。

转载于:https://my.oschina.net/u/590484/blog/81939

你可能感兴趣的文章
9-0-查找表-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
查看>>
thinkphp整合系列之短信验证码、订单通知
查看>>
fsimage 和 edits log
查看>>
遍历json对象---Java
查看>>
从头开始搭建一个Spring boot+RabbitMQ环境
查看>>
bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片...
查看>>
自己动手开发IOC容器
查看>>
hdparm
查看>>
[LeetCode] Best Time to Buy and Sell Stock
查看>>
《Flask Web开发——基于Python的Web应用开发实践》一字一句上机实践(上)
查看>>
C++11学习
查看>>
【java】java工具类StringUtils,org.apache.commons.lang3.StringUtils
查看>>
WPF太阳、地球、月球运动轨迹模拟
查看>>
Getting Started with Scala
查看>>
curl != casperjs ? - Google Groups
查看>>
Oracle从字符串资源中得到想要的数据分析
查看>>
敏捷开发中的Code Review
查看>>
事务 commit
查看>>
{右键我的电脑无法打开计算机管理}解决方法
查看>>
如何获取和发送Http请求和相应
查看>>