小結(jié)
介紹了如何在HttpServletRequest中對Cookie的進(jìn)行設(shè)置和獲取。
問題和解決
在服務(wù)器端的HttpServletRequest中對Cookie的進(jìn)行設(shè)置后,客戶端在接下來的請求中會(huì)攜帶此設(shè)置好的Cookie,所以可以在服務(wù)器端接收請求時(shí)提取這個(gè)Cookie的值。
服務(wù)器端設(shè)置Cookie值 :
String cookieName = "session-id-cookie";
String cookieValue = httpSession.getId();
Cookie SessionIDCookie = new Cookie(cookieName, cookieValue);
httpServletResponse.addCookie(SessionIDCookie);
服務(wù)器端提取Cookie值 :文章來源:http://www.zghlxwxcb.cn/news/detail-699336.html
Cookie[] cookies = httpServletRequest.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("session-id-cookie")) {
System.out.println("=====Cookie=====" + cookie.getValue().toString() + "=====Cookie=====");
logger.info("=====Cookie=====" + cookie.getValue().toString() + "=====Cookie=====");
}
}
參考
Stackoverflow: Get cookie value in java文章來源地址http://www.zghlxwxcb.cn/news/detail-699336.html
到了這里,關(guān)于有關(guān)使用HttpServletRequest的Cookie的設(shè)置和獲取的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!