启动Jetty
Jetty启动配置由全局的home/start.ini及多个home/start.d/*.ini文件控制,DOS模式下启动Jetty很简单,这里以一个空的应用为例(如下),其中--add-to-startd表示添加依赖模块,会在当前项目下创建webapps及start.d目录,并在start.d下生成模块对应的*.ini文件,如start.d/https.ini, start.d/http.ini等。命令及生成的目录如下
E:\jetty-distribution-9.2.24.v20180105>mkdir jettydemo
E:\jetty-distribution-9.2.24.v20180105>cd jettydemo
E:\jetty-distribution-9.2.24.v20180105\jettydemo>java -jar ../start.jar --add-to-startd=http,https,deploy,jsp
INFO: http initialised in ${jetty.base}\start.d\http.ini (created)
INFO: http enabled in ${jetty.base}\${jetty.base}\start.d\http.ini
INFO: server initialised transitively
...
E:\jetty-distribution-9.2.24.v20180105\jettydemo>java -jar ../start.jar jetty.port=9124 https.port=9125
...
2018-05-14 10:48:59.171:INFO:oejs.Server:main: Started @609ms
部署Web应用
部署的方式有以下几种,且都是执部署,不需要重启Jetty服务。
- 将WAR包/标准的web应用(必须包含
{dir}/WEB-INF/web.xml
文件)放到该webapps或home/webapps目录下。 - 定义符合Jetty XML语法的xml文件放到home/webapps目录下即可,Jetty的部署执行程序WebAppProvider(定义在etc/jetty-deploy.xml中)将扫描该目录并加载项目。一个简单的xml配置,只需要定义一个WebAppContext对象,并设置其ContextPath及war包路径即可,甚至文件名都可以随意,未设置ContextPath的情况下将设置为默认项目Root。关于更多配置可参考https://www.eclipse.org/jetty/documentation/9.2.22.v20170531/configuring-specific-webapp-deployment.html。如home/webapps/aaa.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/ft</Set> <Set name="war">D:/jetty-test.war</Set> </Configure>
支持Session
设置应用支持Session的方式主要有两种
- 一种是在web应用程序中的WEB-INF/web.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
...
<!-- 表示会话ID的名称,默认为JSESSIONID -->
<context-param>
<param-name>org.eclipse.jetty.servlet.SessionCookie</param-name>
<param-value>XSESSIONID</param-value>
</context-param>
<!-- 表示附加在URL参数上的会话名称 -->
<context-param>
<param-name>org.eclipse.jetty.servlet.SessionIdPathParameterName</param-name>
<param-value>xsessionid</param-value>
</context-param>
...
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
- 一种是在Jetty xml文件中添加Session相关配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/ft</Set>
<Set name="war">D:/jetty-test.war</Set>
<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.SessionCookie</Arg>
<Arg>XSESSIONID</Arg>
</Call>
<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.SessionIdPathParameterName</Arg>
<Arg>xsessionid</Arg>
</Call>
</Configure>
----或者晨SessionManager中进行配置
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/ft</Set>
<Set name="war">D:/jetty-test.war</Set>
...
<Get name="sessionHandler">
<Set name="sessionManager">
<New class="org.eclipse.jetty.server.session.HashSessionManager">
<Set name="sessionCookie">XSESSIONID</Set>
<Set name="sessionIdPathParameterName">xsessionid</Set>
</New>
</Set>
</Get>
</Configure>
关于Session的更多配置参考:https://www.eclipse.org/jetty/documentation/9.2.22.v20170531/session-management.html
嵌入式开发