BaiFan
文章目录
  1. 1. 初始化参数表格
  2. 2. 使用初始化参数
  3. 3. 使用Servlet 3.0 Session配置
    1. 3.1. SessionCookieConfiguration
    2. 3.2. SessionTrackingModes

自定义Jetty的JSession的配置

初始化参数表格

Context参数名称 默认值 描述
org.eclipse.jetty.servlet.SessionCookie JSESSIONID 会话cookie的名称默认是JSESSIONID,可以通过这个Context参数为特定的web应用设置名称
org.eclipse.jetty.servlet.SessionIdPathParameterName jsessionid 会话URL的参数名称。默认是jsessionid,但可以通过这个Context参数为特定的web应用设置名称。如果值设置为”none”则禁用URL重写
org.eclipse.jetty.servlet.SessionDomain - 会话域。如果ServletContext配置了该参数,就会使用该值作为会话cookie的域。如果没有设置,会话cookie就不会指定域。
org.eclipse.jetty.servlet.SessionPath - 会话路径。 如果ServletContext配置了该参数,就会使用该值作为会话cookie的域。如果没有设置,会话cookie路径就会使用ContextPath。
org.eclipse.jetty.servlet.MaxAge -1 Session的最长有效时间,单位秒.如果ServletContext配置了该参数,就会使用该值作为会话cookie的最大存活时间。如果没有设置,会话cookie的最长存活时间就会使用’-1’作为参数
org.eclipse.jetty.servlet.CheckingRemoteSessionIdEncoding false 默认值是’false’,如果设置成’true’,Jetty会在调用encodeURL()方法时加入JSESSIONID参数,即使是外部url

如果maxAge为负数,则表示该Cookie仅在本浏览器窗口以及本窗口打开的子窗口内有效,关闭窗口后该Cookie即失效。
maxAge为负数的Cookie,为临时性Cookie,不会被持久化,不会被写到Cookie文件中。
Cookie信息保存在浏览器内存中,因此关闭浏览器该Cookie就消失了。Cookie默认的maxAge值为-1。

使用初始化参数

下面提供了几个示例展示了如何使用初始化参数。

Context参数示例
可以在Web应用的WEB-INF/web.xml文件中指定这些参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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">
...
<context-param>
<param-name>org.eclipse.jetty.servlet.SessionCookie</param-name>
<param-value>XSESSIONID</param-value>
</context-param>
<context-param>
<param-name>org.eclipse.jetty.servlet.SessionIdPathParameterName</param-name>
<param-value>xsessionid</param-value>
</context-param>
...
</web-app>

Jetty应用参数设置
可以在Jetty容器的context xml配置文件或者代码中配置这些参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/test</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</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示例
可以在SessionManager实例或者代码中配置这些参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/test</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</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>

使用Servlet 3.0 Session配置

随着Servlet规范3.0的出现,有新的API来配置Session处理这些特性。
之前只能通过Jetty指定初始化参数来实现,现在可以使用容器无感知(container-agnostic)的方式通过代码或者web.xml实现。

SessionCookieConfiguration

javax.servlet.SessionCookieConfig类用于设定session处理这些特性。想要了解详细信息,请参考Javadoc文档
下面是一个示例展示:一个ServletContextListener检索到SessionCookieConfig,并且在context初始化的时候设置新的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import javax.servlet.SessionCookieConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TestListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
String comment = "This is my special cookie configuration";
String domain = "foo.com";
String path = "/my/special/path";
boolean isSecure = true;
boolean httpOnly = false;
int maxAge = 30000;
String cookieName = "FOO_SESSION";
SessionCookieConfig scf = sce.getServletContext().getSessionCookieConfig();
scf.setComment(comment);
scf.setDomain(domain);
scf.setHttpOnly(httpOnly);
scf.setMaxAge(maxAge);
scf.setPath(path);
scf.setSecure(isSecure);
scf.setName(cookieName);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}

你也可以在web.xml中配置session处理这些特性:下面的示例和上面的代码做了完全相同的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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_3_0.xsd"
metadata-complete="true"
version="3.0">
<session-config>
<cookie-config>
<comment>This is my special cookie configuration</comment>
<domain>foo.com</domain>
<http-only>false</http-only>
<max-age>30000</max-age>
<path>/my/special/path</path>
<secure>true</secure>
<name>FOO_SESSION</name>
</cookie-config>
</session-config>
</web-app>

SessionTrackingModes

除了配置会话cookie,从Servlet3.0规范后,你也可以配置 javax.servlet.SessionTrackingMode跟踪session。

如果想要确定容器使用了哪些默认的session跟踪特性,调用以下代码:

1
javax.servlet.SessionContext.getDefaultSessionTrackingModes();

如果改变session的跟踪模式,调用以下代码:

1
javax.servlet.SessionContext.setSessionTrackingModes(Set<SessionTrackingMode>);

也可以在web.xml文件中配置跟中模式,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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_3_0.xsd"
metadata-complete="true"
version="3.0">
<session-config>
<tracking-mode>URL</tracking-mode>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>

原文参照 官方文档:Session管理

文章目录
  1. 1. 初始化参数表格
  2. 2. 使用初始化参数
  3. 3. 使用Servlet 3.0 Session配置
    1. 3.1. SessionCookieConfiguration
    2. 3.2. SessionTrackingModes