本文整理汇总了Java中javax.servlet.http.Cookie.setMaxAge方法的典型用法代码示例。如果您正苦于以下问题:Java Cookie.setMaxAge方法的具体用法?Java Cookie.setMaxAge怎么用?Java Cookie.setMaxAge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.http.Cookie
的用法示例。
在下文中一共展示了Cookie.setMaxAge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: set
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 保存
* @param response
* @param key
* @param value
* @param ifRemember
*/
public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
int age = COOKIE_MAX_AGE;
if (ifRemember) {
age = COOKIE_MAX_AGE;
} else {
age = -1;
}
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(age); // Cookie过期时间,单位/秒
cookie.setPath(COOKIE_PATH); // Cookie适用的路径
response.addCookie(cookie);
}
示例2: doSetCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 设置Cookie的值,并使其在指定时间内生效
*
* @param cookieMaxage cookie生效的最大秒数
*/
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
try {
if (cookieValue == null) {
cookieValue = "";
} else if (isEncode) {
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);
if (null != request) {// 设置域名的cookie
String domainName = getDomainName(request);
System.out.println(domainName);
if (!"localhost".equals(domainName)) {
cookie.setDomain(domainName);
}
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: removeAllCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 销毁所有cookie
*
* @param request
* @param response
*/
public static void removeAllCookie(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
cookie.setValue(null);
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
示例4: login
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* Logins the specified user from the specified request.
*
* <p>
* If no session of the specified request, do nothing.
* </p>
*
* @param request
* the specified request
* @param response
* the specified response
* @param user
* the specified user, for example,
*
* <pre>
* {
* "userEmail": "",
* "userPassword": ""
* }
* </pre>
*/
public static void login(final HttpServletRequest request, final HttpServletResponse response,
final JSONObject user) {
final HttpSession session = request.getSession();
if (null == session) {
logger.warn("The session is null");
return;
}
session.setAttribute(User.USER, user);
try {
final JSONObject cookieJSONObject = new JSONObject();
cookieJSONObject.put(User.USER_EMAIL, user.optString(User.USER_EMAIL));
cookieJSONObject.put(User.USER_PASSWORD, user.optString(User.USER_PASSWORD));
final Cookie cookie = new Cookie("b3log-latke", cookieJSONObject.toString());
cookie.setPath("/");
cookie.setMaxAge(COOKIE_EXPIRY);
response.addCookie(cookie);
} catch (final Exception e) {
logger.warn("Can not write cookie", e);
}
}
示例5: setSearchHistroy
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 设置search记录到cookie中,操作步骤:
* 检查加入的记录是否已经存在cookie中,如果存在,则更新列表次序;如果不存在,则插入到最前面
* @param context
* @param value
*/
private void setSearchHistroy(Map<String, Object> context, String value) {
//分析已有的cookie
String separatorsB = "\\.\\.\\.\\.\\.\\.";
String newCookiev = value;
Cookie[] cookies = request.getCookies();
for(Cookie c:cookies){
if(c.getName().equals("HISTORY")){
String cookiev = c.getValue();
String[] values = cookiev.split(separatorsB);
int count = 1;
for(String v : values){
if(count<=10){
if(!value.equals(v)){
newCookiev = newCookiev + separatorsB + v;
}
}
count ++;
}
break;
}
}
Cookie _cookie=new Cookie("HISTORY", newCookiev);
_cookie.setMaxAge(60*60*24*7); // 设置Cookie的存活时间为30分钟
_cookie.setPath("/");
response.addCookie(_cookie); // 写入客户端硬盘
}
示例6: setSearchHistroy
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 设置search记录到cookie中,操作步骤:
* 检查加入的记录是否已经存在cookie中,如果存在,则更新列表次序;如果不存在,则插入到最前面
*
* @param context
* @param value
*/
private void setSearchHistroy(Map<String, Object> context, String value) {
//分析已有的cookie
String separatorsB = "\\.\\.\\.\\.\\.\\.";
String newCookiev = value;
Cookie[] cookies = request.getCookies();
for (Cookie c : cookies) {
if (c.getName().equals("HISTORY")) {
String cookiev = c.getValue();
String[] values = cookiev.split(separatorsB);
int count = 1;
for (String v : values) {
if (count <= 10) {
if (!value.equals(v)) {
newCookiev = newCookiev + separatorsB + v;
}
}
count++;
}
break;
}
}
Cookie _cookie = new Cookie("HISTORY", newCookiev);
_cookie.setMaxAge(60 * 60 * 24 * 7); // 设置Cookie的存活时间为30分钟
_cookie.setPath("/");
response.addCookie(_cookie); // 写入客户端硬盘
}
示例7: makeCheckCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
private static Cookie makeCheckCookie(ApplicationId id, boolean isSet) {
Cookie c = new Cookie(getCheckCookieName(id),String.valueOf(isSet));
c.setPath(ProxyUriUtils.getPath(id));
c.setMaxAge(60 * 60 * 2); //2 hours in seconds
return c;
}
示例8: testCookieRetrieve
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
@Test
public void testCookieRetrieve() {
final MockHttpServletRequest request = new MockHttpServletRequest();
final Cookie cookie = new Cookie("test", "test");
cookie.setDomain("cas.org");
cookie.setMaxAge(5);
request.setCookies(new Cookie[] {cookie});
assertEquals("test", this.g.retrieveCookieValue(request));
}
示例9: writeCookieValue
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
public void writeCookieValue(CookieValue cookieValue) {
HttpServletRequest request = cookieValue.getRequest();
HttpServletResponse response = cookieValue.getResponse();
String requestedCookieValue = cookieValue.getCookieValue();
String actualCookieValue = this.jvmRoute == null ? requestedCookieValue
: requestedCookieValue + this.jvmRoute;
Cookie sessionCookie = new Cookie(this.cookieName, this.useBase64Encoding
? base64Encode(actualCookieValue) : actualCookieValue);
sessionCookie.setSecure(isSecureCookie(request));
sessionCookie.setPath(getCookiePath(request));
String domainName = getDomainName(request);
if (domainName != null) {
sessionCookie.setDomain(domainName);
}
if (this.useHttpOnlyCookie) {
// sessionCookie.setHttpOnly(true);
}
if ("".equals(requestedCookieValue)) {
sessionCookie.setMaxAge(0);
}
else if (this.rememberMeRequestAttribute != null
&& request.getAttribute(this.rememberMeRequestAttribute) != null) {
// the cookie is only written at time of session creation, so we rely on
// session expiration rather than cookie expiration if remember me is enabled
sessionCookie.setMaxAge(Integer.MAX_VALUE);
}
else {
sessionCookie.setMaxAge(this.cookieMaxAge);
}
response.addCookie(sessionCookie);
}
示例10: addCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* Adds the cookie, taking into account {@link RememberMeCredential#REQUEST_PARAMETER_REMEMBER_ME}
* in the request.
*
* @param request the request
* @param response the response
* @param cookieValue the cookie value
*/
public void addCookie(final HttpServletRequest request, final HttpServletResponse response, final String cookieValue) {
final String theCookieValue = this.casCookieValueManager.buildCookieValue(cookieValue, request);
if (!StringUtils.hasText(request.getParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME))) {
super.addCookie(response, theCookieValue);
} else {
final Cookie cookie = createCookie(theCookieValue);
cookie.setMaxAge(this.rememberMeMaxAge);
if (isCookieSecure()) {
cookie.setSecure(true);
}
response.addCookie(cookie);
}
}
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:23,代码来源:CookieRetrievingCookieGenerator.java
示例11: freedetail
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
@RequestMapping(value = "boarddetail.jy", method = { RequestMethod.POST, RequestMethod.GET })
public String freedetail(JYBoard board, HttpServletRequest request,
HttpServletResponse response, Model model) {
logger.info("Welcome BoardController boarddetail! ---------------------------------------");
Cookie cookies[]=request.getCookies();
Map<String, String> map=new HashMap<>();
if(cookies!=null) {
for (int i = 0; i < cookies.length; i++) {
Cookie tempCookie=cookies[i];
map.put(tempCookie.getName(), tempCookie.getValue());
logger.info("Welcome BoardController boarddetail! -----------------------------------name "+
tempCookie.getName()+" value "+tempCookie.getValue());
}
}
String readCount=map.get("readcount_"+board.getSeq());
String newReadCount="readcount_"+board.getSeq();
if(StringUtils.indexOfIgnoreCase(readCount, newReadCount)==-1) {
Cookie cookie=new Cookie("readcount_"+board.getSeq(),"readcount_"+board.getSeq());
cookie.setMaxAge(24*60*60);
response.addCookie(cookie);
boardService.updateReadcount(board);
}
JYBoardMap bm=new JYBoardMap(boardService.getBoard(board));
request.getSession().setAttribute("bestcategorylist", categoryService.getPopCatList());
request.getSession().setAttribute("categorylist", categoryService.getCatList());
model.addAttribute("boarddetail", bm);
logger.info("Welcome BoardController boarddetail! " + bm);
return "boarddetail.tiles";
}
示例12: addCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* Adds the cookie, taking into account {@link RememberMeCredential#REQUEST_PARAMETER_REMEMBER_ME}
* in the request.
*
* @param request the request
* @param response the response
* @param cookieValue the cookie value
*/
public void addCookie(final HttpServletRequest request, final HttpServletResponse response, final String cookieValue) {
final String theCookieValue = this.casCookieValueManager.buildCookieValue(cookieValue, request);
if (StringUtils.isBlank(request.getParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME))) {
super.addCookie(response, theCookieValue);
} else {
final Cookie cookie = createCookie(theCookieValue);
cookie.setMaxAge(this.rememberMeMaxAge);
cookie.setSecure(isCookieSecure());
cookie.setHttpOnly(isCookieHttpOnly());
response.addCookie(cookie);
}
}
示例13: addUserCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 添加用户信息到Cookie
*
* @param user
* 用户信息
* @return Cookie信息
*/
public static Cookie addUserCookie(TUser user) {
try {
Cookie cookie = new Cookie(USER_COOKIE, URLEncoder.encode(user.getLoginid(), YiDuConstants.ENCODING_UTF_8)
+ "," + user.getPassword() + "," + user.getType() + "," + StringUtils.isNotBlank(user.getOpenid()));
// 默认保持两年cookie保存两周
cookie.setMaxAge(60 * 60 * 24 * 365);
return cookie;
} catch (UnsupportedEncodingException e) {
logger.error(e);
}
return null;
}
示例14: setCookie
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* 设置 Cookie
* @param name 名称
* @param value 值
* @param maxAge 生存时间(单位秒)
*/
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
Cookie cookie = new Cookie(name, null);
cookie.setPath(path);
cookie.setMaxAge(maxAge);
try {
cookie.setValue(URLEncoder.encode(value, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addCookie(cookie);
}
示例15: removeRememberMeCookieAndDigest
import javax.servlet.http.Cookie; //导入方法依赖的package包/类
/**
* Removes "Rememer me" cookiet
*/
protected final void removeRememberMeCookieAndDigest() {
final Cookie cookie = new Cookie(WebUIConstants.COOKIE_REMEMBER_ME, "");
cookie.setPath("/");
cookie.setMaxAge(0);
getTierletContext().addCookie(cookie);
}