📄 cookieoperate.java
字号:
//Source file: D:\\tarena\\global\\CookieOperate.java
package tarena.global;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import tarena.dao.pages.ShopcartDAO;
import tarena.entity.Shopcart;
public class CookieOperate {
public static String GOODS_SEPERATOR="%7C";
public static String ATTRIBUTE_SEPERATOR="%2C";
/**
* @roseuid 490AA60300AB
*/
public CookieOperate() {
}
/**
* 查找cookie
* @param name - cookie名称
* @param cookies - 客户端cookie
* @return tarena.global.CookieOperate
* @roseuid 490AA61F0251
*/
public static Cookie FindCookie(String name, Cookie[] cookies) {
if(cookies!=null){
for(Cookie cookie:cookies){
if(cookie.getName().equals(name)){
return cookie;
}
}
}
return null;
}
/**
* 删除cookie
* @param cookie - 删除某个cookie
* @param response - response
* @roseuid 490AA6260232
*/
public static void DeleteCookie(Cookie cookie, HttpServletResponse response) {
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
/**
* @param cookie - cookie
* @param response - response
* @roseuid 490AA62C02EE
*/
public static void SaveCookie(Cookie cookie, HttpServletResponse response) {
//System.out.println("CookieOperator:saveCookie "+cookie.getValue());
cookie.setPath("/");
response.addCookie(cookie);
}
public static List<Shopcart> getGoodsFromCookie(String cookie){
List<Shopcart> goods = Collections.synchronizedList(new LinkedList<Shopcart>());
if(cookie==null||cookie.equals("")) return goods;
cookie = cookie.replaceAll(CookieOperate.GOODS_SEPERATOR, "|");
StringTokenizer st = new StringTokenizer(cookie,"|");
while(st.hasMoreTokens()){
String next = st.nextToken();
//System.out.println(next);
Shopcart cart = ShopcartDAO.getShopcartFromString(next);
if(cart!=null){
goods.add(cart);
}
}
return goods;
}
public static String toCookieString(List<Shopcart> goods){
StringBuffer cookie = new StringBuffer();
for(Shopcart cart:goods){
cookie.append(cart.toString());
cookie.append("|");
}
return cookie.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -