一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Servlet中Cookie增、删、改、查的例子

时间:2016-11-18 编辑:简简单单 来源:一聚教程网

整理了一个在Servlet中对Cookie增删改查的工具类,首先要注意的是在服务器端是无法对Cookie做修改的,只能做到覆盖创建。

引用StackOverflow上James Sumners的 回答 :

Per section 3.3.4 of RFC 2965 , the user agent does not include the expiration information in the cookie header that is sent to the server. Therefore, there is no way to update an existing cookie’s value while retaining the expiration date that was initially set based solely on the information associated with the cookie.

So the answer to this question is: you can’t do that.

工具类如下:

packagecom.demo.util;

importjava.io.UnsupportedEncodingException;
importjava.net.URLDecoder;
importjava.net.URLEncoder;

importjavax.servlet.http.Cookie;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

publicclassCookieUtils{

/**
 * 新增Cookie
 * @paramresponse
 * @paramname
 * @paramvalue
 */
publicstaticvoidaddCookie(HttpServletResponse response, String name,String value){
try{
//特殊字符需要编码
 Cookie cookie = newCookie(name,URLEncoder.encode(value,"UTF-8"));
 cookie.setMaxAge(60*60*24*7);//- 单位为秒,7天有效
 cookie.setPath("/");//- 根路径
//JavaEE5兼容
try{
 cookie.setHttpOnly(true);//- 防XSS
 }catch(NoSuchMethodError e) {
 e.printStackTrace();
 }
 response.addCookie(cookie);
 } catch(UnsupportedEncodingException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

/**
 * 删除Cookie
 * @paramrequest
 * @paramresponse
 * @paramname
 * @return
 */
publicstaticbooleandeleteCookie(HttpServletRequest request,HttpServletResponse response, String name){
if(request.getCookies() !=null) {
for(Cookie cookie : request.getCookies()) {
if(cookie.getName().equals(name)) {
 cookie.setMaxAge(0);
//经测试发现还需设置如下两个值,之所以这样,原理为覆盖掉Cookie,而不是常规意义中的删除
 cookie.setValue("");
 cookie.setPath("/");
 response.addCookie(cookie);
returntrue;
 }
 }
 }

returnfalse;
 }

/**
 * 覆盖掉之前的cookie
 * @paramrequest
 * @paramname
 * @paramvalue
 */
publicstaticvoidoverrideCookie(HttpServletRequest request,HttpServletResponse response, String name,String value){
if(request.getCookies() !=null) {
for(Cookie cookie : request.getCookies()) {
if(cookie.getName().equals(name)) {
 cookie.setValue(value);
 cookie.setPath("/");
//cookie.getMaxAge()为-1,服务器端无法获取MaxAge
 cookie.setMaxAge(60*60*24*7);
 response.addCookie(cookie);
 }
 }
 }
 }

/**
 * 获取Cookie
 * @paramrequest
 * @paramname
 * @return
 */
publicstaticCookiegetCookie(HttpServletRequest request, String name){
if(request.getCookies() !=null) {
for(Cookie cookie : request.getCookies()) {
if(cookie.getName().equals(name)) {
returncookie;
 }
 }
 }

returnnull;
 }
/**
 * 获取Cookie对应的值
 * @paramrequest
 * @paramname
 * @return
 */
publicstaticStringgetCookieValue(HttpServletRequest request, String name){
if(request.getCookies() !=null) {
for(Cookie cookie : request.getCookies()) {
if(cookie.getName().equals(name)) {
try{
returnURLDecoder.decode(cookie.getValue(),"UTF-8");
 } catch(UnsupportedEncodingException e) {
// TODO Auto-generated catch block
 e.printStackTrace();
returnnull;
 }
 }
 }
 }
returnnull;
 }
}

热门栏目