jetty作为一个web容器,部署时只需要将部署包扔到 $JETTY_HOME/webapp目录下即可!
例如工程 test.war ,在启动jetty后的访问路径就是 http://localhost:8080/test ,若是我们期望访问 http://localhost:8080/ 即是访问test工程的呢?有两种办法可以解决。
1.直接将 test.war 改名为root.war 。
这个是最简单的做法,也是一般web容器都通用的做法,放到tomcat也是这种做法。
2.修改contexts目录下文件。
这个做法稍微有点麻烦,是通过设置 contexts来达到目的,不期望改变原来的配置文件,所以我们copy配置文件出来。
step1 准备工作
mkdir -p /home/inter12/jetty/webapp/virtual mkdir -p /home/inter12/jetty/config mkdir -p /home/inter12/jetty/config/contexts cp $JETTY_HOME/etc/jetty.xml /home/inter12/jetty/config/
step2 修改配置
vim /home/inter12/jetty/config/jetty.xml
修改其中的
.... <Call name="addLifeCycle"> <Arg> <New class="org.mortbay.jetty.deployer.ContextDeployer"> <Set name="contexts"><Ref id="Contexts"/></Set> <!----------------- 将contexts路径设置为我们新的 --------------------> <Set name="configurationDir">/home/inter12/jetty/config/contexts</Set> <Set name="scanInterval">5</Set> </New> </Arg> </Call> ....... <Call name="addLifeCycle"> <Arg> <New class="org.mortbay.jetty.deployer.WebAppDeployer"> <Set name="contexts"><Ref id="Contexts"/></Set> <!--------------- 设置webapp 目录 指定到一个虚拟的webapp 。---------------> <Set name="webAppDir">/home/inter12/jetty/webapp/virtual</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> ......
cd /home/inter12/jetty/config/contexts
新建一个 test.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">/</Set> <!-- 这里必须加上war这个结尾 - - - - - - - - - - - - - - - - - - - - --> <Set name="war">/home/inter12/jetty/webapp/test.war</Set> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <!-- Optional context configuration --> <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> <Set name="extractWAR">true</Set> <Set name="copyWebDir">false</Set> </Configure>
好了,到这里就已经基本结束了。
启动jetty
cd $JETTY_HOME
java -jar start.jar /home/inter12/jetty/config/jetty.xml
那么访问 http://localhost:8080/test 等同于 http://localhost:8080
TIPS:
若是通过contexts来配置 / 访问的话,那么war就不要放在webapp目录下,jetty会加载两边,若是配置了log4j的话,就会出现一下错误,切记!!!
Choose unique values for the 'webAppRootKey' context-param in your web.xml files!