Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)

时间:2023-05-31 17:34:14

public class ShowServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
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("<a href='"+request.getContextPath()+"/jsps/show.jsp"+"'>返回</a>");
out.println("<hr/>");
String img = request.getParameter("img");//1.jpg
String html = "<img src='"+request.getContextPath()+"/imgs/"+img+"'/>";
out.println(html);

//显示最近浏览的图片

//把浏览信息记录在cookie("images",imgs)中
//imgs = "1.jpg,2.jpg,3.jpg";
Cookie cs[] = request.getCookies();
boolean boo = false;
if(cs!=null){
for(Cookie c:cs){
if(c.getName().equals("images")){
String imgs = c.getValue();//以前浏览的
//如果记录中已经存在该当前图片信息(之前浏览的)清除
if(imgs.indexOf(img)>=0){
imgs = imgs.replace(img, "");
if(imgs.indexOf(",")==0){//开始处有一个多余“,”号的情况
imgs = imgs.substring(1);
}else if(imgs.lastIndexOf(",")==imgs.length()-1){//最后处存在一个多余“,”号的情况
imgs = imgs.substring(0,imgs.length()-1);
}else{//中间处有一个多余“,”号的情况
imgs = imgs.replaceAll(",,", ",");
}
}

imgs = img+","+imgs;//更新浏览记录。把这次浏览的图片加到最前面

//控制只保存最近浏览的3个
if(imgs.split(",").length>3){
imgs = imgs.substring(0, imgs.lastIndexOf(","));
}

//把更新后的记录保存到c中
c.setValue(imgs);
c.setMaxAge(60*60*24*30);//30天
c.setPath("/");

response.addCookie(c);
boo = true;
break;
}
}
}
if(boo==false){//不存在图片记录的cookie,这是第一次访问,new一个cookie
Cookie c = new Cookie("images",img);
c.setMaxAge(60*60*24*30);//30天
c.setPath("/");
response.addCookie(c);
}

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

}

----------------------------------------------------------------------------------------------------------------------------------

<a href="jsps/show.jsp">利用cookie技术实现显示用户最近浏览的图片</a>

----------------------------------------------------------------------------------------------------------------------------------

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>图片浏览</title>
</head>

<body>
<h2>图片浏览</h2>
<p>浏览过的图片</p>
<!-- 把ShowServlet记录到cookie中的浏览记录读取出来,并显示相应的图片 -->
<%
//jsp1: html+java
//jsp2: html+jstl+EL
String str=null;
Cookie cs[] = request.getCookies();
if(cs!=null){
for(Cookie c:cs ){
if(c.getName().equals("images")){
str = c.getValue();
break;
}
}
}
if(str!=null){
String strs[] = str.split(",");
for(String s:strs){//s=1.jpg
%>
<img width=50 height=50 src="<%=request.getContextPath()%>/imgs/<%=s%>"/>
<%
}

}
%>

<hr/>
<a href="<%=request.getContextPath()%>/ShowServlet?img=1.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/1.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=2.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/2.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=3.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/3.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=4.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/4.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=5.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/5.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=6.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/6.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=7.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/7.jpg"/>
</a>
<a href="<%=request.getContextPath()%>/ShowServlet?img=8.jpg">
<img width=100 height=100 src="<%=request.getContextPath()%>/imgs/8.jpg"/>
</a>

</body>
</html>

-------------------------------------------------------------------------------------------------------------------------------------------

------

<!--
cookie权限(相同路径可以读取。子路径的servlet可以读上级路径的cookie,反之不行!):
※说明: cookie的path(路径): 通过 coo.setPath()来设置的
servlet的路径: 在web.xml中用<url-pattern>来配置

1)可以读取---相同路径
cookie设置的path= reqeust.getContextPath() ---等价于“/”
读取cookie的servlet的路径: /

2)下面的也可以读---子路径的servlet 可以读 上级路径的cookie
cookie设置的path= reqeust.getContextPath() ---等价于“/”
读取cookie的servlet的路径: /servlet

3)下面的不可以读
cookie设置的path= reqeust.getContextPath()/servlet ---等价于“/servlet”
读取cookie的servlet的路径: /

-->

----------------------------------------------------------------------------------------------------------------------------