一、jetty服務器部署、啟動成功后,在瀏覽器輸入http://localhost:8080/ 可以直接訪問到jetty歡迎首頁。
這是因為在Jetty包中默認帶了一個test.war的應用,在${JETTY_HOME}/webapps目錄下可以找到這個文件,在啟動Jetty服務的時候默認已經部署了test.war應用。
對於test.war文件,Jetty還定義了context文件,放在${JETTY_HOME}/contexts/test.xml。
如${JETTY_HOME}/contexts/test.xml:
<Set name="contextPath">/</Set> <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
其中將contextPath定義成了“/”,這就是為什么默認訪問http://localhost:8080/的時候為什么是訪問test應用的原因了。
二、如果需要把http://localhost:8080/ 地址指向修改到指定的項目,比如自己開發的icsp-webapp.war項目,可以如下操作:
1、可以通過復制${JETTY_HOME}/contexts/test.xml文件,重命名為{項目名.xml},例如icsp-webapp.xml。
2、把復制好的xml文件里的注釋、可選配置都刪掉,例如Optional context configuration,為可選內容配置。
3、修改復制好的xml文件里的相關路徑,修改好后如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/</Set> <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/icsp-webapp.war</Set> </Configure>
這樣就把http://localhost:8080/ 地址指向修改到指定的icsp-webapp.war項目。
然后還要把之前的test.xml里的地址指向修改為/test,如:
<Set name="contextPath">/test</Set> <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test.war</Set>
這樣就把http://localhost:8080/test 地址指向修改到之前默認的test.war項目。
這些步驟完成了,就可以在瀏覽器輸入http://localhost:8080/ 進行測試了。
三、端口設置
可以有三種方式:
1、寫個批處理文件來啟動jetty,在批處理文件中,對jetty進行端口設置。
title webapp d: cd D:\ProgramFiles\jetty\test\jetty-8.1.16-webapp java -version java -jar start.jar jetty.port=5354
2、找到jetty安裝目錄,修改${JETTY_HOME}/etc/jetty.xml 文件。
[port]字段[default]值為[指定的端口號]
<Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <Set name="host"><Property name="jetty.host" /></Set> <Set name="port"><Property name="jetty.port" default="8080"/></Set> <Set name="maxIdleTime">300000</Set> <Set name="Acceptors">2</Set> <Set name="statsOn">false</Set> <Set name="confidentialPort">8443</Set> <Set name="lowResourcesConnections">20000</Set> <Set name="lowResourcesMaxIdleTime">5000</Set> </New> </Arg> </Call>
3、在開發環境java代碼設置
package cn.tisson.icsp.test; import cn.tisson.logicware.core.util.JettyServer; /** * 生產者服務運行 * 使用Jetty運行調試Web應用, 在Console輸入r快速重新加載應用. * @author skyice * */ public class QuickStartServer { public static final int PORT = 8082; public static final String CONTEXT = "/icsp-server"; public static final String BASE_URL = "http://localhost:" + PORT + CONTEXT; public static final String[] TLD_JAR_NAMES = new String[] {"spring-webmvc", "shiro-web", "springside-core" }; // 添加組件路徑 private static final String ICSP_PORTAL_CLASSES = "../icsp-portal/target/classes"; public static void main(String[] args) throws Exception { // 設定Spring的profile System.setProperty("spring.profiles.active", "production"); JettyServer jettyServer = new JettyServer(PORT, CONTEXT); jettyServer.setTldJarNames(TLD_JAR_NAMES); jettyServer.addOtherClasses(ICSP_PORTAL_CLASSES); // 啟動Jetty try { jettyServer.start(); System.out.println("啟動成功,請使用該路徑訪問系統:" + BASE_URL); System.out.println("在控制台輸入'r'重新加載應用,輸入'q'退出jetty程序!"); while (true) { char c = (char) System.in.read(); if (c == 'r') { jettyServer.reloadContext(); } else if (c == 'q') { break; } } } catch (Exception e) { e.printStackTrace(); } finally { System.exit(-1); } } }