《Internet开发技术》
课 程 设 计 报 告
设计题目: 网上购物系统
姓名: 学号:
班级:
设计起止时间:
一、需求分析
网上购物已经成为一种潮流,网上购物的方便、价格便宜、省时等优势使得越来越多的人喜欢上了这种购物方式,良好的购物网站使得其访问率居高不下,顾客络绎不绝,开发一款良好的购物系统软件是一件很有必要的事。下面是根据网上购物系统需求归纳出的文本需求说明:
1.管理员(管理员唯一,用户名及密码已确定都为admin)登录后可以查看用户的信息,删除用户;查看商品信息,当商品信息有误时,可修改商品信息(商品名称严禁修改),删除商品;添加新的货物,加入到货物表中,货物表基本信息:货物名、价格、种类、生产商/作者、商品信息、商品图品。
2.个人用户基本信息:用户名、性别、密码、地址、邮箱、联系方式。个人用户可以注册成为该系统的客户且用户名必须唯一。个人用户可以在不登录情况下直接查看商品信息,但不可添加到购物车中,必须登录后才有这个功能。
3.个人用户登录后可以添加商品到购物车中,购物车表基本信息:用户名、货物名、数量、总价。查看自己的购物车;删除购物车中的货物;修改购物车中商品的数量。
二、系统分析及设计
系统功能描述
1.管理员登录
管理员唯一指定,利用初始化用户名(admin)及密码(admin)。
2.货物管理
管理员登录后,可以查看商品基本信息;修改有误的商品信息;删除商品。
3.用户管理
管理员登录后,可以查看用户的基本信息;删除用户。
4.新货上架
管理员登录后,可以添加新的商品。
5.用户注册
购物需要先成为该系统客户才行,个人用户可以填写基本信息:用户名、性别、密码、地址、邮箱、联系方式申请注册,但系统中用户名必须唯一。
6.用户登录
用户注册成功后,可以登录该系统进行一些特殊操作,如:添加货物到购物车、伤处购物车商品。
7.浏览商品
浏览商品不需判断用户是否已登录该系统。
8.购物车管理
用户登录系统后,可查看购物车中商品;删除购物车中商品;修改购物车中商品的数量。
系统数据流图
顶层数据流图
0层数据流图
系统功能模块划分图
功能模块文件调用图
数据库的设计
表1-1 UserInfor(该表用来记录注册用户的个人信息)
表1-2 GoodsInfor(该表用来记录上架货物的商品信息)
表1-3 ShoppingInfor(该表用来记录用户的购物信息)
三、系统核心功能详细模块说明
1.数据库访问:
利用javaBean封装的数据库连接、访问操作,提供外部接口函数供其调用。
文件名:SqlOperation.java
核心源代码:
package cd;
import java.sql.*;
publicclass SqlOperation
{
Statement stm = null;
Connection con = null;
//注册驱动连接数据库,返回一个连接对象
public SqlOperation()
{
String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String conUrl = "jdbc:odbc:JSP";
try{
Class.forName(JDriver);
con = DriverManager.getConnection(conUrl);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
//执行插入、删除及更新操作
publicint executeUpdate(String sql)
{
int i = 0;
try{
stm = con.createStatement();
i = stm.executeUpdate(sql);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return i;
}
//执行查询操作,返回一个结果集对象
public ResultSet executeQuery(String sql)
{
ResultSet rs = null;
try{
stm = con.createStatement();
rs = stm.executeQuery(sql);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return rs;
}
//关闭各种连接
publicvoid close()
{
try{
stm.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2.用户浏览商品:
当用户点击“图书资料、家用电器、电脑用品、日用百货”中任何一项时,传参数click都不一样故主界面显示不同的都不尽相同,访问数据库将,将其符合条件的结果记录显示在主界面上。
文件名:Welcome.jsp
核心源代码(部分):
//当参数为空或者为“1”时,显示图书资料
if(request.getParameter("click")==null || request.getParameter("click").equals("1"))
{
//访问数据库,获取查询的结果集
ResultSet rs = sqlOperation.executeQuery("select * from GoodsInfor where Gkind='图书'");
int i = 0;
out.println("<table align=center>");
while(rs.next()) //将结果一表格的形式展现在主界面上
{
if(i%3==0)
out.println("<tr><td><table>");
else
out.println("<td><table align=center>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String picture = rs.getString("GPicture");
out.println("<tr align=center><td rowspan=3 width=80>"+
"<img src=picture/"+picture+".jpg height=100></img></td></tr>");
out.println("<tr align=left><td width=100> <a href=\"GoodsInfor.jsp?click=1&name="+name+
"\"><font face=黑体 color=#0000ff>"+name+"</font></td></tr>");
out.println("<tr align=left><td> <font face=黑体 color=#ff0000>¥:"+money+
" 元</font></td></tr>");
if((++i)%3==0)
out.println("</table></td></tr>");
else
out.println("</table></td>");
}
out.println("</table>");
}
3.购物车管理
当用户点击“我的购物车”时,可以查看自己购物车中商品,用户可以点击删除,修改商品的数量,显示效果有ShoppingCart.jsp来处理,由ShoppingCartDeal.java处理来自于ShoppingCart.jsp的操作请求,其实是对数据库的操作,这里使用了UseBean技术,后再利用ShoppingCart.jsp在将数据库中信息显示,保证了数据库文件与显示结果的一致性。
文件名:ShoppingCart.jsp
核心源代码(部分):
while(rs.next())
{
String name = rs.getString("Gname");
String price = rs.getString("Gmoney");
String kind = rs.getString("Gkind");
String producter = rs.getString("Gproducter");
String information = rs.getString("GInformation");
String picture = rs.getString("GPicture");
String num = rs.getString("Snum");
String totalmoney = rs.getString("Smoney");
allexpense += Integer.parseInt(totalmoney);
out.println("<hr width=540><table align=center><tr>");
if(picture.equals("暂无图片"))
out.println("<td rowspan=3><img src=\"picture/none.jpg\" height=80></img></td>");
else
out.println("<td rowspan=3><img src=\"picture/"+picture+".jpg\" height=80></img></td>");
out.println("<td><table>");
out.println("<tr><td width=150><font face=黑体 size=3>"+name+
"</font></td><td width=180><font face=黑体 size=3>单价:</font> "+
"<font face=黑体 size=3 color=red>"+price+"</font> <font face=黑体>元</font>"+
"</td><td align=left width=80><font face=黑体 size=3>"+kind+"</font></td></tr>");
out.println("<tr><td colspan=2><font size=2>"+information+
"</td><td align=center><a href=\"cd/ShoppingCartDeal?id=1"+
"&gname="+name+"\"><font face=幼圆 size=2 color=green>删除</font></a></td></tr>");
out.println("<tr><td><font face=楷体 size=4>"+producter+"</font></td><td>"+
"<a href=\"cd/ShoppingCartDeal?id=2&gname="+name+"&price="+price+"&num="+num+
"\"><img src=\"picture/jianhao.jpg\"></img></a>"+" <input type=text name=number size=3 "+
"style=\"vertical-align:top\" value="+num+" readonly> "+
"<a href=\"cd/ShoppingCartDeal?id=3&gname="+name+"&price="+price+"&num="+num+
"\"><img src=\"picture/jiahao.jpg\"></img></a></td><td><font face=黑体 size=3 color=red>"+
"¥: "+totalmoney+" 元</font></td></tr>");
out.println("</table></td></tr>");
out.println("</table><hr width=540>");
}
out.println("<p align=center><font face=黑体 size=5 color=#ff0000>小计("+
record_count+"种商品): "+allexpense+" 元</font></p>");
rs.close();
文件名:ShoppingCartDeal.java
核心源代码(部分):
publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
String sql = null;
SqlOperation sqlOperation = new SqlOperation();
String uname = request.getSession().getAttribute("account").toString();
String gname = new String(request.getParameter("gname").getBytes("ISO-8859-1"));
if(request.getParameter("id").equals("1"))
sql = new String("delete from ShoppingInfor where Uname='"+uname+"' and Gname='"+gname+"'");
else
{
int price = Integer.parseInt(request.getParameter("price"));
int num = Integer.parseInt(request.getParameter("num"));
if(request.getParameter("id").equals("2")&&num!=1)
num--;
if(request.getParameter("id").equals("3"))
num++;
int money = num*price;
sql = new String("update ShoppingInfor set Snum="+num+",Smoney="+money+" where Uname='"+
uname+"' and Gname='"+gname+"'");
}
if(sqlOperation.executeUpdate(sql)==0)
{
out.println("<html><head></head><body>");
out.println("<p> </p><p align=center><font face=楷体 size=5 color=green></font></p>");
out.println("</body></html>");
}
else
response.sendRedirect("../ShoppingCart.jsp");
sqlOperation.close();
}
4.商品管理
管理员登录后,可查看商品信息,修改商品信息,删除商品信息,显示有AdminOpearation.jsp来处理,操作请求由RequestDeal.java来处理,处理完毕后将结果写入数据库,后直接调用AdminOperation.jsp来显示更改后的结果。其中还使用了jsp动作加载文件。
文件名:AdminOpeartion.jsp
核心源代码(部分):
char a = request.getParameter("id").charAt(0);
if(a=='1')
{
int user_count = 0;
ResultSet rs = sqlOperation.executeQuery("select count(*) from UserInfor");
if(rs.next())
{
user_count = rs.getInt(1); rs.close();
}
if(user_count>0)
{
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>所有用户信息</font></p>");
int num = 0;
rs = sqlOperation.executeQuery("select * from UserInfor");
out.println("<table style=table-layout:fixed border=1 cellpadding=0 cellspacing=0 " +
"bordercolor=#808080>");
out.println("<tr align=center bgcolor=blue>" +
"<th width=40>编号</th>" +
"<th width=70>用户名</th>" +
"<th width=50>性别</th>" +
"<th width=70>密码</th>" +
"<th width=150>地址</th>" +
"<th width=160>邮箱</th>" +
"<th width=90>联系方式</th>" +
"<th width=60>操作</th></tr>");
while(rs.next())
{
if(num%2==0)
out.println("<tr align=center>");
else
out.println("<tr align=center bgcolor=#fefb94>");
String na = rs.getString("Uname");
out.println("<td>" + ++num + "</td>" +
"<td>" + na + "</td>" +
"<td>" + rs.getString("Usex") + "</td>" +
"<td>" + rs.getString("Upassword") + "</td>" +
"<td>" + rs.getString("Uaddress") + "</td>" +
"<td>" + rs.getString("Uemail") +"</td>" +
"<td>" + rs.getString("Utele") + "</td>");
out.println("<td><a href=\"cd/RequestDeal?id=1&name="+ na +
"\"><font face=黑体 size=2 color=green>删除</font></a></td></tr>");
}
out.println("</table>");
rs.close();
}
else
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>暂时无用户信息可查询</font></p>");
sqlOperation.close();
}
if(a=='2')
{
int goods_count = 0;
ResultSet rs = sqlOperation.executeQuery("select count(*) from GoodsInfor");
if(rs.next())
{
goods_count = rs.getInt(1); rs.close();
}
if(goods_count>0)
{
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>所有商品信息</font></p>");
int num = 0;
rs = sqlOperation.executeQuery("select * from GoodsInfor");
while(rs.next())
{
out.println("<table align=center><hr align=center width=620>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String kind = rs.getString("Gkind");
String producter = rs.getString("Gproducter");
String information = rs.getString("GInformation");
String picture = rs.getString("GPicture");
if(picture.equals("暂无图片"))
out.println("<tr><td rowspan=3><img src=picture/none.jpg height=60></img></td>");
else
out.println("<tr><td rowspan=3><img src=picture/"+picture+".jpg height=60></img></td>");
out.println("<td width=150><font face=黑体 size=3 color=#000080> "+name+"</font></td>");
out.println("<td width=100><font face=黑体 size=3 color=#000000>"+kind+"</font></td>");
out.println("<td width=100><font face=黑体 size=3 color=#ff0000>¥:"+money+" 元</font></td></tr>");
out.println("<tr><td width=350> 简介:<font size=2>"+information+"</font></td></tr>");
out.println("<tr><td width=150> "+producter+"</td>");
out.println("<td align=right><a href=\"ChangeGoodsInfor.jsp?name="+name+
"&money="+money+"&kind="+kind+"&producter="+producter+
"&information="+information+"&picture="+picture+
"&operation=修改\"><font face=黑体 size=2 color=green>修改</font></a>");
out.println(" <a href=\"cd/RequestDeal?id=2&name="+name+
"&operation=删除\"><font face=黑体 size=2 color=green>删除</font></a></td></tr>");
out.println("</table><hr align=center width=620>");
}
rs.close();
}
else
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>暂时没有商品信息</font></p>");
}
if(a=='3')
{
out.println("<p align=center><font face=楷体 size=5 color=green>请填写商品信息</font></p>");
%>
<jsp:include page="Goods.jsp" flush="true">
<jsp:param value="cd/RequestDeal?id=3" name="action"/>
</jsp:include>
<%
}
四、收获及体会
通过这次课程设计,我学习到了很多东西,感觉这次课程设计很有意义。利用javaBean封装数据库类,综合运用jsp、html、javaBean、Servlet等知识。使用html知识:插入文本框、文本域、按钮、图片,利用超链接实现文件之间的跳转,在表格中显示信息使其规格化,利用form表单提交信息。 使用jsp知识:利用jsp内置out、response、request、session等对象,利用request获取从另一个界面传来的参数,利用response的refresh函数定时刷新界面显示时间及sendRederict函数来跳转到其它文件中,使用session来获取一个session属性对象的值来判断用户是否登录在线,利用out对象来输出一些信息,使用jsp动作在合适时嵌入文件。 使用javaBean知识:自己封装一个数据库的连接和操作函数,在其它文件中可以定义其对象或者使用useBean技术来对数据库进行操作。 使用servlet知识:利用其doPost函数来处理其它文件提交来的信息。
此外,这次课程设计不仅仅是对课本中知识的综合应用,还学到了许多课外知识,如:超链接的传参问题,从框架中跳出到一般文件(这里使用了javasrcipt语言来完成了操作)。
课程设计中遇到的问题都不是特别的难于处理,不过在界面设计中确实花了不少的心思,就
主界面的显示效果想了好久,让商品的信息显示的整洁,给人一种美感实乃不易呀。本来想插入背景图片,无奈找到的背景图都太丑了,只好作罢。最后在这里感谢一下老师的悉心教导,祝老师工作顺利,身体健康!
附录:(实验效果截图)
图 1-1 主界面
图 1-2 点击“家用电器”后
图 1-3 点击“商品名称”显示商品详细信息
图 1-4 点击“加入购物车”后
图 1-5 管理员查看用户信息
图 1-6 管理员查看货物信息
图 1-7 管理员点击“新货上架”
附录源代码:
1.登录模块
1.1 文件名:Welcome.jsp
<%@page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<jsp:useBean id="sqlOperation" scope="page" class="cd.SqlOperation"></jsp:useBean>
<html>
<head><title>百货商城</title></head>
<body>
<font face="方正舒体" size=3 color="#009942">迷你挑,就在好商城</font>
<%
if(session.getAttribute("account")==null) //判断用户是否上线
out.println("<a href=Login.html style=text-decoration:none>" +
"<font size=2 color=blue>【管理员入口】</font></a> ");
else
out.println("  " +
" ");
%>
<font face="楷体" size=3 color="red">欢迎来到百货商城</font>
<%
//用户上线和用户不在线时,第一行的显示文字有所差异
if(session.getAttribute("account")==null)
{
out.println("<a href=Login.html style=text-decoration:none>" +
"<font size=2 color=blue>[登录]</font></a>");
out.println("<a href=Register.html style=text-decoration:none>" +
"<font size=2 color=blue>[免费注册]</font></a>");
}
else
{
out.println(session.getAttribute("account"));
out.println("<a href=cd/Quit style=text-decoration:none><font size=2 color=blue>[退出]</font></a>");
}
%>
<font color="Navy">--</font>
<a href="ShoppingCart.jsp"><font size=2 color=blue>我的购物车</font></a><br><hr><br>
<div align="center"><font face=楷体 size=5 color=#ff8b53>
<a href="Welcome.jsp?click=1">图书资料</a></font><br><hr width=600></div>
<%
//当参数为空或者为“1”时,显示图书资料
if(request.getParameter("click")==null || request.getParameter("click").equals("1"))
{
//访问数据库,获取查询的结果集
ResultSet rs = sqlOperation.executeQuery("select * from GoodsInfor where Gkind='图书'");
int i = 0;
out.println("<table align=center>");
while(rs.next()) //将结果一表格的形式展现在主界面上
{
if(i%3==0)
out.println("<tr><td><table>");
else
out.println("<td><table align=center>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String picture = rs.getString("GPicture");
out.println("<tr align=center><td rowspan=3 width=80>"+
"<img src=picture/"+picture+".jpg height=100></img></td></tr>");
out.println("<tr align=left><td width=100> <a href=\"GoodsInfor.jsp?click=1&name="+name+
"\"><font face=黑体 color=#0000ff>"+name+"</font></td></tr>");
out.println("<tr align=left><td> <font face=黑体 color=#ff0000>¥:"+money+
" 元</font></td></tr>");
if((++i)%3==0)
out.println("</table></td></tr>");
else
out.println("</table></td>");
}
out.println("</table>");
}
%>
<div align="center"><font face=楷体 size=5 color=#ff8b53>
<a href="Welcome.jsp?click=2">家用电器</a></font><br><hr width=600></div>
<%
//当参数不为空且参数为“2”时,显示家用电器信息
if(request.getParameter("click")!=null && request.getParameter("click").equals("2"))
{
ResultSet rs = sqlOperation.executeQuery("select * from GoodsInfor where Gkind='家用电器'");
int i = 0;
out.println("<table align=center>");
while(rs.next())
{
if(i%3==0)
out.println("<tr><td><table>");
else
out.println("<td><table align=center>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String picture = rs.getString("GPicture");
out.println("<tr align=center><td rowspan=3 width=80>"+
"<img src=picture/"+picture+".jpg height=100></img></td></tr>");
out.println("<tr align=left><td width=100> <a href=\"GoodsInfor.jsp?click=2&name="+name+
"\"><font face=黑体 color=#0000ff>"+name+"</font></td></tr>");
out.println("<tr align=left><td> <font face=黑体 color=#ff0000>¥:"+money+
" 元</font></td></tr>");
if((++i)%3==0)
out.println("</table></td></tr>");
else
out.println("</table></td>");
}
out.println("</table>");
}
%>
<div align="center"><font face=楷体 size=5 color=#ff8b53>
<a href="Welcome.jsp?click=3">电脑用品</a></font><br><hr width=600></div>
<%
//当参数不为空且参数为“3”时,显示电脑用品的信息
if(request.getParameter("click")!=null && request.getParameter("click").equals("3"))
{
ResultSet rs = sqlOperation.executeQuery("select * from GoodsInfor where Gkind='电脑用品'");
int i = 0;
out.println("<table align=center>");
while(rs.next())
{
if(i%3==0)
out.println("<tr><td><table>");
else
out.println("<td><table align=center>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String picture = rs.getString("GPicture");
out.println("<tr align=center><td rowspan=3 width=80>"+
"<img src=picture/"+picture+".jpg height=100></img></td></tr>");
out.println("<tr align=left><td width=100> <a href=\"GoodsInfor.jsp?click=3&name="+name+
"\"><font face=黑体 color=#0000ff>"+name+"</font></td></tr>");
out.println("<tr align=left><td> <font face=黑体 color=#ff0000>¥:"+money+
" 元</font></td></tr>");
if((++i)%3==0)
out.println("</table></td></tr>");
else
out.println("</table></td>");
}
out.println("</table>");
}
%>
<div align="center"><font face=楷体 size=5 color=#ff8b53>
<a href="Welcome.jsp?click=4">日用百货</a></font><br><hr width=600></div>
<%
//当参数不为空且参数为“4”时,显示日用百货信息
if(request.getParameter("click")!=null && request.getParameter("click").equals("4"))
{
ResultSet rs = sqlOperation.executeQuery("select * from GoodsInfor where Gkind='日用百货'");
int i = 0;
out.println("<table align=center>");
while(rs.next())
{
if(i%3==0)
out.println("<tr><td><table>");
else
out.println("<td><table align=center>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String picture = rs.getString("GPicture");
out.println("<tr align=center><td rowspan=3 width=100>"+
"<img src=picture/"+picture+".jpg height=100></img></td></tr>");
out.println("<tr align=left><td width=100> <a href=\"GoodsInfor.jsp?click=4&name="+name+
"\"><font face=黑体 color=#0000ff>"+name+"</font></td></tr>");
out.println("<tr align=left><td> <font face=黑体 color=#ff0000>¥:"+money+
" 元</font></td></tr>");
if((++i)%3==0)
out.println("</table></td></tr>");
else
out.println("</table></td>");
}
out.println("</table>");
}
%>
</body>
</html>
1.2 文件名:Login.html
<html>
<head><title>管理员</title></head>
<body>
<a href="Welcome.jsp"><font size=2 color="red">【返回首页】</font></a>
<form action="cd/CheckLoginInfor" method="post">
<p align="center"><font face="楷体" size=4 color="#000080">欢迎来到登录界面</font><br><br>
<table align="center">
<tr><td>用户名:</td><td><input type="text" name="userName"></td></tr>
<tr><td>密 码:</td><td><input type="password" name="password"></td></tr>
</table><br>
<input type="submit" value="确认">
<input type="reset" value="清除">
</form>
</body>
</html>
1.3 文件名:CheckLoginInfor.java
package cd;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class CheckLoginInfor extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
//当信息填写不完整时,直接跳到原界面
if(request.getParameter("userName").length()==0||
request.getParameter("password").length()==0)
{
response.sendRedirect("../Login.html");
}
byte b1[] = request.getParameter("userName").getBytes("ISO-8859-1");
String name = new String(b1);
byte b2[] = request.getParameter("password").getBytes("ISO-8859-1");
String password = new String(b2);
if(name.equals("admin"))
{
if(password.equals("admin"))
{
response.sendRedirect("../AdminLoad.html");
}
else
{
out.println("<html><head></head><body>");
out.println("<a href=" + "../Welcome.jsp" + "><font size=2 color=red>【返回首页】</font></a>");
out.println("<p align=center><font face=楷体 size=4 color=blue>密码输入有误!</font><br><br>");
out.println("<a href=" + "../Login.html" + "><font size=3 color=green>返回上一页</font></a></p>");
out.println("</body></html>");
}
}
else
{
SqlOperation so = new SqlOperation();
String sql = "select * from UserInfor where Uname='" + name + "' and Upassword='" + password + "'";
ResultSet rs = so.executeQuery(sql);
try {
if(rs.next())
{
rs.close();
so.close();
request.getSession().setAttribute("account",name.trim());
response.sendRedirect("../Welcome.jsp");
}
else
{
out.println("<html><head></head><body>");
out.println("<a href=" + "../Welcome.jsp" + "><font size=2 color=red>【返回首页】</font></a>");
out.println("<p align=center><font face=楷体 size=4 color=blue>用户名或者密码输入有误!</font><br><br>");
out.println("<a href=" + "../Login.html" + "><font size=3 color=green>返回上一页</font></a></p>");
out.println("</body></html>");
}
rs.close();
so.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}
2.注册模块
2.1 文件名:Register.html
<html>
<head><title>注册界面</title></head>
<body>
<a href="Welcome.jsp"><font size=2 color="red">【返回首页】</font></a>
<form action="cd/CheckRegisterInfor" method="post">
<table align="center">
<tr><td align="right">用户名:</td><td><input type="text" name="name"></td></tr>
<tr><td align="right">密码:</td><td><input type="password" name="password1"></td></tr>
<tr><td align="right">密码确认:</td><td><input type="password" name="password2"></td></tr>
<tr><td align="right">性别:</td>
<td align="center"><input type="radio" name="sexItem" value="男">男
<input type="radio" name="sexItem" value="女" checked>女</td></tr>
<tr><td align="right">地址:</td><td><input type="text" name="address"></td></tr>
<tr><td align="right">邮箱:</td><td><input type="text" name="email"></td></tr>
<tr><td align="right">联系方式:</td><td><input type="text" name="tele"></td></tr>
</table>
<p align="center"><input type="submit" value="确认">
<input type="reset" value="清除"></p>
</form>
</body>
</html>
2.2 文件名:CheckRegisterInfor.java
package cd;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class CheckRegisterInfor extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
if(request.getParameter("name").length()==0 ||
request.getParameter("password1").length()==0 ||
request.getParameter("password2").length()==0 ||
request.getParameter("address").length()==0 ||
request.getParameter("email").length()==0 ||
request.getParameter("tele").length()==0)
{
response.sendRedirect("../Register.html");
}
byte b1[] = request.getParameter("name").getBytes("ISO-8859-1");
String na = new String(b1);
byte b2[] = request.getParameter("password1").getBytes("ISO-8859-1");
String pa1 = new String(b2);
byte b3[] = request.getParameter("password2").getBytes("ISO-8859-1");
String pa2 = new String(b3);
byte b4[] = request.getParameter("sexItem").getBytes("ISO-8859-1");
String se = new String(b4);
byte b5[] = request.getParameter("address").getBytes("ISO-8859-1");
String ad = new String(b5);
byte b6[] = request.getParameter("email").getBytes("ISO-8859-1");
String em = new String(b6);
byte b7[] = request.getParameter("tele").getBytes("ISO-8859-1");
String te = new String(b7);
if(pa1.equals(pa2)==false)
{
out.println("<html><head></head><body>");
out.println("<a href=" + "../Welcome.jsp" + "><font size=2 color=red>" +
"【返回首页】</font></a>");
out.println("<p align=center><font face=楷体 size=4 color=blue>" +
"两次密码输入不一致!</font><br><br>");
out.println("<a href=" + "../Register.html" + "><font size=3 color=green>" +
"返回上一页</font></a></p>");
out.println("<html><head></head><body>");
return;
}
SqlOperation so = new SqlOperation();
String sql = "select * from UserInfor where Uname='" + na + "'";
ResultSet rs = so.executeQuery(sql);
try {
out.println("<html><head></head><body>");
out.println("<a href=" + "../Welcome.jsp" + "><font size=2 color=red>" +
"【返回首页】</font></a>");
if(rs.next())
{
out.println("<p align=center><font face=楷体 size=4 color=blue>" +
"该用户名已被占用!</font><br><br>");
out.println("<a href=" + "../Register.html" + "><font size=3 color=green>" +
"返回上一页</font></a></p>");
}
else
{
sql = "insert into UserInfor values('" + na + "','" + se +"','" + pa1 + "','" +
ad + "','" + em + "','" + te +"')";
int i = so.executeUpdate(sql);
if(i==0)
{
out.println("<p align=center><font face=楷体 size=4 color=blue>服务器忙," +
"未能注册成功,请您稍后再试!</font><br><br>");
out.println("<a href=" + "../Register.html" + "><font size=3 color=green>" +
"返回上一页</font></a></p>");
}
else
{
out.println("<p align=center><font face=楷体 size=4 color=blue>恭喜您," +
"注册成功!</font><br><br>");
out.println("<a href=" + "../Login.html" + "><font size=3 color=green>" +
"跳转到登录界面</font></a></p>");
}
}
out.println("</body></html>");
rs.close();
so.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}
3.管理员界面模块
3.1 文件名:AdminLoad.html
<html>
<head></head>
<frameset rows="22%,78%" framespacing="1">
<frame noresize src="Up.jsp">
<frameset cols="20%,80%">
<frame noresize name="select" src="Left.html">
<frame noresize name="display" src="">
</frameset>
</frameset>
</html>
3.2 文件名:Up.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.util.*" %>
<html>
<head>
<script type="text/javascript">
function jump()
{
if (top.location !== self.location){
top.location = "Welcome.jsp";
}
}
</script>
</head>
<body>
<p align= center><font face=楷体 size=5 color=#008040>欢迎管理员的到来</font></p>
<div align="right"><font face=楷体 size=3 color=#ff8000><%= new Date()%></font>
<a href="" onclick="jump()"><font size=2>[退出]</font></a></div>
<% response.setHeader("Refresh","6"); %>
</body>
</html>
3.3 文件名:Left.html
<html>
<head><base target="display"></head>
<body>
<p> </p>
<p align="center"><a href="AdminOperation.jsp?id=1">
<font face=楷体 size=4 color=blue>用户信息</font></a></p>
<p align="center"><a href="AdminOperation.jsp?id=2">
<font face=楷体 size=4 color=blue>商品信息</font></a></p>
<p align="center"><a href="AdminOperation.jsp?id=3">
<font face=楷体 size=4 color=blue>新货上架</font></a></p>
</body>
</html>
4.管理员操作模块
4.1 文件名:AdminOperation.jsp
<%@page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<jsp:useBean id="sqlOperation" scope="page" class="cd.SqlOperation"></jsp:useBean>
<html>
<head></head>
<body>
<%
char a = request.getParameter("id").charAt(0);
if(a=='1')
{
int user_count = 0;
ResultSet rs = sqlOperation.executeQuery("select count(*) from UserInfor");
if(rs.next())
{
user_count = rs.getInt(1); rs.close();
}
if(user_count>0)
{
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>所有用户信息</font></p>");
int num = 0;
rs = sqlOperation.executeQuery("select * from UserInfor");
out.println("<table style=table-layout:fixed border=1 cellpadding=0 cellspacing=0 " +
"bordercolor=#808080>");
out.println("<tr align=center bgcolor=blue>" +
"<th width=40>编号</th>" +
"<th width=70>用户名</th>" +
"<th width=50>性别</th>" +
"<th width=70>密码</th>" +
"<th width=150>地址</th>" +
"<th width=160>邮箱</th>" +
"<th width=90>联系方式</th>" +
"<th width=60>操作</th></tr>");
while(rs.next())
{
if(num%2==0)
out.println("<tr align=center>");
else
out.println("<tr align=center bgcolor=#fefb94>");
String na = rs.getString("Uname");
out.println("<td>" + ++num + "</td>" +
"<td>" + na + "</td>" +
"<td>" + rs.getString("Usex") + "</td>" +
"<td>" + rs.getString("Upassword") + "</td>" +
"<td>" + rs.getString("Uaddress") + "</td>" +
"<td>" + rs.getString("Uemail") +"</td>" +
"<td>" + rs.getString("Utele") + "</td>");
out.println("<td><a href=\"cd/RequestDeal?id=1&name="+ na +
"\"><font face=黑体 size=2 color=green>删除</font></a></td></tr>");
}
out.println("</table>");
rs.close();
}
else
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>暂时无用户信息可查询</font></p>");
sqlOperation.close();
}
if(a=='2')
{
int goods_count = 0;
ResultSet rs = sqlOperation.executeQuery("select count(*) from GoodsInfor");
if(rs.next())
{
goods_count = rs.getInt(1); rs.close();
}
if(goods_count>0)
{
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>所有商品信息</font></p>");
int num = 0;
rs = sqlOperation.executeQuery("select * from GoodsInfor");
while(rs.next())
{
out.println("<table align=center><hr align=center width=620>");
String name = rs.getString("Gname");
String money = rs.getString("Gmoney");
String kind = rs.getString("Gkind");
String producter = rs.getString("Gproducter");
String information = rs.getString("GInformation");
String picture = rs.getString("GPicture");
if(picture.equals("暂无图片"))
out.println("<tr><td rowspan=3><img src=picture/none.jpg height=60></img></td>");
else
out.println("<tr><td rowspan=3><img src=picture/"+picture+".jpg height=60></img></td>");
out.println("<td width=150><font face=黑体 size=3 color=#000080> "+name+"</font></td>");
out.println("<td width=100><font face=黑体 size=3 color=#000000>"+kind+"</font></td>");
out.println("<td width=100><font face=黑体 size=3 color=#ff0000>¥:"+money+" 元</font></td></tr>");
out.println("<tr><td width=350> 简介:<font size=2>"+information+"</font></td></tr>");
out.println("<tr><td width=150> "+producter+"</td>");
out.println("<td align=right><a href=\"ChangeGoodsInfor.jsp?name="+name+
"&money="+money+"&kind="+kind+"&producter="+producter+
"&information="+information+"&picture="+picture+
"&operation=修改\"><font face=黑体 size=2 color=green>修改</font></a>");
out.println(" <a href=\"cd/RequestDeal?id=2&name="+name+
"&operation=删除\"><font face=黑体 size=2 color=green>删除</font></a></td></tr>");
out.println("</table><hr align=center width=620>");
}
rs.close();
}
else
out.println("<p align=center><font face=楷体 size=5 color=#00FF00>暂时没有商品信息</font></p>");
}
if(a=='3')
{
out.println("<p align=center><font face=楷体 size=5 color=green>请填写商品信息</font></p>");
%>
<jsp:include page="Goods.jsp" flush="true">
<jsp:param value="cd/RequestDeal?id=3" name="action"/>
</jsp:include>
<%
}
%>
</body>
</html>
4.2 文件名:ChangeGoodsInfor.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%!String Gkinds[] = {"图书","电脑用品","家用电器","日用百货"}; %>
<%
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"));
String money = new String(request.getParameter("money").getBytes("ISO-8859-1"));
String kind = new String(request.getParameter("kind").getBytes("ISO-8859-1"));
String producter = new String(request.getParameter("producter").getBytes("ISO-8859-1"));
String information = new String(request.getParameter("information").getBytes("ISO-8859-1"));
String picture = new String(request.getParameter("picture").getBytes("ISO-8859-1"));
%>
<html>
<head></head>
<body>
<form action="cd/RequestDeal?id=2&operation=修改" method="post">
<table align="center">
<tr><td align="right">商品名:</td>
<td><input type="text" name="Gname" size=30 value=<%=name%> readonly><font color=red> *</font></td></tr>
<tr><td align="right">价格:</td>
<td><input onkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false"
style="ime-mode:Disabled" name="Gmoney" size=9 value=<%=money%>> 元<font color=red> *</font></td></tr>
<tr><td align="right">类别:</td>
<td><select name="Gkind">
<%
for(int i=0; i<4; i++){
if(kind.equals(Gkinds[i]))
out.println("<option selected>"+Gkinds[i]);
else
out.println("<option>"+Gkinds[i]);
}
%>
</select></td></tr>
<tr><td align="right">生产商/作者:</td>
<td><input type="text" name="Gproducter" value=<%=producter%>><font color=red> *</font></td></tr>
<tr><td align="right">图片名称</td><td><input type="text" name="Gpicture" value=<%=picture%>></td></tr>
<tr><td align="right">商品描述:</td><td><textarea name="GInformation" rows=5 cols=30><%=information%></textarea></td></tr>
</table>
<p align=center><input type="submit" value="确认"> <input type="reset" value="清除"></p>
</form>
</body>
</html>
4.3.文件名:Goods.jsp
<%@page contentType="text/html; charset=gb2312"%>
<html>
<head></head>
<body>
<form action=<%=request.getParameter("action")%> method="post">
<table align="center">
<tr><td align="right">商品名:</td>
<td><input type="text" name="Gname" size=30><font color=red> *</font></td></tr>
<tr><td align="right">价格:</td>
<td><input onkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false"
style="ime-mode:Disabled" name="Gmoney" size=9 value=0> 元<font color=red> *</font></td></tr>
<tr><td align="right">类别:</td>
<td><select name="Gkind"><option>图书<option>家用电器<option selected>电脑用品<option>日用百货</select></td></tr>
<tr><td align="right">生产商/作者:</td>
<td><input type="text" name="Gproducter"><font color=red> *</font></td></tr>
<tr><td align="right">图片名称</td><td><input type="text" name="Gpicture"></td></tr>
<tr><td align="right">商品描述:</td><td><textarea name="GInformation" rows=5 cols=30></textarea></td></tr>
</table>
<p align=center><input type="submit" value="确认"> <input type="reset" value="清除"></p>
</form>
</body>
</html>
4.4.文件名:RequestDeal.java
package cd;
import java.awt.List;
import java.io.*;
import java.sql.*;
import java.util.Collection;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestDeal extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
SqlOperation sqlOperation = new SqlOperation();
int bOk = 0;
if(request.getParameter("id").equals("1"))
{
byte b[] = request.getParameter("name").getBytes("ISO-8859-1");
String name = new String(b);
String sql = "delete from UserInfor where Uname='"+name+"'";
bOk = sqlOperation.executeUpdate(sql);
sqlOperation.close();
response.sendRedirect("../AdminOperation.jsp?id=1");
}
if(request.getParameter("id").equals("2"))
{
byte b2[] = request.getParameter("operation").getBytes("ISO-8859-1");
String operation = new String(b2);
if(operation.equals("删除"))
{
byte b1[] = request.getParameter("name").getBytes("ISO-8859-1");
String name = new String(b1);
String sql = "delete from GoodsInfor where Gname='"+name+ "'";
bOk = sqlOperation.executeUpdate(sql);
sqlOperation.close();
response.sendRedirect("../AdminOperation.jsp?id=2");
}
else
{
String name = new String(request.getParameter("Gname").getBytes("ISO-8859-1"));
String money = new String(request.getParameter("Gmoney").getBytes("ISO-8859-1"));
String kind = new String(request.getParameter("Gkind").getBytes("ISO-8859-1"));
String producter = new String(request.getParameter("Gproducter").getBytes("ISO-8859-1"));
String information = new String(request.getParameter("GInformation").getBytes("ISO-8859-1"));
String picture = new String(request.getParameter("Gpicture").getBytes("ISO-8859-1"));
SqlOperation sqlOpeartion = new SqlOperation();
String sql = "update GoodsInfor set Gmoney="+Integer.parseInt(money)+",Gkind='"+
kind+"',Gproducter='"+producter+"',GInformation='"+information+"',GPicture='"+
picture+"' where Gname='"+name+"'";
bOk = sqlOpeartion.executeUpdate(sql);
out.println("<html><head></head><body>");
if(bOk==0)
out.println("<p> </p><p align=center><font face=楷体 size=5 color=#00ff00>"+
"操作失败,请您稍后重试!</font></p>");
else
out.println("<p> </p><p align=center><font face=楷体 size=5 color=#00ff00>"+
"操作成功!</font></p>");
out.println("</body></html>");
sqlOperation.close();
}
}
if(request.getParameter("id").equals("3"))
{
if(request.getParameter("Gname").length()==0||
Integer.parseInt(request.getParameter("Gmoney"))<=0||
request.getParameter("Gproducter").length()==0)
{
response.sendRedirect("../AdminOperation.jsp?id=3");
}
byte b1[] = request.getParameter("Gname").getBytes("ISO-8859-1");
String name = new String(b1);
int money = Integer.parseInt(request.getParameter("Gmoney"));
byte b3[] = request.getParameter("Gkind").getBytes("ISO-8859-1");
String kind = new String(b3);
byte b4[] = request.getParameter("Gproducter").getBytes("ISO-8859-1");
String producter = new String(b4);
byte b5[] = request.getParameter("Gpicture").getBytes("ISO-8859-1");
String picture = new String(b5);
byte b6[] = request.getParameter("GInformation").getBytes("ISO-8859-1");
String information = new String(b6);
if(request.getParameter("Gpicture").length()==0)
picture = new String("暂无图片");
if(request.getParameter("GInformation").length()==0)
information = new String("暂无描述信息");
String sql = "insert into GoodsInfor values('"+name+"',"+money+",'"+kind+"','"+producter +
"','"+information+"','"+picture+"')";
bOk = sqlOperation.executeUpdate(sql);
out.println("<html><head></head><body>");
out.println("<p> </p>");
if(bOk==0)
out.println("<p align=center>" +
"<font face=楷体 size=5 color=#00FF00>新货未能添加成功</font><br><br>"+
"<a href=\"../AdminOperation.jsp?id=3\"><font face=楷体 size=4 color=blue>" +
"返回上一页</a></p>");
else
out.println("<p align=center>" +
"<font face=楷体 size=5 color=#00FF00>商品上架成功</font><br><br>"+
"<a href=\"../AdminOperation.jsp?id=3\"><font face=楷体 size=4 color=blue>" +
"继续添加</a></p>");
out.println("</body></html>");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}
5.购物模块
5.1 文件名:CheckAddToCart.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<jsp:useBean id="sqlOperation" scope="page" class="cd.SqlOperation"></jsp:useBean>
<html>
<head></head>
<body>
<a href="Welcome.jsp"><font size=2 color="red">【返回首页】</font></a>
<%
if(session.getAttribute("account")==null)
out.println("<p align=center><font face=楷体 size=5 color=green>您尚未登录!</font><br><br>"+
"<a href=Login.html><font face=楷体 size=4 color=blue>跳转至登录界面>>></font></a></p>");
else
{
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"));
int num = Integer.parseInt(request.getParameter("num"));
String username = new String(session.getAttribute("account").toString());
int totalmoney = Integer.parseInt(request.getParameter("totalmoney"));
int bOk = 0;
String strSql = "select * from ShoppingInfor where Uname='"+username+"' and Gname='"+name+"'";
ResultSet rs = sqlOperation.executeQuery(strSql);
if(rs.next())
{
int number = rs.getInt("Snum")+num;
int money = rs.getInt("Smoney")+totalmoney;
rs.close();
String sql = "update ShoppingInfor set Snum="+number+",Smoney="+money+
" where Uname='"+username+"' and Gname='"+name+"'";
bOk = sqlOperation.executeUpdate(sql);
}
else
{
String sql = "insert into ShoppingInfor values('"+username+"','"+name+"',"+num+
","+totalmoney+")";
bOk = sqlOperation.executeUpdate(sql);
}
if(bOk==0)
out.println("<p align=center><font face=楷体 size=5 color=green>操作失败!</font><br><br>"+
"<a href=\"GoodsInfor.jsp?choice=2&click="+request.getParameter("click")+
"&name="+name+"&num="+(num-1)+"\"><font face=楷体 size=4 color=blue><<<跳转至上一页"+
"</font></a></p>");
else
response.sendRedirect("ShoppingCart.jsp");
}
%>
</body>
</html>
5.2 文件名:ShoppingCart.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*" %>
<jsp:useBean id="sqlOperation" scope="page" class="cd.SqlOperation"/>
<%
out.println("<html><head></head><body>");
out.println("<a href=" + "Welcome.jsp" + "><font size=2 color=red>【返回首页】</font></a>");
if(session.getAttribute("account")==null)
{
out.println("<p align=center><font face=楷体 size=4 color=blue>您还未登录,请登录后再点击!</font><br><br>");
out.println("<a href=" + "Login.html" + "><font size=3 color=green>跳转至登录界面</font></a></p>");
}
else
{
int record_count = 0;
String sql = "select count(*) from ShoppingInfor where Uname='"+session.getAttribute("account")+"'";
ResultSet rs = sqlOperation.executeQuery(sql);
if(rs.next())
{ record_count=rs.getInt(1); rs.close(); }
if(record_count>0)
{
int allexpense = 0;
out.println("<p align=center><font face=楷体 size=5 color=#00ff00>购物车所有商品</font></p>");
String strSql = "select GoodsInfor.*,Snum,Smoney from ShoppingInfor,GoodsInfor where GoodsInfor.Gname="+
"ShoppingInfor.Gname and ShoppingInfor.Uname='"+session.getAttribute("account")+"'";
rs = sqlOperation.executeQuery(strSql);
while(rs.next())
{
String name = rs.getString("Gname");
String price = rs.getString("Gmoney");
String kind = rs.getString("Gkind");
String producter = rs.getString("Gproducter");
String information = rs.getString("GInformation");
String picture = rs.getString("GPicture");
String num = rs.getString("Snum");
String totalmoney = rs.getString("Smoney");
allexpense += Integer.parseInt(totalmoney);
out.println("<hr width=540><table align=center><tr>");
if(picture.equals("暂无图片"))
out.println("<td rowspan=3><img src=\"picture/none.jpg\" height=80></img></td>");
else
out.println("<td rowspan=3><img src=\"picture/"+picture+".jpg\" height=80></img></td>");
out.println("<td><table>");
out.println("<tr><td width=150><font face=黑体 size=3>"+name+
"</font></td><td width=180><font face=黑体 size=3>单价:</font> "+
"<font face=黑体 size=3 color=red>"+price+"</font> <font face=黑体>元</font>"+
"</td><td align=left width=80><font face=黑体 size=3>"+kind+"</font></td></tr>");
out.println("<tr><td colspan=2><font size=2>"+information+
"</td><td align=center><a href=\"cd/ShoppingCartDeal?id=1"+
"&gname="+name+"\"><font face=幼圆 size=2 color=green>删除</font></a></td></tr>");
out.println("<tr><td><font face=楷体 size=4>"+producter+"</font></td><td>"+
"<a href=\"cd/ShoppingCartDeal?id=2&gname="+name+"&price="+price+"&num="+num+
"\"><img src=\"picture/jianhao.jpg\"></img></a>"+" <input type=text name=number size=3 "+
"style=\"vertical-align:top\" value="+num+" readonly> "+
"<a href=\"cd/ShoppingCartDeal?id=3&gname="+name+"&price="+price+"&num="+num+
"\"><img src=\"picture/jiahao.jpg\"></img></a></td><td><font face=黑体 size=3 color=red>"+
"¥: "+totalmoney+" 元</font></td></tr>");
out.println("</table></td></tr>");
out.println("</table><hr width=540>");
}
out.println("<p align=center><font face=黑体 size=5 color=#ff0000>小计("+
record_count+"种商品): "+allexpense+" 元</font></p>");
rs.close();
}
else
{
out.println("<p align=center><font face=楷体 size=5 color=#00ff00>购物车为空</font></p>");
out.println("<p align=center><a href=Welcome.jsp><font size=3 color=#0000ff>快去选购吧!</font></a></p>");
}
}
out.println("</body></html>");
%>
5.3.文件名:ShoppingCartDeal.java
package cd;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShoppingCartDeal extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
String sql = null;
SqlOperation sqlOperation = new SqlOperation();
String uname = request.getSession().getAttribute("account").toString();
String gname = new String(request.getParameter("gname").getBytes("ISO-8859-1"));
if(request.getParameter("id").equals("1"))
sql = new String("delete from ShoppingInfor where Uname='"+uname+"' and Gname='"+gname+"'");
else
{
int price = Integer.parseInt(request.getParameter("price"));
int num = Integer.parseInt(request.getParameter("num"));
if(request.getParameter("id").equals("2")&&num!=1)
num--;
if(request.getParameter("id").equals("3"))
num++;
int money = num*price;
sql = new String("update ShoppingInfor set Snum="+num+",Smoney="+money+" where Uname='"+
uname+"' and Gname='"+gname+"'");
}
if(sqlOperation.executeUpdate(sql)==0)
{
out.println("<html><head></head><body>");
out.println("<p> </p><p align=center><font face=楷体 size=5 color=green></font></p>");
out.println("</body></html>");
}
else
response.sendRedirect("../ShoppingCart.jsp");
sqlOperation.close();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}
6.数据库操作模块
6.1 文件名:SqlOperation.java
package cd;
import java.sql.*;
publicclass SqlOperation
{
Statement stm = null;
Connection con = null;
//注册驱动连接数据库,返回一个连接对象
public SqlOperation()
{
String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String conUrl = "jdbc:odbc:JSP";
try{
Class.forName(JDriver);
con = DriverManager.getConnection(conUrl);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
//执行插入、删除及更新操作
publicint executeUpdate(String sql)
{
int i = 0;
try{
stm = con.createStatement();
i = stm.executeUpdate(sql);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return i;
}
//执行查询操作,返回一个结果集对象
public ResultSet executeQuery(String sql)
{
ResultSet rs = null;
try{
stm = con.createStatement();
rs = stm.executeQuery(sql);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return rs;
}
//关闭各种连接
publicvoid close()
{
try{
stm.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
7.退出模块
7.1 文件名:Quit.java
package cd;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Quit extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter();
request.getSession().setAttribute("account",null);
response.sendRedirect("../Welcome.jsp");
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}
}