需要设置四个类,一个Book、
DBUtil、
ServletDemo1和
ServletDemo2
,分析见图。
一个指
针越界搞了好久,最后才发现错误,原来找错误有几个方法:一可以将错误的信息发到网上进行搜索、二将错误处的附近地方注释掉,看其他地方是否报错,找到出错的原因。一般错误的地方就在附近。三、指针越界真的会报出一些想不到的错误。具体的代码如下,其实将思路搞清楚之后,其他的还是比较好写的。只有一个难点,就是id号的设置。
public class Book {
private String id;
private String name;
private int price;
private String author;
....设置get、set方法就好了
}
public class DBUtil {
public static Map<String,Book>books=new HashMap<String,Book>();
static{
books.put("1", new Book("1","Java",20,"王欣"));
books.put("2", new Book("2","语文",30,"王亿"));
books.put("3", new Book("3","数学",40,"王二"));
books.put("4", new Book("4","英语",50,"王三"));
}
//得到所有书
public static Map<String,Book>showBooks(){
return books;
}
public static Book finallyBookId(String id){
return books.get(id);
}
}
public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//展示所有书的信息
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.write("所有的书为: <br/>");
//展示浏览记录
Map<String ,Book>books=DBUtil.showBooks();
//遍历,将书名全部展示出来
for(Map.Entry<String ,Book> book:books.entrySet()){
out.write("<a href='"+request.getContextPath()+"/servlet/demo2?id="+book.getKey()+"' target='_blank'>"+book.getValue().getName()+"</a><br/>");
}
//从cookie中取值并且输出
out.write("展示浏览记录: <hr/><br/>");
Cookie[] ck=request.getCookies();
//Cookie historyId=null;
for(int i=0;ck!=null&&i<ck.length;i++){
//选择有名字为historyId的cookie
if("historyId".equals(ck[i].getName())){
String value=ck[i].getValue();
String[] values=value.split("-");
for(int j=0;j<values.length;j++){
Book book=DBUtil.finallyBookId(values[j]);
out.print(book.getName()+"<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
public class ServletDemo2 extends HttpServlet {
private String orgnizeId(String id, HttpServletRequest request) {
// 假设最多只有三本书的浏览记录显示
Cookie[] ck=request.getCookies();
if(ck==null){
return id;
}
Cookie historyId=null;
for(int i=0;i<ck.length;i++){
if("historyId".equals(ck[i].getName())){
historyId=ck[i];
}
}
if(historyId==null){
return id;
}
//证明存在cookie,并且有名字为historyId的cookie
String value=historyId.getValue();
//先把字符串分成数组的形式
String[] values=value.split("-");
//在这里为了方便删除和修改,使用LinkedList来存储
LinkedList<String >list=new LinkedList<String >(Arrays.asList(values));
if(list.size()<3){
if(list.contains(id)){
list.remove(id);
}
}else{
if(list.contains(id)){
list.remove(id);
}else{
list.removeLast();
}
}
list.addFirst(id);//最后第一个id都是刚传入的
//将list表中的数据进行拼接
StringBuffer sbd=new StringBuffer();
for(int i=0;i<list.size();i++){
if(i>0){
sbd.append("-");
}
sbd.append(list.get(i));
}
return sbd.toString();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置格式
response.setContentType("text/html;charset=UTF-8");
PrintWriter out =response.getWriter();//获得一个输出流
//获得所有图书的详细细节
//首先获得传过来的id号
String id=request.getParameter("id");
//通过id获得指定的书
Book book=DBUtil.finallyBookId(id);
//打印图书的信息两种方法输出
out.write(book.toString());
out.print(book);
//设置Cookie orgnizeId(id,request)组织id
String historyId=orgnizeId(id,request);
Cookie cookie=new Cookie("historyId", historyId);
cookie.setPath("/");
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
}
/*
* id的显示形式,有三个地方会是的id值发生改变
* 客户端 demo1 demo2
* 没有cookie 1 historyId=1
* 有,但是不是historyId 1 historyId=1
* 有,historyId=1-2 3 historyId=3-2-1
* 有,historyId=1-2 2 historyId=2-1
* historyId=3-2-1 4 historyId=4-2-1
* historyId=3-2-1 2 historyId=2-3-1
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
转载自己的另一个账号,估计以后只会用csdn的了http://blog.163.com/mr_wang_xin/