求高手帮忙解决一下问题Java Web Cookie实例
package cn.com; import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CookieDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码问题
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter(); //输出所有的商品
out.write("本网站有如下商品:<br/>");
Map<String ,Book > map=Db.getAll();
for(Map.Entry<String, Book> entry:map.entrySet())
{
Book book=entry.getValue();
out.print("<a href='/Cookie/CookieDemo4?id=' "+book.getId()+" target='_blank'>"+book.getName()+"</a><br/>");
} //显示用户曾经看过的商品
Cookie cookies[]=request.getCookies();
for(int i=0;cookies!=null&&i<cookies.length;i++)
{
if(cookies[i].getName().equals("bookHistory"))
{
String ids[]=cookies[i].getValue().split("\\,");
for(String id:ids)
{
Book book=(Book)Db.getAll().get(id);
out.print(book.getName()+"<br/>");
}
}
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response);
} }
//Db作为数据库
class Db
{
private static Map<String ,Book> map=new LinkedHashMap();
static
{
map.put("1", new Book("1","Java WEB开发","WY","好书"));
map.put("2", new Book("2","WEB开发","zt","一般"));
map.put("3", new Book("3","程序设计","df","较好书"));
map.put("4", new Book("4","计算机组成","as","一般好书"));
map.put("5", new Book("5","编译原理","ty","很好书"));
map.put("6", new Book("6","网络维护","hj","非常好书"));
} public static Map getAll()
{
return map;
}
}
//书
class Book
{
private String id;
private String name;
private String author;
private String description; public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} }
package cn.com; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class CookieDemo4
*/
public class CookieDemo4 extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter(); //根据用户带过来的id ,显示响应商品的详细信息
String id=request.getParameter("id");
Book book=(Book)Db.getAll().get(id); out.write(book.getId()+"<br/>");
out.write(book.getAuthor()+"<br/>");
out.write(book.getDescription()+"<br/>");
out.write(book.getName()+"<br/>"); //构建Cookie ,回写给浏览器
String cookieValue=buildCookie(id,request);
Cookie cookie=new Cookie("bookHistory",cookieValue);
cookie.setMaxAge(30*24*3600);
cookie.setPath("/Cookie");
response.addCookie(cookie);
} private String buildCookie(String id, HttpServletRequest request) {
// TODO Auto-generated method stub
//如果用户没有带任何的Cookie过来 //如果用户带了Cookie来了包含在看过的id中,则要把id删掉 //带了Cookie过来了但是没有包含看过的id //带了Cookie过来但是没有包含 String bookHistory=null;
Cookie cookies[]=request.getCookies();
for(int i=0;cookies!=null&&i<cookies.length;i++)
{
if(cookies[i].getName().equals("bookHistory"))
{
bookHistory=cookies[i].getValue();
}
}
if(bookHistory==null)
{
return id;
}
@SuppressWarnings("rawtypes")
LinkedList list=new LinkedList(Arrays.asList(bookHistory.split("\\,")));
// if(list.contains(id))
// {
// list.remove(id);
// list.addFirst(id);
// }
// else
// {
// if(list.size()>=3)
// {
// list.removeLast();
// list.addFirst(id);
// }
// else
// {
// list.addFirst(id);
// }
// } if(list.contains(id))
{
list.remove(id);
}
else if(list.size()>=3)
{
list.removeLast();
}
list.addFirst(id); StringBuffer sb=new StringBuffer();
Iterator it=list.iterator();
while(it.hasNext())
{
sb.append(id+",");
}
return sb.deleteCharAt(sb.length()-1).toString();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
} }
求高手帮忙解决一下问题Java Web Cookie实例的更多相关文章
- Java执行批处理.bat文件(有问题???求高手帮忙解答!!!)
Java执行批处理.bat文件(有问题???求高手帮忙解答!!!) 在项目开发中常常都会遇到需要在代码中调用批处理bat脚本,把自己在项目中遇到过的总结下 ...
- Java web Cookie详解(持久化+原理详解+共享问题+设置中文+发送多个Cookie)
Java web Cookie详解 啥是cookie? 查询有道词典得: web和饼干有啥关系? 这个谜底等等来为大家揭晓 会话技术 web中的会话技术类似于生活中两个人聊天,不过web中的会话指的是 ...
- 阿里云服务器云数据库免费体验(Java Web详细实例)
一.效果展示 博主部署了两个war包到阿里云服务器上,一个是没有连接数据库的,另外一个是连接了数据库的. (由于阿里云服务器免费使用15天,下面链接约2016年3月9日后无效) (1)无数据库版访问地 ...
- java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)
编写过程中需要注意的问题: 1.建立eclipse动态web工程时,需要改写编译后class文件的位置,通常情况下是这个位置:/WebContent/WEB-INF/classes 2.配置的页面链接 ...
- Java Web MVC实例
开发环境 Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 2 (4.4.2) tomcat:7.0 数据库: ...
- Java Web Cookie
一.什么是cookie? 1.Cookie能使站点跟踪特定访问者的访问次数.最后访问时间和访问者进入站点的路径 2.Cookie能告诉在线广告商广告被点击的次数,从而可以更精确的投放广告 3.Cook ...
- java web Cookie浅谈
1.会话控制 > Http协议 Http协议两个缺陷: 1.HTTP协议是纯文本的 2.HTTP协议是无状态的 服务器不能简单的通过HTTP协议来区分多次请求是否发自同一个用户 虽然通过HTTP ...
- Java/Java Web中乱码解决汇总
在开发Java/Java Web Application过程中,往往会出现乱码问题,而且有的时候真会弄得人很烦,浪费太多的时间. 记得之前看过一篇帖子,详细解释了Encoding/Decoding过程 ...
- maven的java web项目启动找不到Spring ContextLoaderListener的解决办法
用maven搭建的java web项目,上传到git仓库后,当同事clone下来项目,部署到tomcat运行时,就报了如下错误,即启动web项目时,加载web.xml文件,找不到spring的监听器, ...
随机推荐
- cookie程序设计举例
编写Cookie应用程序,一般流程是:首先尝试获取某个Cookie变量,如果有,则表明是老客户,读取其cookie信息,为其提供服务. 如果没有,则表明是第一次来访的客户,通过表单提交获取其身份信息, ...
- java设计模式之 工厂模式Factory
好比某种套路,经过不断实践,证明对项目结构非常有利 如果需要获取某种对象,如同获取不同的产品 需要由工厂来提供,工厂模式 可能的类或者对象:工厂类 产品:Cat Dog Fish ... //动 ...
- 【异常处理】java.lang.NoClassDefFoundError
Exception in thread"main" java.lang.NoClassDefFoundError:org/apache/commons/lang/exception ...
- centos下添加的端口不能访问(防火墙关闭)
最近遇到一个郁闷的问题.好几天都没解决,求助,谢谢大家. 打算开放一个端口15900.可是无论怎么设置防火墙,或者干脆关闭防火墙.就是不能被外部机器访问(在同一内网网段机器). 本机访问没有问题(12 ...
- 26个jQuery使用小技巧(25)
下面列出了一些Jquery使用技巧.比如有禁止右键点击.隐藏搜索文本框文字.在新窗口中打开链接.检测浏览器.预加载图片.页面样式切换.所有列等高.动态控制页面字体大小.获得鼠标指针的X值Y值.验证元 ...
- delphi 7中使用idhttp抓取网页 解决假死现象
在delphi 7中使用idhttp抓取网页,造成窗口无反应的假死状态.通过搜索获得两种方法. 1.写在线程中,但是调用比较麻烦 2.使用delphi 提供的idantifreeze(必须安装indy ...
- Microsoft Azure 在北美 TechEd 大会上发布令人振奋的更新,帮助客户开始使用云服务
云计算因其速度.规模和成本节省等优势而备受众多企业青睐.但企业需帮助,才能以安全可靠的方式使用云,同时还要利用企业的现有投资, 才能实现这些优势.因此,在TechEd 大会上,我们推出了一些新的服务, ...
- 圣何塞与 Microsoft 宣布该市为超过 5,000 名市府公务员选择 Office 365、Windows Azure 和 StorSimple
过去几个月来我们展示了极大的客户吸引力,今天我们非常高兴地宣布,我们又赢得了一位新客户,且他们利用 Microsoft 革新 IT 的方式非常有趣. 今天,我们非常高兴地告诉大家,圣何塞市选择了 Mi ...
- 分析Ext2文件系统结构。
1. 目的 分析Ext2文件系统结构. 使用 debugfs 应该跟容易分析 Ext2文件系统结构 了解ext2的hole的 2. 准备工作 预习文件系统基本知识: http://www.doc88. ...
- 历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网
历峰集团3.43亿美元收购Net-a-Porter剩余股权_财经_腾讯网 历峰集团3.43亿美元收购Net-a-Porter剩余股权