原理:
项目目录:
Book.java代码:
package com.haidi8.book;
import java.io.Serializable;
public class Book implements Serializable{
private String id;
private String name;
private double price;
private String author;
public Book(String id, String name, double price, String author) {
super();
this.id = id;
this.name = name;
this.price = price;
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price
+ ", author=" + author + "]";
}
}
showallbooksservlet.java代码:
package com.haidi8.car;
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 com.haidi8.book.Book;
import com.haidi8.util.DBUtil;
public class showallbooksservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("本网站有一下好书<br/>");
Map<String, Book> books = DBUtil.findAllBooks();
for (Map.Entry<String, Book> book : books.entrySet()) {
String url = request.getContextPath()+"/addcar?id="+book.getKey();
out.print("<a href='"+response.encodeURL(url)+"' >"+book.getValue().getName()+"</a><br/>");
}
String url2 = request.getContextPath()+"/showcar";
out.print("<a href='"+response.encodeURL(url2)+"'>查看购物车</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
addcar.java代码:
package com.haidi8.car;
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 com.haidi8.book.Book;
import com.haidi8.util.DBUtil;
public class addcar extends HttpServlet {
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");
Book book=DBUtil.findBookById(id);
//得到session对象
HttpSession session = request.getSession();
//从session中取出list(购物车)
List<Book> list = (List<Book>) session.getAttribute("car");
if(list==null){
list=new ArrayList<Book>();
}
list.add(book);
out.print("购买成功!");
String url = request.getContextPath()+"/showallbooksservlet";
response.setHeader("refresh", "2;url="+response.encodeURL(url));
session.setAttribute("car", list);//把list放回session域中
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
showcar.java:
package com.haidi8.car;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.haidi8.book.Book;
public class showcar extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("购物车中有一下商品");
//得到session对象
List<Book> books = (List<Book>) request.getSession().getAttribute("car");
if(books==null){
out.print("你还没有忘购物车中添加商品");
response.setHeader("refresh", "2;url="+request.getContextPath()+"/showallbooksservlet");
//response.sendRedirect(request.getContextPath()+"/showallbooksservlet");
}
for(Book book:books){
out.write(book.getName()+"<br/>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
DBUtil.java代码:
package com.haidi8.util;
import java.util.HashMap;
import java.util.Map;
import com.haidi8.book.Book;
public class DBUtil {
private static Map<String, Book> books = new HashMap<String, Book>();
static{
books.put("1", new Book("1", "金瓶梅", 20, "王瑞鑫"));
books.put("2", new Book("2", "葵花宝典", 20, "杨成毅"));
books.put("3", new Book("3", "九阴真经", 30, "陈光"));
books.put("4", new Book("4", "玉女心经", 10, "陈志家"));
}
//得到所有书
public static Map<String, Book> findAllBooks(){
return books;
}
/**
* 根据id查找指定的书
* @param id
* @return
*/
public static Book findBookById(String id){
return books.get(id);
}
}
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>showallbooksservlet</servlet-name>
<servlet-class>com.haidi8.car.showallbooksservlet</servlet-class>
</servlet>
<servlet>
<servlet-name>addcar</servlet-name>
<servlet-class>com.haidi8.car.addcar</servlet-class>
</servlet>
<servlet>
<servlet-name>showcar</servlet-name>
<servlet-class>com.haidi8.car.showcar</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showallbooksservlet</servlet-name>
<url-pattern>/showallbooksservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>addcar</servlet-name>
<url-pattern>/addcar</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>showcar</servlet-name>
<url-pattern>/showcar</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行效果: