當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServletResponse.addCookie方法代碼示例

本文整理匯總了Java中javax.servlet.http.HttpServletResponse.addCookie方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletResponse.addCookie方法的具體用法?Java HttpServletResponse.addCookie怎麽用?Java HttpServletResponse.addCookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.servlet.http.HttpServletResponse的用法示例。


在下文中一共展示了HttpServletResponse.addCookie方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doFilterInternal

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    // Spring put the CSRF token in session attribute "_csrf"
    CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf");

    // Send the cookie only if the token has changed
    String actualToken = request.getHeader("X-CSRF-TOKEN");
    if (actualToken == null || !actualToken.equals(csrfToken.getToken())) {
        // Session cookie that will be used by AngularJS
        String pCookieName = "CSRF-TOKEN";
        Cookie cookie = new Cookie(pCookieName, csrfToken.getToken());
        cookie.setMaxAge(-1);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    filterChain.doFilter(request, response);
}
 
開發者ID:GastonMauroDiaz,項目名稱:buenojo,代碼行數:19,代碼來源:CsrfCookieGeneratorFilter.java

示例2: logout

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 注銷
 *
 * @param session
 * @param response
 */
@RequestMapping("/logout")
public void logout(HttpSession session, HttpServletResponse response, HttpServletRequest request) {
    session.removeAttribute(WebConst.LOGIN_SESSION_KEY);
    Cookie cookie = new Cookie(WebConst.USER_IN_COOKIE, "");
    cookie.setValue(null);
    cookie.setMaxAge(0);// 立即銷毀cookie
    cookie.setPath("/");
    response.addCookie(cookie);
    try {
        response.sendRedirect("/admin/login");
    } catch (IOException e) {
        e.printStackTrace();
        LOGGER.error("注銷失敗", e);
    }
}
 
開發者ID:ZHENFENG13,項目名稱:My-Blog,代碼行數:22,代碼來源:AuthController.java

示例3: addCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的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);
        }
        if (isCookieHttpOnly()) {
            final Method setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly", boolean.class);
            if(setHttpOnlyMethod != null) {
                cookie.setHttpOnly(true);
            } else {
                logger.debug("Cookie cannot be marked as HttpOnly; container is not using servlet 3.0.");
            }
        }
        response.addCookie(cookie);
    }
}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:31,代碼來源:CookieRetrievingCookieGenerator.java

示例4: setCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 設置 Cookie
 *
 * @param name   名稱
 * @param value  值
 * @param maxAge 生存時間(單位秒)
 * @param path    路徑
 */
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);
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:20,代碼來源:CookieUtils.java

示例5: set

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 保存
 * 
 * @param response
 * @param key
 * @param value
 * @param ifRemember
 * true = age設置-1,不緩存;否則 age設置倆小時;
 */
public static void set(HttpServletResponse response, String key,
		String value, boolean ifRemember) {

	int age = Constant.COOKIE_MAX_AGE;
	if (ifRemember) {
		age = Constant.COOKIE_MAX_AGE;
	} else {
		age = -1;
	}

	Cookie cookie = new Cookie(key, value);
	cookie.setMaxAge(age); // Cookie過期時間,單位/秒
	cookie.setPath(Constant.COOKIE_PATH); // Cookie適用的路徑
	response.addCookie(cookie);
}
 
開發者ID:shiziqiu,項目名稱:shiziqiu-configuration,代碼行數:25,代碼來源:CookieUtil.java

示例6: checkforCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * Checks if specific cookies exist and save their value as message.
 * 
 * @param request
 * @param response
 */
protected void checkforCookie(HttpServletRequest request, HttpServletResponse response) {
	if (request.getCookies() != null) {
		for (Cookie cook : request.getCookies()) {
			if (cook.getName().equals(MESSAGECOOKIE)) {
				request.setAttribute("message", cook.getValue().replaceAll("_", " "));
				cook.setMaxAge(0);
				response.addCookie(cook);
			} else if (cook.getName().equals(PRODUCTCOOKIE)) {
				request.setAttribute("numberProducts", cook.getValue());
			} else if (cook.getName().equals(ERRORMESSAGECOOKIE)) {
				request.setAttribute("errormessage", cook.getValue().replaceAll("_", " "));
				cook.setMaxAge(0);
				response.addCookie(cook);
			}
		}
	}
}
 
開發者ID:DescartesResearch,項目名稱:Pet-Supply-Store,代碼行數:24,代碼來源:AbstractUIServlet.java

示例7: addCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 添加cookie
 * 
 * @param response
 * @param name
 *            Cookie的名稱,不能為null
 * @param value
 *            Cookie的值,默認值空字符串
 * @param maxAge
 * @param path
 *            默認值'/'
 */
public static void addCookie(HttpServletResponse response, String name,
		String value, Integer maxAge, String path) {
	if (value == null) {
		value = "";
	}
	if (path == null) {
		path = "/";
	}

	Cookie cookie = new Cookie(name, value);
	cookie.setPath(path);
	if (maxAge != null) {
		cookie.setMaxAge(maxAge);
	}

	response.addCookie(cookie);
}
 
開發者ID:wkeyuan,項目名稱:DWSurvey,代碼行數:30,代碼來源:CookieUtils.java

示例8: saveToken

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@Override
public void saveToken(CsrfTokenBean token, HttpServletRequest request,
		HttpServletResponse response) {
	String tokenValue = token == null ? "" : token.getToken();
	Cookie cookie = new Cookie(this.cookieName, tokenValue);
	cookie.setSecure(request.isSecure());
	if (this.cookiePath != null && !this.cookiePath.isEmpty()) {
			cookie.setPath(this.cookiePath);
	} else {
			cookie.setPath(this.getRequestContext(request));
	}
	if (token == null) {
		cookie.setMaxAge(0);
	}
	else {
		cookie.setMaxAge(-1);
	}
	if (cookieHttpOnly && setHttpOnlyMethod != null) {
		ReflectionUtils.invokeMethod(setHttpOnlyMethod, cookie, Boolean.TRUE);
	}

	response.addCookie(cookie);
}
 
開發者ID:TomChen001,項目名稱:xmanager,代碼行數:24,代碼來源:CookieCsrfTokenRepository.java

示例9: clearCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 清空Cookie操作 clearCookie
 * 
 * @param request
 * @param response
 * @return boolean
 * @author JIANG FEI Jun 19, 2014 10:12:17 AM
 */
public static boolean clearCookie(HttpServletRequest request,
		HttpServletResponse response, String name, String domain) {
	boolean bool = false;
	Cookie[] cookies = request.getCookies();
	if(null == cookies || cookies.length == 0) return bool;
	try {
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = new Cookie(name, null);
			cookie.setMaxAge(0);
			cookie.setPath("/");// 根據你創建cookie的路徑進行填寫
			cookie.setDomain(domain);
			response.addCookie(cookie);
			bool = true;
		}
	} catch (Exception ex) {
		logger.error("清空Cookies發生異常!", ex);
	}
	return bool;
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:28,代碼來源:CookieUtil.java

示例10: addCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
protected void addCookie(HttpServletResponse response, String key, String value, int expiry) {
    if (StringUtils.isBlank(key) || null == response) {
        return;
    }

    Cookie cookie = new Cookie(key, value);

    cookie.setPath("/");
    cookie.setMaxAge(expiry);

    response.addCookie(cookie);
}
 
開發者ID:HujiangTechnology,項目名稱:Juice,代碼行數:13,代碼來源:BaseController.java

示例11: index

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
@RequestMapping("/")
public String index( Model model, HttpServletResponse response) {
	
	Cookie cookie = new Cookie("jwtk", "011001110202423424214234");
	cookie.setHttpOnly(true);
	response.addCookie(cookie);
	
	List<JwtkMessage> msg = jwtkMessageRepository.findAllByOrderByDateDesc();
	model.addAttribute("msg", msg);
	
	return INDEX_PAGE;
}
 
開發者ID:yrzx404,項目名稱:interview-question-code,代碼行數:13,代碼來源:IndexContoller.java

示例12: setCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
/**
 * 設置 Cookie
 * @param name 名稱
 * @param value 值
 * @param maxAge 生存時間(單位秒)
 * @param uri 路徑
 */
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);
}
 
開發者ID:NeilRen,項目名稱:GeCMS,代碼行數:19,代碼來源:CookieUtils.java

示例13: userLogin

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
private void userLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Model model, String username, String headImgUrl) {
    httpServletResponse.setCharacterEncoding("utf-8");
    Cookie usercookie = new Cookie("uid", username);
    Cookie userheadimg = new Cookie("headimg", headImgUrl);
    usercookie.setPath("/");
    userheadimg.setPath("/");
    httpServletResponse.addCookie(usercookie);
    httpServletResponse.addCookie(userheadimg);
    HttpSession httpSession = httpServletRequest.getSession();
    httpSession.setAttribute("uid", username);
    httpSession.setAttribute("headimg", headImgUrl);
    model.addAttribute("uid", username);
    model.addAttribute("headimg", headImgUrl);
}
 
開發者ID:zxbangban,項目名稱:zxbangban,代碼行數:15,代碼來源:UserInfoController.java

示例14: addCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
public void addCookie(final HttpServletRequest request, final HttpServletResponse response, final String cookieValue) {

        if (!StringUtils.hasText(request.getParameter(RememberMeCredential.REQUEST_PARAMETER_REMEMBER_ME))) {
            super.addCookie(response, cookieValue);
        } else {
            final Cookie cookie = createCookie(cookieValue);
            cookie.setMaxAge(this.rememberMeMaxAge);
            if (isCookieSecure()) {
                cookie.setSecure(true);
            }
            response.addCookie(cookie);
        }
    }
 
開發者ID:luotuo,項目名稱:cas4.0.x-server-wechat,代碼行數:14,代碼來源:CookieRetrievingCookieGenerator.java

示例15: addSessionCookie

import javax.servlet.http.HttpServletResponse; //導入方法依賴的package包/類
private static void addSessionCookie(String token, HttpServletResponse response) {
    Cookie session = new Cookie("sid", token);
    if (domain != null) {
        session.setDomain(domain);
    }
    session.setPath("/");
    session.setHttpOnly(true);

    response.addCookie(session);
}
 
開發者ID:freedompy,項目名稱:commelina,代碼行數:11,代碼來源:AuthenticatedWebInterceptor.java


注:本文中的javax.servlet.http.HttpServletResponse.addCookie方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。