当前位置: 技术问答>java相关
关于cookie的一个问题???
来源: 互联网 发布时间:2015-07-20
本文导语: 我知道在jsp中如何向cookie添加数据,但是现在我需要从cookie中删除一些数据,不知有没有方法可以实现? | 把他的过期时间设成现在时间就可以了 | 给你一个Bean包治百病。 pa...
我知道在jsp中如何向cookie添加数据,但是现在我需要从cookie中删除一些数据,不知有没有方法可以实现?
|
把他的过期时间设成现在时间就可以了
|
给你一个Bean包治百病。
package cn.edu.ncepubj.util;
import java.beans.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* cookie管理
*
*/
public class CookieBean extends HttpServlet {
public CookieBean() {
}
/**
* Add a new cookie to Response with
* specified name and value.
* @param res HttpServletResponse cookie is added to.
* @param cookieName Name of new cookie.
* @param cookieValue Value of new cookie.
*/
public void addCookie(HttpServletResponse res, String cookieName, String cookieValue) {
Cookie c = new Cookie(cookieName, cookieValue);
res.addCookie(c);
}
/**
* Setter for property cookieMaxAge.
* @param req HttpServletRequest for retrieving cookie array.
* @param res HttpServletResponse for writing updated cookie.
* @param cookieName Name of cookie to update.
* @param cookieExpires New value of property cookieMaxAge.
*/
public void setCookieMaxAge(HttpServletRequest req, HttpServletResponse res, String cookieName, int cookieExpires) {
Cookie c[] = req.getCookies();
if (c != null) {
for (int i = 0; i
package cn.edu.ncepubj.util;
import java.beans.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* cookie管理
*
*/
public class CookieBean extends HttpServlet {
public CookieBean() {
}
/**
* Add a new cookie to Response with
* specified name and value.
* @param res HttpServletResponse cookie is added to.
* @param cookieName Name of new cookie.
* @param cookieValue Value of new cookie.
*/
public void addCookie(HttpServletResponse res, String cookieName, String cookieValue) {
Cookie c = new Cookie(cookieName, cookieValue);
res.addCookie(c);
}
/**
* Setter for property cookieMaxAge.
* @param req HttpServletRequest for retrieving cookie array.
* @param res HttpServletResponse for writing updated cookie.
* @param cookieName Name of cookie to update.
* @param cookieExpires New value of property cookieMaxAge.
*/
public void setCookieMaxAge(HttpServletRequest req, HttpServletResponse res, String cookieName, int cookieExpires) {
Cookie c[] = req.getCookies();
if (c != null) {
for (int i = 0; i