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

[经验分享] jetty学习总结

[复制链接]

尚未签到

发表于 2017-2-25 13:13:13 | 显示全部楼层 |阅读模式
  最近开始选择JETTY作为服务器了,乘这现在空闲点学习了些JETTY的部署知识,原来她真的跟TOMCAT很类似,先总结如下:
  部署应用方法(下载好jetty);
  方法一:
  直接将应用的 war包放在jetty_home/webapps下,启动jetty(命令:java -jar start.jar),默认jetty的端口是8080,在IE输入http://localhost:8080/jetty/jsp/login.jsp即可。
  方法二(动态部署):
  1.拷贝jetty_home/contexts/test.xml到相同目录,更改名称为jetty.xml,修改改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 and deploy the test web application in $(jetty.home)/webapps/test
  Note. If this file did not exist or used a context path other that /test
  then the default configuration of jetty.xml would discover the test
  webapplication with a WebAppDeployer.  By specifying a context in this
  directory, additional configuration may be specified and hot deployments 
  detected.
  ===================================================================== -->
  <Configure class="org.mortbay.jetty.webapp.WebAppContext">
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
    <Set name="contextPath">/jetty</Set>
    <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/jetty</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>-->
  <!-- virtual hosts
  <Set name="virtualHosts">
  <Array type="String">
  <Item>www.myVirtualDomain.com</Item>
  <Item>localhost</Item>
  <Item>127.0.0.1</Item>
  </Array>
  </Set>
  -->
  <!-- disable cookies 
  <Get name="sessionHandler">
  <Get name="sessionManager">
  <Set name="usingCookies" type="boolean">false</Set>
  </Get>
  </Get>
  -->
  <Get name="securityHandler">
  <Set name="userRealm">
  <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>
  <!-- To enable reload of realm when properties change, uncomment the following lines -->
  <!-- changing refreshInterval (in seconds) as desired                                -->
  <!-- 
  <Set name="refreshInterval">5</Set>
  <Call name="start"></Call>
  -->
  </New>
  </Set>
  <Set name="checkWelcomeFiles">true</Set>
  </Get>
  <!-- Non standard error page mapping -->
  <!--
  <Get name="errorHandler">
  <Call name="addErrorPage">
  <Arg type="int">500</Arg>
  <Arg type="int">599</Arg>
  <Arg type="String">/dump/errorCodeRangeMapping</Arg>
  </Call>
  </Get>
  -->
  </Configure>
2.将应用的war文件解压到jetty_home/webapps下(与配置文件jetty.xml中的<Set name="war">设置的路径匹配)
  
DSC0000.jpg
 之后启动jetty即可完成部署。
方法三(使用jetty插件,适用于单个工程的应用,且个文件放在maven默认路径):
在应用的pom.xml文件中添加jetty插件配置如下;
<build>
       <!-- jetty插件 -->
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
运行命令:mvn jetty:run 即可启动jetty服务,默认端口是8080,在IE输入http://localhost:8080/jetty/jsp/login.jsp即可。
停止服务可直接ctrl+c或者运行mvn jetty:stop即可。
开启服务后可以实现热部署功能,修改jsp或java代码均可。


或配置如下:
<plugin> 
<groupId>org.mortbay.jetty</groupId> 
<artifactId>maven-jetty-plugin</artifactId> 
<version>6.1.22</version> 
<configuration> 
<contextPath>/</contextPath> 
<connectors> 
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> 
<port>8081</port> 
<maxIdleTime>60000</maxIdleTime> 
</connector> 
</connectors> 
<requestLog implementation="org.mortbay.jetty.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>false</value> 
</systemProperty> 
</systemProperties> 
</configuration> 
</plugin> 
指定jetty端口为8081,且直接输入http://localhost:8081/jsp/login.jsp 即可





jetty知识:
如果启动脚本不在jetty目录下,则需执行如下命令启动jetty;
D:\>"D:/Program Files/Java/jdk1.6.0_20\bin\java" -jar d:/jetty-distribution-7.4.
2.v20110526/start.jar --ini=d:/jetty-distribution-7.4.2.v20110526/start.ini -Dje
tty.home=d:/jetty-distribution-7.4.2.v20110526



如果启动脚本在jetty目录下则只需:java -jar start.jar


-------------------------------------------------------------------------------------
利用macen的jetty插件调试工程:
在pom中添加jetty插件:
<build>

     <plugins>
<plugin>
       <groupId>org.mortbay.jetty</groupId>
       <artifactId>maven-jetty-plugin</artifactId>
       <version>6.1.9</version>
       <configuration>
    <contextPath>/</contextPath>
     <scanIntervalSeconds>3</scanIntervalSeconds>
     <scanTargetPatterns>
<scanTargetPattern>
       <directory>src/main/webapp</directory>
</scanTargetPattern>
      </scanTargetPatterns>
      </configuration>
              </plugin>
     </plugins>
  </build>

1、先来配置一个外部工具,来运行JETTY:
选择菜单Run->External Tools->External Tools Configurations ...在左边选择Program,再点New:
配置Location为mvn完整命令行。定位到bin下的mvn.bat
选择Working Directory为本项目。
Arguments填写:jetty:run


DSC0001.jpg
 
再点选Enviroment页:加入MAVEN_OPTS变量,值为:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8088,server=y,suspend=y
其中,如果suspend=n 表示不调试,直接运行。address=8080为端口
然后,点APPLY,再点run已启动jetty.


DSC0002.jpg
 
另外注意一点,好像external tool菜单项在java browering的perspective下才会出现。如果在java下看不见,可以切换下试试。
2、调试应用
右键点击工程,选debug as ->debug configurations...


DSC0003.bmp
 
选中左树中的Remote Java Application,再点New。
选择你的项目,关键是要填和之前设置外部工具时相同的端口号。


DSC0004.jpg
 点击apply,再点击degug即可。

运维网声明 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-347108-1-1.html 上篇帖子: Jetty实战之 嵌入式运行Jetty 下篇帖子: jetty修改端口方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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