package cn.itcast.shopping;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import cn.itcast.Book;
import cn.itcast.Db; public class ListBookServlet extends HttpServlet { public 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(book.getName()+"<a href='/ServletDemo/servlet/BuyServlet?id="+book.getId()+"' target='_blank'>购买</a><br/>");
} } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast.shopping;

 import java.io.IOException;
import java.io.PrintWriter;
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; import cn.itcast.Book;
import cn.itcast.Db; //完成购买
public class BuyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String id = request.getParameter("id");
Book book = (Book) Db.getAll().get(id); HttpSession session =request.getSession(); //从Session得到用户名用于保存所有书的集合(购物车)
List list = (List) session.getAttribute("list");
if(list==null){
list = new ArrayList();
session.setAttribute("list", list);
}
list.add(book); // request.getRequestDispatcher("/servlet/ListCarServlet").forward(request, response);
response.sendRedirect(request.getContextPath()+"/servlet/ListCarServlet");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast.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; import cn.itcast.Book; //显示用户购买商品
public class ListCarServlet extends HttpServlet { public 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(false);
if(session==null){
out.write("您没有购买任何商品!!");
return;
} out.write("您购买了如下商品:<br/>");
List<Book> list = (List) session.getAttribute("list");
for(Book book : list){
out.write(book.getName());
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast;

 import java.util.LinkedHashMap;
import java.util.Map; public class Db {
private static Map<String, Book> map = new LinkedHashMap(); static{
map.put("1", new Book("1","javaweb开发","Zero","一本好书!!"));
map.put("2", new Book("2","jdbc开发","one","一本好书!!"));
map.put("3", new Book("3","spring开发","two","一本好书!!"));
map.put("4", new Book("4","struks开发","three","一本好书!!"));
map.put("5", new Book("5","hibernate开发","four","一本好书!!"));
} public static Map getAll(){
return map;
}
}
 package cn.itcast;

 import java.io.Serializable;

 public class Book implements Serializable {
private String id;
private String name;
private String author;
private String description; 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;
}
}

用session实现简单的购物的更多相关文章

  1. php实现一个简单的购物网站

    实现一个简单的购物网站 一.考试时间:8小时 二.开发工具:DW 三.数据库:见附件 四.需要实现的页面: Index:浏览商品页面,显示商品列表,用户可以点击“购买“. ViewCart:查看购物车 ...

  2. flask的cookie和session的简单原理

    在Flask的框架中,自己已经封装了 cookie的respons,request 有存储就有读取及删除,那么就拿购物车来举例 在我们登陆的时候会有之前在购物车存放的物品.也就是说在一个地方为我们保存 ...

  3. 用 Vue 做一个简单的购物app

    前言 最近在学习Vue的使用.看了官方文档之后,感觉挺有意思的.于是着手做了一个简单的购物app.h5 与原生 app 交互的原理这是我第一次在这个网站上写分享,如有不当之处,请多多指教. 一整个项目 ...

  4. java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)

    1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...

  5. 使用cookie/session实现简单的用户信息的保存

    cookie一般用来存储非关键信息 , 用户名和密码等敏感信息一般采用session 来存储:cookie和session的最大区别是当服务器端存储session 之后,用户再次请求时候只是请求了一个 ...

  6. Spring Session实现分布式session的简单示例

    前面有用 tomcat-redis-session-manager来实现分布式session管理,但是它有一定的局限性,主要是跟tomcat绑定太紧了,这里改成用Spring Session来管理分布 ...

  7. 【Spring】Spring Session的简单搭建与源码阅读

    搭建一个简单的Spring Session例子 引入依赖包 <dependencies> <dependency> <groupId>org.springframe ...

  8. 12月13日 什么是help_method,session的简单理解, find_by等finder method

    helper_method Declare a controller method as a helper. For example, helper_method :link_to def link_ ...

  9. 2017.9.28 web设计简单的购物车应用案例--session的简单应用

    该购物过程是在session范围内完成的,需要使用session对象实现信息的共享 (1)购买“肉类”商品的页面 <%@ page language="java" impor ...

随机推荐

  1. DSP using MATLAB 示例Example3.1 3.2 3.3

    上代码: w = [0:1:500]*pi/500; % [0,pi] axis divided into 501 points. X = exp(j*w) ./ (exp(j*w) - 0.5*on ...

  2. http://www.cnblogs.com/summers/p/3225375.html

    http://www.cnblogs.com/summers/p/3225375.html

  3. Android自动化测试 - Robotium之Robotium在不同分辨率下clickonview不支持解决方案

    使用Robotium中的clickonview方法进行点击操作时,可能在你本机上能够顺利执行,但把脚本移植到不同分辨率的设备下却有可能点不到控件的情况. 网上找了一些资料,基本一条语句可以搞定: 在m ...

  4. 把Actor绑定到角色的插槽上

    void AMonster::PostInitializeComponents(){ Super::PostInitializeComponents(); // instantiate the mel ...

  5. Python学习笔记02

      元组:圆括号的,不能进行赋值操作,即不可更改. 列表:方括号的,可以修改. 访问:均使用下标访问   # 元组是一个静态列表,初始化之后就不可以修改,可以试任意类型 tuple1 = ('a st ...

  6. 彩色照片转换为黑白照片(Color image converted to black and white picture)

    This blog will be talking about the color image converted to black and white picture. The project st ...

  7. ios8 tableView设置滑动删除时 显示多个按钮

      ** *  tableView:editActionsForRowAtIndexPath:     //设置滑动删除时显示多个按钮 *  UITableViewRowAction          ...

  8. LVS DR脚本 解析

    vip.sh #!/bin/bash 192.168.2.240 broadcast 192.168.2.240 netmask 255.255.255.255 up route add -host ...

  9. 使用C#实现FTP的文件上传和下载【转】

    参考博文:http://blog.163.com/mity_rui@126/blog/static/1098136182013101525615577/  

  10. Maven3路程(一)用Maven创建第一个web项目(1)

    一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Packaged是默认创建 ...