利用Cookie记录用户浏览的图片
2016-07-26 01:23
465 查看
一:当用户点击图片时,记录用户的浏览信息
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String imgpath=request.getParameter("img");//浏览图片的信息
Cookie cs[]=request.getCookies();//获取可访问cookie
boolean flag=false;
if(cs!=null){
for(Cookie c:cs){
if(!c.getName().equals("imgs"))//获取记录图片信息的cookie
continue;
String split="jrd_splitImg###";//分隔符
String imgsPath=c.getValue();
String[] strs=imgsPath.split(split);
imgsPath=imgpath;//将图片的显示位置置前
boolean boo=false;
for(String str:strs){
if(str.equals(imgpath))
boo=true;//当图片再次被浏览时
}
if(boo){
for(String str:strs){
if(str.equals(imgpath))//去除重复信息
continue;
imgsPath=imgsPath+split+str;
}
}else{//否则直接添加
int count=1;
for(String str:strs){
if(count>=3)//限制记录的个数
break;
imgsPath=imgsPath+split+str;
count++;
}
}
c.setValue(imgsPath);
c.setPath(request.getContextPath());//必须设置的path路径与请求的cookie的path一致,否则为不同对象
c.setMaxAge(60*60);
response.addCookie(c);
flag=true;
}
}
if(flag==false){//第一次访问时,创建cookie对象
Cookie cookie=new Cookie("imgs", imgpath);
cookie.setPath(request.getContextPath());//path设置要可访问,且可被显示的JSP访问
cookie.setMaxAge(60*60);//设置有效时间,以秒为单位
response.addCookie(cookie);//发送cookie
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<img src='"+imgpath+"'/>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}jsp界面显示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
img{
width: 200px;
height: 200px;
}
.last{
width: 40px;
height: 40px;
}
</style>
</head>
<body>
<h1>最近访问的图片</h1>
<%
Cookie[] cs=request.getCookies();
for(Cookie c:cs){
if(!c.getName().equals("imgs"))//获取图片信息
continue;
String split="jrd_splitImg###";
String imgsPath=c.getValue();
String[] strs=imgsPath.split(split);
for(String str:strs){
%>
<img class="last" src="<%=str%>"/>
<%}} %>
<hr/>
<%
for(int i=1;i<=8;i++){
String path=request.getContextPath()+"/imgs/"+i+".jpg";
%>
<a href="showServlet?img=<%=path %>">
<img src="<%=path%>"/></a>
<%} %>
</body>
</html>
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String imgpath=request.getParameter("img");//浏览图片的信息
Cookie cs[]=request.getCookies();//获取可访问cookie
boolean flag=false;
if(cs!=null){
for(Cookie c:cs){
if(!c.getName().equals("imgs"))//获取记录图片信息的cookie
continue;
String split="jrd_splitImg###";//分隔符
String imgsPath=c.getValue();
String[] strs=imgsPath.split(split);
imgsPath=imgpath;//将图片的显示位置置前
boolean boo=false;
for(String str:strs){
if(str.equals(imgpath))
boo=true;//当图片再次被浏览时
}
if(boo){
for(String str:strs){
if(str.equals(imgpath))//去除重复信息
continue;
imgsPath=imgsPath+split+str;
}
}else{//否则直接添加
int count=1;
for(String str:strs){
if(count>=3)//限制记录的个数
break;
imgsPath=imgsPath+split+str;
count++;
}
}
c.setValue(imgsPath);
c.setPath(request.getContextPath());//必须设置的path路径与请求的cookie的path一致,否则为不同对象
c.setMaxAge(60*60);
response.addCookie(c);
flag=true;
}
}
if(flag==false){//第一次访问时,创建cookie对象
Cookie cookie=new Cookie("imgs", imgpath);
cookie.setPath(request.getContextPath());//path设置要可访问,且可被显示的JSP访问
cookie.setMaxAge(60*60);//设置有效时间,以秒为单位
response.addCookie(cookie);//发送cookie
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<img src='"+imgpath+"'/>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}jsp界面显示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
img{
width: 200px;
height: 200px;
}
.last{
width: 40px;
height: 40px;
}
</style>
</head>
<body>
<h1>最近访问的图片</h1>
<%
Cookie[] cs=request.getCookies();
for(Cookie c:cs){
if(!c.getName().equals("imgs"))//获取图片信息
continue;
String split="jrd_splitImg###";
String imgsPath=c.getValue();
String[] strs=imgsPath.split(split);
for(String str:strs){
%>
<img class="last" src="<%=str%>"/>
<%}} %>
<hr/>
<%
for(int i=1;i<=8;i++){
String path=request.getContextPath()+"/imgs/"+i+".jpg";
%>
<a href="showServlet?img=<%=path %>">
<img src="<%=path%>"/></a>
<%} %>
</body>
</html>
相关文章推荐
- cookie的secure属性详解
- 浏览器 cookie 限制
- android读写cookie的方法示例
- 浅谈COOKIE和SESSION区别
- 深入解析Session是否必须依赖Cookie
- 对比分析php中Cookie与Session的异同
- 新手菜鸟必读:session与cookie的区别
- php实现通过cookie换肤的方法
- php cookie工作原理与实例详解
- ASP.NET中Application、Cookie、Session、Cache和ViewState
- C#中Cookie之存储对象
- C#基于WebBrowser获取cookie的实现方法
- ASP.NET Cookie 操作实现
- php中cookie的作用域
- 写入cookie的JavaScript代码库 cookieLibrary.js
- 关于cookie的初识和运用(js和jq)
- JavaScript cookie 跨域访问之广告推广
- ie7下利用ajax跨域盗取cookie的解决办法
- JS使用cookie实现DIV提示框只显示一次的方法