java contextpath_Java Context.getEncodedPath方法代码示例

import org.apache.catalina.Context; //导入方法依赖的package包/类

/**

* Creates a new session cookie for the given session ID

*

* @param context The Context for the web application

* @param sessionId The ID of the session for which the cookie will be

* created

* @param secure Should session cookie be configured as secure

*/

public static Cookie createSessionCookie(Context context,

String sessionId, boolean secure) {

SessionCookieConfig scc =

context.getServletContext().getSessionCookieConfig();

// NOTE: The priority order for session cookie configuration is:

// 1. Context level configuration

// 2. Values from SessionCookieConfig

// 3. Defaults

Cookie cookie = new Cookie(

SessionConfig.getSessionCookieName(context), sessionId);

// Just apply the defaults.

cookie.setMaxAge(scc.getMaxAge());

cookie.setComment(scc.getComment());

if (context.getSessionCookieDomain() == null) {

// Avoid possible NPE

if (scc.getDomain() != null) {

cookie.setDomain(scc.getDomain());

}

} else {

cookie.setDomain(context.getSessionCookieDomain());

}

// Always set secure if the request is secure

if (scc.isSecure() || secure) {

cookie.setSecure(true);

}

// Always set httpOnly if the context is configured for that

if (scc.isHttpOnly() || context.getUseHttpOnly()) {

cookie.setHttpOnly(true);

}

String contextPath = context.getSessionCookiePath();

if (contextPath == null || contextPath.length() == 0) {

contextPath = scc.getPath();

}

if (contextPath == null || contextPath.length() == 0) {

contextPath = context.getEncodedPath();

}

if (context.getSessionCookiePathUsesTrailingSlash()) {

// Handle special case of ROOT context where cookies require a path of

// '/' but the servlet spec uses an empty string

// Also ensure the cookies for a context with a path of /foo don't get

// sent for requests with a path of /foobar

if (!contextPath.endsWith("/")) {

contextPath = contextPath + "/";

}

} else {

// Only handle special case of ROOT context where cookies require a

// path of '/' but the servlet spec uses an empty string

if (contextPath.length() == 0) {

contextPath = "/";

}

}

cookie.setPath(contextPath);

return cookie;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值