设为首页 收藏本站
查看: 799|回复: 0

[经验分享] jetty 快速入门

[复制链接]

尚未签到

发表于 2017-2-25 12:30:21 | 显示全部楼层 |阅读模式
Jetty 是一个开源的servlet容器, 它为基于Java的web内容,例如JSP和servlet提供运行环境。

Jetty以其高效、小巧、可嵌入式等优点深得人心, 让我一看就喜欢上了。尤其是启动速度Tomcat 6简直望尘莫及.

如果让一个人说出一种开源的servlet容器,可能他们会回答Apache Tomcat。但是,Tomcat并不是孤单的,我们还有Jetty。Jetty作为可选的servlet容器只是一个额外的功能,而它真正出名是因为它是作为一个可以嵌入到其他的Java代码中的servlet容器而设计的。这就是说,开发小组将Jetty作为一组Jar文件提供出来,因此你可以在你自己的代码中将servlet容器实例化成一个对象并且可以操纵这个容器对象。

可以提供给客户一个自己具有启动,停止以及管理功能的应用程序:Jetty对于普通的HTTP服务(没有servlet)只需要350k的内存,这使得可以将其用在智能设备中。你可以提供基于web的控制面板并且具有Java web应用的所有功能而不用担心那些独立的容器所带来的压力。

Jetty 的配置文件放在 etc 路径下,该路径下有如下几个配置文件:
jetty.xm1文件。
jetty-jmx.xm1文件。
jetty-plus.xm1文件。
webdefault.xm1文件。
其中 webdefault.xm1文件是 Web 应用的默认配置文件,与 Jetty 的配置没有太大的关系,该文件通常不需要修改。
另外的三个都是 Tomcat 的配置文件:jetty.xm1文件是默认的配置文件;jetty-jmx.xm1是启动 JMX 控制的配置文件; jetty-plus.xm1文件是在增加 Jetty 扩展功能的配置文件。
在启动 Jetty 时 输入如下命令:
java -jar startup.jar
默认使用jetty.xm1文件时启动Jetty,即与如下命令效果相同:
java -jar startup.jar etc/jetty.xml

关闭时在命令行中用: ctrl+c

Jetty 配置文件语法说明:


打开 Jetty 配置文件,该配置文件的根元素是Configure.
Jetty 是个嵌入式 Web 容器,因此它的服务对应一个 Server 实例,可以看到配置文件中有如下片段:
<1-配置了一个Jetty服务器进程一〉
<Configure id="Server" class="org.mortbay.jetty.Server">



另外还会看到有如下的配置元素:
Set: 相当于调用 se仅xx 方法。
Get: 相当于调用 getXxx 方法。
New: 创建某个类的实例。
Arg: 为方法或构造器传入参数。
Array: 设置一个数组。
Map: 允许生成一个新的HashMap.

Item: 设置一个Array或者Map条目元素的条目

Call: 调用某个方法。
<SystemProperty>: 这个元素允许JVM系统属性作为Set、Put或者Arg 元素的值。


Java对象通过一连串<New>, <Set>, <Put> 和 <Call>元素来配置。下面的例子让你有个初步的了解:
<Set name="Test">value</Set> == obj.setTest("value");
<Put name="Test">value</Put> == obj.put("Test","value");
<Call name="test"><Arg>value</Arg></Call> == obj.test("value");
<New class="com.acme.MyStuff"><Arg/></New> == new com.acme.MyStuff();


一个Call元素可以包含一系列的Arg元素,跟随一系列作用在新对象上的Set、Put或者Call元素。

<Call name="test">
<Arg>value1</Arg>
<Set name="Test">Value2</Set>
</Call>

等价于:

Object o2 = o1.test("value1");
o2.setTest("value2");




<SystemProperty name="Test" default="value"/>

等价于:

System.getProperty("Test","value");






<ref>,


这个元素引用以前产生的一个对象,一个Ref元素包含一系列作用在参考对象上的Set, Put 和 Call元素:


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

(1)
把应用程序或WAR包放于jetty的webapps下来发布:
试运行下,可以把一个简单的web项目放到webapps目录下,或是*.war,如:web-demo(或web-demo.war)放到webapps目录下。
运行: E:\jetty-6.1.14>java -jar start.jar   
当看到:
2009-01-13 15:35:01.953::INFO: Started SelectChannelConnector@127.0.0.1:8080   ,表示jetty启动完成.
打开:http://localhost:8080/web-demo , 就有结果了。

(2)
如果不把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" > < SystemProperty name ="jetty.home" default ="." /> /etc/webdefault.xml</ Set >      
    20. <!--   
    21. <Set name="overrideDescriptor"><SystemProperty name="jetty.home" default="."/>/contexts/test.d/override-web.xml</Set>   
    22. -->     
    23. </ Configure >     war可以设置成绝对路径。然后再重启jetty,也可得到同样的效果.

(3)在现成的项目中嵌入使用 jetty
现我有一示例项目 e:/workspace/web-demo(称为project_home),里面的Web根目录是WebContent。
1.
在project_home 建一个jetty目录,子目录如:contexts、etc、lib。
2.
把${jetty_home}/etc目录下的jetty.xml、 webdefault.xml文件复制到${project_home}/jetty/etc目录中;
3.
把${jetty_home}/lib/jsp- 2.1目录复制到${project_home}/jetty/lib目录下,
把jetty-6.1.14.jar、jetty-util-6.1.14.jar、servlet-api-2.5- 6.1.14.jar复制到${project_home}/jetty/lib目录下.
(如果不复制jsp-2.1或jsp-2.0也可以正常启动,只是不能解析 jsp,打开主页时提示 JSP not support)
4.
${jetty_home}/start.jar复制到$ {project_home}/jetty目录下。
5.
接下来在${project_home}/jetty/contexts目录下加一个文件。可以是上面给出的web-demo.xml。不过war 可以改为:
< Set   name = "war" > < SystemProperty   name = "jetty.home"   default = "." /> /../WebContent </ Set >
6.
注释${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 可以启动这个项目了。

(4) 使用Jetty作为嵌入式服务器
有两种方式: 一种是单独设置.另一种是结合配置文件(比较简单).
需要导入的JAR包有:
jetty-6.1.14.jar
jetty-util-6.1.14.jar
servlet-api-2.5-6.1.14.jar
jsp-2.1目录下的JAR包.
(4.1)
结合配置文件(比较简单):
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.xml.XmlConfiguration;
import org.xml.sax.SAXException;

public class JettyServer {

        public static void main(String[] args) {
                Server server = new Server(8080);
                server.setHandler(new DefaultHandler());
                XmlConfiguration configuration = null ;
                try {
                        configuration = new XmlConfiguration(
                                new FileInputStream("C:/jetty.xml" ));
                } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                } catch (SAXException e1) {
                        e1.printStackTrace();
                } catch (IOException e1) {
                        e1.printStackTrace();
                }
                try {         
                        configuration.configure(server);
                        server.start();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}配置文件为c盘中的Jetty.xml.
复制原始的Jetty.xml,进行修改:
RequestLog 是访问记录,不注释也可,前提就是在JettyServer.java的目录中,有logs目录。
addLifeCycle ,修改webapps目录的位置。
Test Realm 不注释也是,前提是etc目录下有realm.properties。
如下:
        < Call name ="addLifeCycle" >
            < Arg >
                < New class ="org.mortbay.jetty.deployer.WebAppDeployer" >
                    < Set name ="contexts" > < Ref id ="Contexts" /> </ Set >
                    < Set name ="webAppDir" > E:/HUANGWEIJUN/jetty-6.1.14/webapps</ Set >
     < Set name ="parentLoaderPriority" > false</ Set >
     < Set name ="extract" > true</ Set >
     < Set name ="allowDuplicates" > false</ Set >
                    < Set name ="defaultsDescriptor" >
        E:/HUANGWEIJUN/jetty-6.1.14/etc/webdefault.xml</ Set >
                </ New >
            </ Arg >
        </ Call >



        < Set name ="UserRealms" >
            < Array type ="org.mortbay.jetty.security.UserRealm" >
<!--        
                <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>
-->
            </ Array >
        </ Set >


<!--
        <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>
-->
(4.2)
不用配置文件:
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.thread.BoundedThreadPool;

public class JettyServer2 {
    public static void main(String[] args) throws Exception {     
                     // Jetty HTTP Servlet Server.     
                     Server server = new Server();     
                     BoundedThreadPool threadPool = new BoundedThreadPool();     
                     // 设置线程池     
                     threadPool.setMaxThreads(100);     
                     server.setThreadPool(threadPool);     
                     // 设置连接参数     
                     Connector connector = new SelectChannelConnector();     
                     // 设置监听端口     
                     connector.setPort(8000);     
                     // 为服务设置连接器     
                     server.setConnectors(new Connector[] { connector });     
                     WebAppContext context = new WebAppContext();     
                     // 设置contextPath如:http://localhost:8000/test...   
                     context.setContextPath("/jason" );     
                     // 启动的war包的位置     
                     context.setWar("E:/HUANGWEIJUN/jetty-6.1.14/webapps/jason" );     
                     server.addHandler(context);     
                     server.setStopAtShutdown(true );     
                     server.setSendServerVersion(true );     
                     server.start();     
                     // 将服务添加到连接池中     
                     server.join();     
             }     
     }  
以上两种方式,当运行相关的JAVA类时,即可启动Jetty服务器,可以访问服务器中webapps中的内容.

(5)Jetty 的安全性
为了防止任何人都有权限去关闭一个已经开启的 Jetty 服务器, 我们可以通过在启动 Jetty 服务器的时候指定参数来进行控制,使得用户必须提供密码才能关闭 Jetty 服务器,启动 Jetty 服务器的命令如下所示:
java -DSTOP.PORT=8079 -DSTOP.KEY=mypassword -jar start.jar
这样,用户在停止 Jetty 服务器的时候,就必须提供密码“mypassword”。

-D<name>=<value>
               set a system property

(6)Jetty 通过JNDI配置数据源(暂略)


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sony315/archive/2011/05/13/6416796.aspx

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347069-1-1.html 上篇帖子: jetty eclipse plugin 下篇帖子: maven项目增加jetty支持
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表