本人现在北大青鸟学二期。。最近在学JSP。。
这是我最近用servlet写的购物车的例子,还不够完整希望高手给于指点![]()
package com.xaccp.ui;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xaccp.po.*;
import java.sql.*;
public class NetShop extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>NetShop</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
/**
* 从数据库中取值 并封装成PONETSHUOP类,再添加到arrayList对象里
*/
String sql="select * from netshop";
ArrayList arrayList=new ArrayList();
DbConnection db=new DbConnection();
try {
ResultSet rs = db.select(sql);
while (rs.next()){
PONetShop poNetShop=new PONetShop();
String name=rs.getString("name");
int number=rs.getInt("number");
String type=rs.getString("type");
int price=rs.getInt("price");
poNetShop.setName(name);
poNetShop.setNumber(number);
poNetShop.setType(type);
poNetShop.setPrice(price);
arrayList.add(poNetShop);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
out.println("<table width='800' border='1' align='center'>");
out.println("<tr bgcolor='#999999'>");
out.println("<td windth='200'>商品名称</td>");
out.println("<td windth='200'>商品数量</td>");
out.println("<td windth='200'>商品类型</td>");
out.println("<td windth='200'>商品售价</td>");
out.println("</tr>");
out.println("<tr bgcolor='#66CCFF'>");
out.println("<td colspan='5'> </td>");
out.println("</tr>");
/**
* 通过FOR循环把arrayList里的数据添加到表格中,并且每一行数据用FORM显示出来,便于向ACTION中传值
*/
for (int i = 0; i < arrayList.size(); i++) {
PONetShop poNetShop=(PONetShop)arrayList.get(i);
out.println("<form name='form"+i+"' action='shopaction' method='post'>");
out.println("<tr>");
out.println("<td windth='200'>"+poNetShop.getName()+"</td>");
out.println("<td windth='200'><input type='text' name='number' value='"+poNetShop.getNumber()+"'></td>");
out.println("<td windth='200'>"+poNetShop.getType()+"</td>");
out.println("<td windth='200'>"+poNetShop.getPrice()+"</td>");
out.println("<td windth='200'><input type='submit' value='购买'></td>");
out.println("</tr>");
out.println("</form>");
out.println("<tr bgcolor='#66CCFF'>");
out.println("<td colspan='5'> </td>");
out.println("</tr>");
/**
* 这两行是向ACTION中传name和number值,并不在表格中显示
*/
out.println("<input type='hidden' name='name' value='"+poNetShop.getName()+"'>");
out.println("<input type='hidden' name='price' value='"+poNetShop.getPrice()+"'>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
第二个servlet类
package com.xaccp.bussiness;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xaccp.po.POShop;
import com.xaccp.ui.ShoppingCar;
import com.xaccp.po.DbConnection;
import java.sql.*;
public class ShopAction extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
/**
* 从netshop中取值,并封装成poshop对象,便于向list中添值
*/
String name = request.getParameter("name");
int price = Integer.parseInt(request.getParameter("price"));
int number = Integer.parseInt(request.getParameter("number"));
POShop poShop = new POShop();
poShop.setGoodsName(name);
poShop.setGoodsNumber(number);
poShop.setGoodsPrice(price);
HttpSession session = request.getSession();
List list = (List) session.getAttribute("list");
/**
* 先要从数据库中取出商品的数量,用于和netshop中取出的number比较,判断顾客购买的数量是否超出库存
*/
String sql = "select * from netshop where name='" + name + "'";
DbConnection db = new DbConnection();
int netnumber=0;
try {
ResultSet rs = db.select(sql);
while (rs.next()) {
netnumber = rs.getInt("number");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
if ( number > netnumber) {
out.println("对不起!您购买的商品超出库存。页面将在5秒中后返回到购物页!");
response.setHeader("refresh","5;url='netshop'");
} else {
/**
* 如果没有超出库存,就判断顾客有没有购物车LIST,如果没有就NEW个新的。
* 如果有就判断顾客从netshop购买的商品是不是已经在购物车中出现,如果已经购买过就把商品的数量相加
* flag用于判断是不是在购物车中出现的状态
*/
int flag = 0;
if (list == null) {
list = new ArrayList();
} else {
for (int i = 0; i < list.size(); i++) {
POShop poShop1 = (POShop) list.get(i);
if (poShop1.getGoodsName().equals(poShop.getGoodsName())) {
poShop1.setGoodsNumber(poShop1.getGoodsNumber() +
poShop.getGoodsNumber());
flag = 1;
}
}
}
/**
* 如果没有在购物车中出现,就把封装的poshop对象添加到购物车list中
*/
if (flag == 0) {
list.add(poShop);
}
/**
* 修改数据库中购买后商品的数量
*/
session.setAttribute("list", list);
String sql2 = "update netshop set number=number-'" + number +
"' where name='" + name + "'";
db.upData(sql2);
response.sendRedirect("shoppingcar");
out.close();
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
第三个servlet
package com.xaccp.ui;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xaccp.po.POShop;
public class ShoppingCar extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>ShoppingCar</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<table width='100%' border='1'>"
+ "<tr bgcolor='#999999'>"
+ "<td>商品名称</td>"
+ "<td>商品价格</td>"
+ "<td>商品数量</td>"
+ "<td>修改</td>"
+ "<td>删除</td>"
+ "</tr>");
HttpSession session=request.getSession();
List list=(List) session.getAttribute("list");
for (int i = 0; i < list.size(); i++) {
POShop poShop=(POShop)list.get(i);
out.println("<form name='form"+i+"' action='updatacar' method='post'>");
out.println("<input type='hidden' name='name' value='"+poShop.getGoodsName()+"'>");
out.println("<tr>");
out.println("<td>"+poShop.getGoodsName()+"</td>");
out.println("<td>"+poShop.getGoodsPrice()+"</td>");
out.println("<td><input type='text' name='number' value='"+poShop.getGoodsNumber()+"'</td>");
out.println("<td><input type='submit' name='submit' value='修改'></td>");
out.println("<td><input type='submit' name='submit' value='删除'></td>");
out.println("</tr>");
out.println("</form>");
}
out.println("<tr>"
+ "<td colspan='5'><a href='netshop'>继续购买</a></td>"
+ "</tr>"
+ "</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
这是第四个servlet类
package com.xaccp.ui;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xaccp.po.POShop;
public class UpdataCar extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
List list=(List)session.getAttribute("list");
int number=Integer.parseInt(request.getParameter("number"));
String name=request.getParameter("name");
String submit=request.getParameter("submit");
if (submit.equals("修改")){
for (int i = 0; i < list.size(); i++) {
POShop poShop = (POShop) list.get(i);
if (poShop.getGoodsName().equals(name)) {
poShop.setGoodsNumber(number);
}
}
}else {
for (int i = 0; i < list.size(); i++) {
POShop poShop = (POShop) list.get(i);
if (poShop.getGoodsName().equals(name)) {
list.remove(i);
}
}
}
response.sendRedirect("shoppingcar");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
这四第五个类
package com.xaccp.po;
public class PONetShop {
private String name;
private int number;
private String type;
private int price;
public PONetShop() {
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setNumber(int number) {
this.number = number;
}
public void setPrice(int price) {
this.price = price;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public int getNumber() {
return number;
}
public int getPrice() {
return price;
}
}
这是第六个类
package com.xaccp.po;
import java.sql.*;
/**
* 数据库连接
*/
public class DbConnection {
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:aaa";
public DbConnection() {
}
/**
* 查询操作
*/
public ResultSet select(String sql) throws SQLException{
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, "sa", "");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
return rs;
} catch (SQLException ex) {
return null;
} catch (ClassNotFoundException ex) {
return null;
}
}
/**
* 增删改操作
*/
public void upData(String sql){
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, "sa", "");
Statement stat = conn.createStatement();
stat.executeQuery(sql);
stat.close();
conn.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
写的烂的话。你就P我吧。