Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)
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的路径: /
-->
----------------------------------------------------------------------------------------------------------------------------
Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)的更多相关文章
- 用JS中的cookie实现商品的浏览记录
最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...
- js操作Cookie,实现历史浏览记录
/** * history_teacher.jsp中的js,最近浏览名师 * @version: 1.0 * @author: mingming */ $(function(){ getHistory ...
- Chrome 中删除单条浏览记录
悲伤...之前用非隐私窗口观看了小电影.于是打开 chrome://settings/ ...... 现在才知道 windows 上使用 shift + del 即可删除该浏览记录 ....... 以 ...
- if;脚本中退出语句:exit 数字,用$?查时为exit设置的数字,此数字为程序执行完后的返回数据,可以通过此方法自动设定
if [ 条件 ];then 代码 fi if [ 条件 ] then 代码 fi [root@localhost ~]# df 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda5 % ...
- 使用cookie实现打印浏览记录的功能
可以用cookie知识来实现打印浏览记录.这里面用到的思路是将浏览记录以字符串的方式保存到cookie中,当浏览记录增加时,再将其转化为数组. $uri=$_SERVER['REQUEST_URI'] ...
- 使用modle1(jsp+javabeans)及cookie技术实现商品展示和浏览记录
步骤1:创建dbHelper工具类,该类主要用于获取数据库连接,采用单例模式. 步骤2:创建实体类商品类,商品表,在dao实现数据的封装处理. 步骤3:在jsp页面导入实体类,调用DAO的静态方案获取 ...
- 使用Cookie实现用户商品历史浏览记录
该功能分为四个模块: 1. 获取所有商品并以链接的形式显示 out.write("网站商品: <br/>"); Map<String, Book> book ...
- Cookie实现商品浏览记录--方式一:Java实现
方式一:Java代码方式实现:此种方式实现思路较为顺畅.难点在于,如何实现将最近浏览的产品显示在最前面:实现方式是借助LinkedList提供的remove()方法,先将此id从列表中移除,然后再借助 ...
- destoon系统开发-最新利用浏览器的cookie 做历史浏览记录
注意: 代码 放在要显示的为 (一般放在详情页),注意本教程不入库,直接利用浏览器的 cookie 缓存判断 <!--历史浏览记录 S--> <div class=&quo ...
随机推荐
- yii 验证器和验证码
http://www.yiiframework.com/doc/api/1.1/CCaptcha http://www.cnblogs.com/analyzer/articles/1673015.ht ...
- Android开发UI之动画侦听
动画侦听使用了AnimationListener接口,需要实现三个方法onAnimationStart().onAnimationRepeat().onAnimationEnd() 代码: 实现But ...
- USACO3.22Stringsobits
DP预处理出来 i位不超过j的个数 然后再进行从小到大找第一个比I大的 然后用I减掉上一个比I小的 剩余的按照之前的方法循环找 知道剩余为0 细节挺繁琐的 对照数据改了又改 最后一组数据还超 了int ...
- 2016值得关注的语言平台、JS框架
语言和平台 Python 3.5 在今年发布了,带来了很多新特性 比如 Asyncio,,为你带来了类似 node.js 的事件机制,还有type hints. 鉴于Python 3 终于真正地火起来 ...
- 为SharePoint网站创建自定义导航菜单
转:http://kaneboy.blog.51cto.com/1308893/397779 相信不少人都希望把SharePoint网站内置的那个顶部导航菜单,换成自己希望的样式.由于SharePoi ...
- 1.进入debug模式(基础知识列表)
1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...
- MVC3中Action返回类型ActionResult类型
MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中.这些包含在控制器中的方法,我们称为控制器中的 Action,比如:HomeController 中的 I ...
- Android 再按一次退出程序
实现代码: private long exitTime = 0; /** * 捕捉返回事件按钮 * * 因为此 Activity 继承 TabActivity 用 onKeyDown 无响应,所以改用 ...
- vijosP1359 Superprime
vijosP1359 Superprime 链接:https://vijos.org/p/1359 [思路] 搜索+数学. 很明显的搜索,依次确定每一个数,用参数sum记录dfs即可. 本题的关键在于 ...
- matplotlib 初使用
试玩了一下 matplotlib, 感觉是:很酥狐吖~ 完全不像 ggplot 那样云里雾里,但是后者展现出的图要漂亮优雅许多. x = linspace(0, 10, 100) //初始化一个 [0 ...