bean包
bean包共四个类,用于包装。其中两个集合,BOOKSTORE中的HASHMAP以及ShopCar中的ArrayList
package bean;
public class Book {
private int id;
private String name;
private double price;
public Book(){
}
public Book(int id,String name,double price){
this.id=id;
this.name=name;
this.price=price;
}
public int getId() {
return id;
}
public void setId(int 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;
}
}
package bean;
import java.util.*;
public class BookStore {
private Book book;
private int num;
HashMap bookstore=new HashMap();
public BookStore(){
bookstore.put(1, new Book(1,"England",60.0));
bookstore.put(2, new Book(2,"Japanese",60.0));
bookstore.put(3, new Book(3,"American",60.0));
bookstore.put(4, new Book(4,"Britiash",60.0));
bookstore.put(5, new Book(5,"Button",60.0));
bookstore.put(6, new Book(6,"java",60.0));
bookstore.put(7, new Book(7,"sports",60.0));
bookstore.put(8, new Book(8,"Ellle",60.0));
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public HashMap getBookStore(){
return bookstore;
}
}
package bean;
public class Line {
private int code;
private Book book;
private int num;
public Line() {
}
public Line(int code, Book book, int num) {
this.book = book;
this.num = num;
this.code = code;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getNum() {
return num;
}
public void setNum(int num) {
if (num > 0) {
this.num = num;
}
else{
this.num=1;
}
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public boolean equals(Line line) {
int id = book.getId();
int nextId = line.getBook().getId();
if (id == nextId) {
return true;
} else {
return false;
}
}
public int hashCode() {
return book.getId();
}
}
package bean;
import java.util.*;
public class ShopCar {
ArrayList<Line> lines;
public ShopCar(){
lines=new ArrayList<Line>();
}
public ArrayList<Line> getLines() {
return lines;
}
public void setLines(ArrayList<Line> lines) {
this.lines = lines;
}
public void addLine(Line l){
for(int i=0;i<lines.size();i++){
Line line=(Line)lines.get(i);
if(l.equals(line)){
line.setNum(l.getNum()+line.getNum());
return;
}
}
lines.add(l);
}
public void delLine(Line l){
lines.remove(l);
}
}
Servlet包
主要四个类,实现增删改查
package servlet;
import java.io.IOException;
import bean.Book;
import bean.BookStore;
import bean.ShopCar;
import bean.Line;
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 AddServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
addProcess(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
addProcess(request,response);
}
public void addProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
ShopCar shopcar;
if(request.getSession().getAttribute("ShopCar")==null){
shopcar=new ShopCar();
}
else{
shopcar=(ShopCar)request.getSession().getAttribute("ShopCar");
}
BookStore bookstore=new BookStore();
addLine(request,shopcar,bookstore);
request.getRequestDispatcher("ListCart.jsp").forward(request, response);
}
public void addLine(HttpServletRequest request,ShopCar shopcar,BookStore bookstore){
String id=request.getParameter("id");
int code=Integer.parseInt(id);
Book book=(Book)bookstore.getBookStore().get(Integer.parseInt(id));
int num=Integer.parseInt(request.getParameter(request.getParameter("id")));
if(num<1){
num=1;
}
Line l=new Line(code,book,num);
shopcar.addLine(l);
HttpSession session=request.getSession();
session.setAttribute("ShopCar", shopcar);
}
}
package servlet;
import java.io.IOException;
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 java.io.*;
import java.util.*;
import bean.Line;
import bean.ShopCar;
public class DelServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
delProcess(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
delProcess(request,response);
}
public void delProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
response.setCharacterEncoding("gb2312");
PrintWriter out=response.getWriter();
ShopCar shopcar=(ShopCar)request.getSession().getAttribute("ShopCar");
delLine(request,shopcar);
request.getRequestDispatcher("ListCart.jsp").forward(request, response);
out.flush();
out.close();
}
public void delLine(HttpServletRequest request,ShopCar shopcar){
int id=Integer.parseInt(request.getParameter("id"));
ArrayList lines=shopcar.getLines();
for(int i=0;i<lines.size();i++){
Line line=(Line)lines.get(i);
if(id==line.getBook().getId()){
lines.remove(line);
shopcar.setLines(lines);
HttpSession session=request.getSession();
session.setAttribute("ShopCar", shopcar);
}
}
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import bean.ShopCar;
public class ListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
listProcess(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
listProcess(request, response);
}
public void listProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
response.setCharacterEncoding("gb2312");
PrintWriter out=response.getWriter();
ShopCar shopcar;
if(request.getSession().getAttribute("ShopCar")==null){
out.println("<script>location.href='ListCart.jsp';</script>");
}
else{
shopcar=(ShopCar)request.getSession().getAttribute("ShopCar");
//System.out.println(shopcar.getLines().size());
if(shopcar.getLines()==null||shopcar.getLines().size()==0){
out.println("<script>alert('come here');location.href='ListBookStore.jsp';</script>");
}
request.getRequestDispatcher("ListCart.jsp").forward(request, response);
}
out.flush();
out.close();
}
}
package servlet;
import java.io.IOException;
import java.util.ArrayList;
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 bean.Line;
import bean.ShopCar;
public class UpdateServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
updateProcess(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
updateProcess(request,response);
}
public void updateProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
ShopCar shopcar=(ShopCar)request.getSession().getAttribute("ShopCar");
updateLines(request,shopcar);
request.getRequestDispatcher("ListCart.jsp").forward(request, response);
}
public void updateLines(HttpServletRequest request,ShopCar shopcar){
int id=Integer.parseInt(request.getParameter("id"));
ArrayList lines=(ArrayList)shopcar.getLines();
int num=Integer.parseInt(request.getParameter(request.getParameter("id")));
for(int i=0;i<lines.size();i++){
Line line=(Line)lines.get(i);
if(id==line.getBook().getId()){
line.setNum(num);
shopcar.setLines(lines);
}
}
HttpSession session=request.getSession();
session.setAttribute("ShopCar", shopcar);
}
}
Jsp页面
confirm.jsp
<%@ page language="java"
import="java.util.*,bean.Line,bean.BookStore,bean.ShopCar"
pageEncoding="gb2312"%>
<script>
function doSubmit(){
alert('欢迎再次购买!');
window.close();
}
</script>
<html>
<body bgcolor="silver">
<div align=center>
<font color="red">提交订单</font>
<form action="" method="post" name=form1>
<table border=1>
<tr>
<td width=s"10%">
ID
</td>
<td width="50%">
书名
</td>
<td width="10%">
价格
</td>
<td width="30%">
数量
</td>
</tr>
<%
ShopCar shopcar = (ShopCar) session.getAttribute("ShopCar");
if (shopcar == null) {
shopcar = new ShopCar();
if (shopcar.getLines().size() == 0) {
%><script>alert('购物车为空');location.href="ListBookStore.jsp";</script> <%
}
}
if(shopcar.getLines().size()==0){
%><script>alert('购物车为空');location.href="ListBookStore.jsp";</script> <%
}
%>
<%
ArrayList<Line> lines = (ArrayList<Line>) shopcar.getLines();
for (int i = 0; i < lines.size(); i++) {
Line line = (Line) lines.get(i);
%>
<tr>
<td>
<%=line.getBook().getId()%>
</td>
<td>
<%=line.getBook().getName()%>
</td>
<td>
<%=line.getBook().getPrice()%>
</td>
<td>
<%=line.getNum()%>
</td>
</tr>
<%
}
%>
<%
double sum = 0;
for (int i = 0; i < lines.size(); i++) {
Line line = (Line) lines.get(i);
if (line.getNum() == 1) {
sum += line.getBook().getPrice();
} else {
sum += line.getBook().getPrice() * line.getNum();
}
}
%>
<tr>
<td colspan="5" align="right">
总价为:
<%=sum%>
</td>
</tr>
</table>
<table>
<tr>
<td>
<input type="submit" value="确认订单" οnclick="doSubmit();">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
BookStore.jsp
<%@ page language="java" import="java.util.*,bean.BookStore,bean.Book"
pageEncoding="gb2312"%>
<jsp:useBean id="store" scope="application" class="bean.BookStore" />
<script>
function doSubmit(type){
var num=0;
if(type=="buy"){
for(var i=0;i<form1.id.length;i++){
if(form1.id[i].checked){
num=num+1;
}
}
if(num==0){
alert('至少选择一个');
return;
}
}
if(type=="buy"){
form1.action="add";
}
else{
form1.action="list";
}
form1.submit();
}
</script>
<html>
<body bgcolor="silver">
<div align=center>
<font color="red">欢迎光临ESUN书店超人分店!</font>
<form method="post" name="form1">
<table border=1>
<tr>
<td width="10%">
</td>
<td width="50%">
书名
</td>
<td width="10%">
价格
</td>
<td width="30%">
数量
</td>
</tr>
<%int size=store.getBookStore().size();
for(int i=1;i<size;i++){
Book book=(Book)store.getBookStore().get(i);
%>
<tr>
<td>
<input type="radio" name="id" value="<%=book.getId() %>">
</td>
<td>
<%=book.getName()%>
</td>
<td><%=book.getPrice() %></td>
<td><input type="text" name="<%=book.getId() %>" value="1"></td>
</tr>
<%
} %>
</table>
<table>
<tr>
<td>
<input type="button" value=" 购买 " οnclick="doSubmit('buy');">
</td>
<td>
<input type="button" value="查看购物车" οnclick="doSubmit('list');">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
ListBook.jsp
<%@ page language="java" import="java.util.*,bean.ShopCar,bean.Line"
pageEncoding="gb2312"%>
<script>
function doBack(){
location.href="ListBookStore.jsp";
}
function doSubmit(){
location.href="confirm.jsp";
}
function doDel(){
if(form1.id.length==undefined){
if(form1.id.checked){
form1.action="del";
form1.submit();
}
else{
alert('选择一个');
}
}
else{
var num=0;
for(var i=0;i<form1.id.length;i++){
if(form1.id[i].checked){
num=num+1;
}
}
if(num==0){
alert('至少选择一个');
return;
}
form1.action="del";
form1.submit();
}
}
function doUpdate(){
if(form1.id.length==undefined){
if(form1.id.checked){
form1.action="update";
form1.submit();
}
else{
alert('选择一个');
}
}
else{
var num=0;
for(var i=0;i<form1.id.length;i++){
if(form1.id[i].checked){
num=num+1;
}
}
if(num==0){
alert('至少选择一个');
return;
}
form1.action="update";
form1.submit();
}
}
</script>
<html>
<body bgcolor="silver">
<div align=center>
<font color="red">购物车中的书籍明细</font>
<form method="post" name=form1>
<table border=1>
<tr>
<td width="10%">
</td>
<td width="50%">
书名
</td>
<td width="10%">
价格
</td>
<td width="30%">
数量
</td>
</tr>
<%
ShopCar shopcar = (ShopCar) session.getAttribute("ShopCar");
if(shopcar==null){
shopcar=new ShopCar();
}
ArrayList<Line> lines = (ArrayList<Line>) shopcar.getLines();
for (int i = 0; i < lines.size(); i++) {
Line line = (Line) lines.get(i);
%>
<tr>
<td>
<input type="radio" name="id" value="<%=line.getBook().getId()%>">
</td>
<td>
<%=line.getBook().getName()%>
</td>
<td>
<%=line.getBook().getPrice()%>
</td>
<td>
<input type="text" name="<%=line.getBook().getId()%>"
value=<%=line.getNum()%>>
</td>
</tr>
<%
}
%>
<%
double sum = 0;
for (int i = 0; i < lines.size(); i++) {
Line line = (Line) lines.get(i);
if (line.getNum() == 1) {
sum += line.getBook().getPrice();
} else {
sum += line.getBook().getPrice() * line.getNum();
}
}
%>
<tr>
<td colspan="5" align="right">
总和:
<%=sum%>
</td>
</tr>
</table>
<table>
<tr>
<td>
<input type="button" value=" 修改 " οnclick="doUpdate();">
</td>
<td>
<input type="button" value=" 删除 " οnclick="doDel();">
</td>
<td>
<input type="button" value=" 返回 " οnclick="doBack();">
</td>
<td>
<input type="button" value="提交订单" οnclick="doSubmit();">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>