一个小小购物车案例
改项目用到cookie技术和session技术的综合运用,以及分别用cookie和session达到不同的功能,模拟一个简单的购物车,登录、浏览商品、以及购买的过程。一下是案例:
用cookie编写的代码:
BookList.java
package com.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.BookDao;
import com.domain.Book;
public class BookList extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.显示所有书名
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
BookDao book = new BookDao();
pw.print("<h1>本网站的商品有:</h1>");
List<Book> list = book.getAll();
for(Book book1:list){
pw.print("<a href='BookBack?id="+book1.getId()+"'target='_blank'>"+book1.getName()+"</a><br>");
}
//2.显示浏览过的书名
pw.write("<h1>您最近浏览过的商品有:</h1>");
Cookie[] cookie = request.getCookies();
for(int i=0; cookie!=null && i<cookie.length;i++){
if(cookie[i].getName().equals("bookshop")){
String[] id = cookie[i].getValue().split("_");
for(String str:id){
Book books = book.getBook(str);
pw.print(books.getName()+"<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Bookback.java
package com.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
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;
import com.dao.BookDao;
import com.domain.Book;
public class BookBack extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//显示书的详细信息,根据id获取
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
String id = request.getParameter("id");
BookDao bd = new BookDao();
Book book2 = bd.getBook(id);
pw.print("书号:"+book2.getId()+"<br>");
pw.print("书名:"+book2.getName()+"<br>");
pw.print("作者:"+book2.getAuthor()+"<br>");
pw.print("描述:"+book2.getDescribe()+"<br>");
//将书号写入cookie,有三种情况
//bookhistory=2,3,1 1 1,2,3 (已存在,删除后面的加前面)
//bookhistory=2,3,5 1 1,2,3 (三个删后面)
//bookhistory=2,3 1 1,2,3(两个直接添加)
String cookieValue = BuildCookieValue(id,request);
Cookie cookie = new Cookie("bookshop",cookieValue);
cookie.setMaxAge(30*24*3600);
cookie.setPath("/BookShop");
response.addCookie(cookie);
}
private String BuildCookieValue(String id, HttpServletRequest request) {
String bookshop = null;
Cookie[] cookies = request.getCookies();
for(int i=0;cookies!=null&&i<cookies.length;i++){
if(cookies[i].getName().equals("bookshop")){
bookshop = cookies[i].getValue();
}
}
if(bookshop==null){
return id;
}
LinkedList<String> list = new LinkedList<String>(Arrays.asList(bookshop.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);
}
StringBuffer sb = new StringBuffer();
for(String ids:list){
sb.append(ids+"_");
}
return sb.deleteCharAt(sb.length()-1).toString();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
BookDao.java
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.domain.Book;
import com.util.DBManager;
public class BookDao {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
//查询所有书的方法
public List<Book> getAll(){
List<Book> list = new ArrayList<Book>();
String sql = "select * from book";
con = DBManager.getConnection();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Book book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setDescribe(rs.getString("describe"));
list.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBManager.closeDB(con, ps, rs);
}
return list;
}
//按id获取书
public Book getBook(String id){
Book book = null;
con = DBManager.getConnection();
String sql = "select * from book where id=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
if(rs.next()){
book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setDescribe(rs.getString("describe"));
}
} catch (SQLException e) {
e.printStackTrace();
&nb
补充:Web开发 , 其他 ,