java教程

java用Cookie记录商品浏览记录

位置:首页 > java教程 > java技巧,2013-07-10
java用Cookie记录商品浏览记录(指定记录个数并按浏览时间排序),同时支持中文,防止中文乱码Cookiebean.javapackagewww.ablanxue.com;publicclassC...

java用Cookie记录商品浏览记录(指定记录个数并按浏览时间排序),同时支持中文,防止中文乱码

Cookiebean.java

package www.ablanxue.com;
public class Cookiebean {
private String idurlname;//cookie的名称,组成的格式为:商品id+商品图片地址+商品名称
private Integer xuhao;//用来记录浏览排序


public String getIdurlname() {
return idurlname;
}
public void setIdurlname(String idurlname) {
this.idurlname = idurlname;
}
public Integer getXuhao() {
return xuhao;
}
public void setXuhao(Integer xuhao) {
this.xuhao = xuhao;
}
}

工具类

Cook.java

package www.ablanxue.com;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;


public class Cook {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HttpServletRequest request = ServletActionContext.getRequest();
Cook c = new Cook();
// c.writecookie(。。。。。);写上参数哦


}

//id是商品id,url是商品图片地址,name是商品名称,num是要记录的个数
public static void writecookie(Integer id, String url, String name,
HttpServletRequest request, HttpServletResponse response,
Integer num) throws UnsupportedEncodingException {
if (ishave(id, request) == true) {


ArrayList<Cookiebean> arl = checkcookie(request);
Integer abc = 0;
if (arl != null) {
for (Cookiebean at : arl) {
if (at != null) {
abc = at.getXuhao() + 1;


if (abc > num) {


Cookie ck = new Cookie("myproductid" + abc, at
.getIdurlname());
ck.setMaxAge(0);
ck.setPath("/");
response.addCookie(ck);
}
if (abc <= num) {
Cookie ck = new Cookie("myproductid" + abc, at
.getIdurlname());
ck.setMaxAge(3600);
ck.setPath("/");
response.addCookie(ck);
}
}


}
}

// 序号,编号+"lai"+url+”huan“+name, lai和huan作为分隔符,其它字母也行
Cookie ckk = new Cookie("myproductid1", URLEncoder.encode(id
.toString()
+ "lai" + url + "huan" + name, "UTF-8"));
ckk.setMaxAge(3600);
ckk.setPath("/");
response.addCookie(ckk);
}


}


public static boolean ishave(Integer id, HttpServletRequest request) {
boolean ha = true;
Integer cb = null;
String cc = null;
Cookie[] cookie = request.getCookies();
if (cookie != null && cookie.length > 0) {
for (Cookie co : cookie) {
if (co.getName().indexOf("myproductid") == 0) {
// 序号,编号+"lai"+url+”huan“+name, lai和huan作为分隔符,其它字母也行


cc = URLDecoder.decode(co.getValue());
cb = Integer.parseInt(cc.substring(0, cc.indexOf("lai")));
if (cb.equals(id)) {
ha = false;
break;
}


}
}


}
return ha;
}


public static ArrayList<Cookiebean> checkcookie(HttpServletRequest request) {
ArrayList<Cookiebean> al = null;
Integer caa = null;
String cbb = null;
al = new ArrayList<Cookiebean>();
Cookie[] cookie = request.getCookies();
if (cookie != null && cookie.length > 0) {
for (Cookie coo : cookie) {
if (coo.getName().indexOf("myproductid") == 0) {
// 序号,编号
caa = Integer.parseInt(coo.getName().substring(11,
coo.getName().length()));
cbb = coo.getValue();
Cookiebean ck = new Cookiebean();
ck.setIdurlname(cbb);
ck.setXuhao(caa);
al.add(ck);
}
}
}


return al;


}


}


jsp页面显示


cookie.jsp

<%@ page language="java" import="java.util.*,java.net.URLEncoder" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
   <%
String coname = "";
String courl = "";
String cooval = "";
Integer coid = null;
Integer cd=0;
Cookie[] cookie = request.getCookies();


if (cookie != null && cookie.length > 0) {
for (Cookie coo : cookie) {
if (coo.getName().indexOf("myproductid") == 0) {
cd=1;
cooval =coo.getValue();
coid = Integer.parseInt(cooval.substring(0, cooval
.indexOf("lai")));
coname = cooval.substring(cooval.indexOf("huan") + 4,
cooval.length());
courl = cooval.substring(cooval.indexOf("lai") + 3,
cooval.indexOf("huan"));
%>
<br/>
<a href="/getpr-<%=coid%>.html"><img src="/productsmallfile/<%=courl%>" title="<%=coname%>" /></a><br/>

<a href="/getpr-<%=coid%>.html" title="<%=coname%>"><%=coname%></a><br/> <br/>
<%




}
}


}
%>


<% if(cd==0){
%>
没有浏览记录
<%
}

 %>

原创内容,转载请明出处




TAGS:java用Cookie记录商品浏览记录

猜你喜欢

NewHot