Java Web开发之基于Session的购物商店实现方法
程序员文章站
2024-03-07 11:21:21
本文实例讲述了java web开发之基于session的购物商店实现方法。分享给大家供大家参考,具体如下:
package cn.com.shopping;
i...
本文实例讲述了java web开发之基于session的购物商店实现方法。分享给大家供大家参考,具体如下:
package cn.com.shopping; import java.io.ioexception; import java.util.arraylist; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; //完成购买 public class buyservlet extends httpservlet { private static final long serialversionuid = 1l; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string id=request.getparameter("id"); book book=(book)db.getall().get(id); //再加上那个关闭cookie时session的剞劂方案 //阻止session的时候解决方案 httpsession session=request.getsession(false); //从session中得到用户的保存所有书的集合(购物车) list list=(list)session.getattribute("list"); if(list==null) { list=new arraylist(); session.setattribute("list", list); } list.add(book); string url=response.encoderedirecturl("/session/sessioncountdemo"); response.sendredirect(url); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request,response); } } package cn.com.shopping; import java.io.ioexception; import java.io.printwriter; import java.util.linkedhashmap; import java.util.map; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; //显示书 public class listbookservlet 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(); httpsession session=request.getsession(); out.print("本店有如下的商品:<br/>"); map<string ,book > map=db.getall(); for(map.entry<string, book> entry:map.entryset()) { book book=entry.getvalue(); string url=response.encodeurl("/session/buyservlet?id="+book.getid()); out.print(book.getname()+"<a href='"+url+"' target='_blank' >购买</a><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.shopping; import java.io.ioexception; import java.io.printwriter; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class sessioncountdemo 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(); httpsession session=request.getsession(); if(session==null) { out.write("您没买任何的商品!"); return; } out.write("您购买了如下的商品:"); list<book> list=(list) session.getattribute("list"); for(book book:list) { out.write(book.getname()); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request,response); } }
希望本文所述对大家java web程序设计有所帮助。