HttpSession session = request.getSession();
String data = "message form SessionDemo";
session.setAttribute("data", data);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
HttpSession session = request.getSession();
String data = (String) session.getAttribute("data");
writer.write(data);
public class Product {
private String id;
private String name;
private String author;
public Product() {
super();
}
public Product(String id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
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 class ProductDatabase {
private static Map<String,Product> map = new HashMap<String,Product>();
static{
map.put("1", new Product("1","《Java编程思想》","JB"));
map.put("2", new Product("2","《Java核心技术》","fdaf"));
map.put("3", new Product("3","《Java并发编程》","什么鬼"));
map.put("4", new Product("4","《Head first 设计模式》","老王"));
map.put("5", new Product("5","《HTML5权威手册》","hhaa"));
}
public static Map<String,Product> getMap() {
return map;
}
}
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
//获取数据库中的商品数据
Map<String, Product> map = ProductDatabase.getMap();
for(Map.Entry<String, Product> entry : map.entrySet()) {
Product book = entry.getValue();
writer.print(book.getName()+" <a href='/myservlet/servlet/BuySession?id="+book.getId()+"' >购买</a> <br/>");
}
//获取用户所购买商品的id号
String productId = request.getParameter("id");
Product book = ProductDatabase.getMap().get(productId);
//将用户所购买的商品加入到Session对象中保存,以便最后一起结账,类似于购物车功能
HttpSession session = request.getSession();
List<Product> list = (List<Product>) session.getAttribute("productList");
if(list == null){
//首次购买
list = new ArrayList<Product>();
session.setAttribute("productList", list);
}
list.add(book);
//跳转到购物车列表上
response.sendRedirect("/myservlet/servlet/CartListServlet");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("您购买的商品如下: <br/>");
HttpSession session = request.getSession();
List<Product> list = (List<Product>) session.getAttribute("productList");
for(Product p : list) {
writer.write(p.getName()+"<br/>");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有