1. 程式人生 > >jsp實現購物車

jsp實現購物車

以下是一個學習是做的小專案 購物模組的的購物車 為了省事沒用jstl

用的是JSP+SERVLET+JAVABEAN+DAO

---------------------------------------------------------------------
jSP
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*,shopbean.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>海鮮超市</title>
<% request.setCharacterEncoding("gbk"); %>
</head>
<body style="margin:0px">
<jsp:include page="/title.jsp"/><br>
<table align="middle">
<tr>
<td><% out.print("以下是你要購買的物品"+"<br>");%></td>
</tr>

<%
ArrayList<ShoppingBean> cart = (ArrayList<ShoppingBean>)session.getAttribute("cart");
if(cart != null)
{
for(Iterator it = cart.iterator();it.hasNext();){
ShoppingBean sbb = (ShoppingBean)it.next();
out.print("<tr>");
out.print("<td>"+"你購買的物品為"+sbb.getName()+"</td>");out.print("<td>"+"數量為:"+sbb.getCount()+"</td>");
out.print("</tr>");
}
}
else
{
response.sendRedirect("/shop/index.jsp");
}
%>
</table>
<a href="/shop/index.jsp">點選返回繼續購物</a>
</body>
</html>


---------------------------------------------------------------------
Bean類

package shopbean;
import java.io.Serializable;
public class ShoppingBean implements Serializable{

private static final long serialVersionUID = 8838427047488625882L;

private int id;
private String name;
private String station;
private String price;
private String blance;
private String sendman;
private String phone;
private String descritp;
private int count;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStation() {
return station;
}
public void setStation(String station) {
this.station = station;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getBlance() {
return blance;
}
public void setBlance(String blance) {
this.blance = blance;
}
public String getSendman() {
return sendman;
}
public void setSendman(String sendman) {
this.sendman = sendman;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDescritp() {
return descritp;
}
public void setDescritp(String descritp) {
this.descritp = descritp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}

----------------------------------------------------------------------

2.servlet

package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

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 shopbean.ShoppingBean;
import fishdao.BuyDao;

@SuppressWarnings("serial")
public class BuyAction extends HttpServlet {


@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String para = request.getParameter("para");
System.out.println(para);
ShoppingBean sb = new ShoppingBean();
sb.setName(para);
BuyDao bd = new BuyDao();
HttpSession session = request.getSession();
ShoppingBean wc = bd.getContext(sb);
ArrayList<ShoppingBean> cart = null;
if(para != null){
cart = (ArrayList<ShoppingBean>)session.getAttribute("cart");
if(session.getAttribute("cart") == null){
cart = new ArrayList<ShoppingBean>();
session.setAttribute("cart",cart);
}
}

boolean flag = false;
ShoppingBean s_sb = null;
for(Iterator it = cart.iterator();it.hasNext();){
ShoppingBean s_ssb = (ShoppingBean)it.next();
if(para.equals(s_ssb.getName()))
flag = true;
s_sb = s_ssb;
}
if(flag){
s_sb.setCount(s_sb.getCount()+1);
}
else
{
ShoppingBean sab = new ShoppingBean();
sab.setName(new String(para.getBytes("iso_8859-1")));
sab.setCount(1);
cart.add(wc);
}

response.sendRedirect("/shop/BuyCart.jsp");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}

--------------------------------------------------------------------
3.dao

package fishdao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import shopbean.ShoppingBean;


public class BuyDao {

private Connection conn = null;
private ResultSet rs = null;

public Connection getConnection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hxcs?useUnicode=true&characterEncoding=utf8","root", "root");
} catch (SQLException e) {
p(e.getMessage());
}catch (ClassNotFoundException e) {
p(e.getMessage());
}
return conn;
}

public ShoppingBean getContext(ShoppingBean wcb)
{
this.getConnection();
ShoppingBean wbb = null;
String sql = "select id,name,price,station,descript from shopping where name = ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,wcb.getName());
rs = ps.executeQuery();
while(rs.next()){
wbb = new ShoppingBean();
wbb.setId(rs.getInt(1));
wbb.setName(rs.getString(2));
wbb.setStation(rs.getString(3));
wbb.setPrice(rs.getString(4));
}
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeConn();
}
return wbb;
}

public void closeConn()
{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}

public void p(Object o)
{
System.out.println(o);
}
}