Jetty和tomcat的比较

Jetty和tomcat的比较
 
相同点:
1.      Tomcat和Jetty都是一种Servlet引擎,他们都支持标准的servlet规范和JavaEE的规范。
 
 
不同点:
1.      架构比较
Jetty的架构比Tomcat的更为简单
Jetty的架构是基于Handler来实现的,主要的扩展功能都可以用Handler来实现,扩展简单。
Tomcat的架构是基于容器设计的,进行扩展是需要了解Tomcat的整体设计结构,不易扩展。
 
2.      性能比较
Jetty和Tomcat性能方面差异不大
Jetty可以同时处理大量连接而且可以长时间保持连接,适合于web聊天应用等等。
Jetty的架构简单,因此作为服务器,Jetty可以按需加载组件,减少不需要的组件,减少了服务器内存开销,从而提高服务器性能。
Jetty默认采用NIO结束在处理I/O请求上更占优势,在处理静态资源时,性能较高
 
Tomcat适合处理少数非常繁忙的链接,也就是说链接生命周期短的话,Tomcat的总体性能更高。
Tomcat默认采用BIO处理I/O请求,在处理静态资源时,性能较差。
 
3.      其它比较
Jetty的应用更加快速,修改简单,对新的Servlet规范的支持较好。
Tomcat目前应用比较广泛,对JavaEE和Servlet的支持更加全面,很多特性会直接集成进来。
 
 
**********************************************
mvn下多jetty运行 
 
jetty 使用时,如果出现 address already in use , 可以换个端口再启用。用下面的命令:
 
mvn -Djetty.port=9091 jetty:run
 
我们可以同时开几个端口来运行多个项目,不至于关提一个应用所有的应用都停掉。
 
****************************
maven 运行 war 到jetty 
 
1.修改项目pom.xml增加
[html] view plaincopyprint?
<plugin>  
     <groupId>org.mortbay.jetty</groupId>  
     <artifactId>jetty-maven-plugin</artifactId>  
     <version>8.1.5.v20120716</version>  
     <configuration>  
         <stopPort>9966</stopPort>  
         <stopKey>foo</stopKey>  
         <scanIntervalSeconds>10</scanIntervalSeconds>  
         <webApp>  
             <contextPath>/teff</contextPath>  
         </webApp>  
     </configuration>  
 </plugin>  
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/teff</contextPath>
</webApp>
</configuration>
</plugin>
2.执行mvn jetty:run-war -Djetty:port=9999,访问http://localhost:9999/teff/xxx
3.可以执行mvn jetty:stop停止jetty
 
*************************************
错误整理:No plugin found for prefix 'jetty' in the current project and in the plugin groups 
 
在maven进行jetty的调试中出现错误:
[plain] view plaincopyprint?
[ERROR] No plugin found for prefix 'jetty' in the current project and in the plu  
 gin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repo  
 sitories [local (C:\Documents and Settings\Administrator\.m2\repository), centra  
 l (http://repo.maven.apache.org/maven2)] -> [Help 1]  
 [ERROR]  
 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit  
[ERROR] No plugin found for prefix 'jetty' in the current project and in the plu
gin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repo
sitories [local (C:\Documents and Settings\Administrator\.m2\repository), centra
l (http://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
 
 
settings.xml没有配置插件应此需要
[sql] view plaincopyprint?
mvn org.mortbay.jetty:maven-jetty-plugin:run   
mvn org.mortbay.jetty:maven-jetty-plugin:run 
这样来运行。
如果需要使用jetty:run,那么必须在maven的setting.xml下配置
[plain] view plaincopyprint?
<pluginGroups>  
     <pluginGroup>org.mortbay.jetty</pluginGroup>  
   </pluginGroups>  
<pluginGroups>
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>
或者在对应项目的pom.xml中plugins的节点下添加配置
[sql] view plaincopyprint?
<plugin>  
                 <groupId>org.mortbay.jetty</groupId>  
                 <artifactId>jetty-maven-plugin</artifactId>  
                 <configuration>  
                     <webApp>  
                         <contextPath>/</contextPath>  
                     </webApp>  
                     <stopKey>webx</stopKey>  
                     <stopPort>9999</stopPort>  
                     <connectors>  
                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
                             <port>8081</port>  
                             <maxIdleTime>60000</maxIdleTime>  
                         </connector>  
                     </connectors>  
                     <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">  
                         <filename>target/access.log</filename>  
                         <retainDays>90</retainDays>  
                         <append>false</append>  
                         <extended>false</extended>  
                         <logTimeZone>GMT+8:00</logTimeZone>  
                     </requestLog>  
                     <systemProperties>  
                         <systemProperty>  
                             <name>productionMode</name>  
                             <value>${productionMode}</value>  
                         </systemProperty>  
                     </systemProperties>  
                 </configuration>  
             </plugin>  
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
    <webApp>
<contextPath>/</contextPath>
    </webApp>
    <stopKey>webx</stopKey>
    <stopPort>9999</stopPort>
    <connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
   <port>8081</port>
   <maxIdleTime>60000</maxIdleTime>
</connector>
    </connectors>
    <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
<filename>target/access.log</filename>
<retainDays>90</retainDays>
<append>false</append>
<extended>false</extended>
<logTimeZone>GMT+8:00</logTimeZone>
    </requestLog>
    <systemProperties>
<systemProperty>
   <name>productionMode</name>
   <value>${productionMode}</value>
</systemProperty>
    </systemProperties>
</configuration>
</plugin>
 
 
**********************************************
推荐使用run-jetty-run来debug你的war
使用过maven2的jetty plugin,貌似很好的支持热部署,用了后,恩,确实很好,调试起来非常的方便,不像平常用tomcat那样改下代码就看到控制台一堆异常信息。但是,它是对java类才容易实现热部署,而且与eclipse的auto build有很大关系。但是我们知道,像jsp,html那些文件,即使修改在eclipse中都没有自动编译的概念,都是运行时容器解析的东西,编译个头咩(我们用tomcat插件时它会自动复制到webapp中)。。。所以我用m2的jetty plugin时,一直因为修改jsp文件代码没法马上体现而感觉很烦,每次修改jsp后如果不想重启容器,不想让m2重新编译打包的话,就自己手动把jsp文件拷贝到target里面去。。但是,我们用ide的目的是什么?不是为了开发方便一些吗?如果连这些工作都要自己去做的话那还不如用记事本。
 刚才,试了一下久闻的jetty launcher (修正:run-jetty-run, jetty launcher早就不支持eclipse3.X了),居然和我的m2项目很完美地结合了,本来还以为要自己写maven脚本的工程肯定不行,必须再建另一个项目,想不到就这样OK了,实在太开心了,马上试着改了java代码和jsp代码,哭了,都OK了!哈哈。。。用run-jetty-run还免去每次都让m2重新编译和打包(jetty plugin自动让其打包的,你就算只用mvn jetty:run-exploded -Djetty.port=8080,mvn package都会自动执行,这样对效率无疑造成较大影响)。
 今天看到一则新闻,google放弃tomcat选择jetty,原因很多,这里不说,自己搜索去,让我跟随google用下jetty吧!!!困扰了我这么久,终于把铁柱磨成针了
原文地址:https://www.cnblogs.com/huapox/p/3516215.html