系统后台开发
一、主页
indexx.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<HEAD>
<!--<%@ include file="head.txt" %>-->
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
</HEAD>
<title>首页</title>
<CENTER>
<h1><font Size=4 color=pink>
欢迎光临“智多星”智能手机销售网
</font>
</h1>
</CENTER>
<meta charset="UTF-8">
<title>树形菜单</title>
<BODY>
<div class="dtree">
<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
<script type="text/javascript">
<!--
d = new dTree('d');
d.add(0,-1,'“智多星”智能手机销售网');
//d.add(2,0,'Node 2','example01.html');
//d.add(4,0,'Node 3','example01.html');
//d.add(5,3,'登录','example01.html');
//d.add(6,5,'Node 1.1.1.1','example01.html');
//d.add(7,0,'Node 4','example01.html');
d.add(2,0,'用户入口');
d.add(8,2,'注册','inputRegisterMess.jsp');
d.add(9,2,'登录','login.jsp');
d.add(3,0,'商品信息');
d.add(10,3,'浏览手机','lookMobile.jsp');
d.add(11,3,'查询手机','searMobile.jsp');
d.add(4,0,'查看购物车和订单');
d.add(12,4,'查看购物车','lookShoppingCar.jsp');
d.add(13,4,'查看订单','lookOrderForm.jsp');
d.add(5,0,'后台管理入口','login.html');
d.add(6,0,'退出','exitServlet');
d.add(7,0,'主页','indexx.jsp');
document.write(d);
//-->
</script>
</div>
<div class="zhi">
<img src="image/welcome.jpg" width=400 ></img>
<</div>
</BODY></HTML>
二、网站功能
1、用户注册
源代码:
inputRegisterMess.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<jsp:useBean id="userBean" class="mybean.data.Register" scope="request"/>
<HEAD><%@ include file="head.txt" %></HEAD>
<title>注册页面</title>
<HTML><BODY bgcolor=pink><Font size=2>
<div align="center">
<FORM action="registerServlet" method="post" name=form>
<table>
用户名由字母、数字、下划线构成,*注释的项必须填写。
<tr><td>*用户名称:</td><td><Input type=text name="logname" ></td>
<td>*用户密码:</td><td><Input type=password name="password">
</td></tr>
<tr><td>*重复密码:</td><td>
<Input type=password name="again_password"></td>
<td>联系电话:</td><td><Input type=text name="phone"></td></tr>
<tr><td>邮寄地址:</td><td><Input type=text name="address"></td>
<td>真实姓名:</td><td><Input type=text name="realname"></td>
<td><Input type=submit name="g" value="提交"></td> </tr>
</table>
</Form>
</div >
<div align="center">
<p> 注册反馈:
<jsp:getProperty name="userBean" property="backNews" />
<table border=3>
<tr><td>会员名称:</td>
<td><jsp:getProperty name="userBean" property="logname"/></td>
</tr>
<tr><td>姓名:</td>
<td><jsp:getProperty name="userBean" property="realname"/></td>
</tr>
<tr><td>地址:</td>
<td><jsp:getProperty name="userBean" property="address"/></td>
</tr>
<tr><td>电话:</td>
<td><jsp:getProperty name="userBean" property="phone"/></td>
</tr>
</table></div >
</Body></HTML>
register.java
package mybean.data;
public class Register{
String logname="" , phone="",
address="",realname="",backNews="请输入信息";
public void setLogname(String logname){
this.logname=logname;
}
public String getLogname(){
return logname;
}
public void setPhone(String phone){
this.phone=phone;
}
public String getPhone(){
return phone;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return address;
}
public void setRealname(String realname){
this.realname=realname;
}
public String getRealname(){
return realname;
}
public void setBackNews(String backNews){
this.backNews=backNews;
}
public String getBackNews(){
return backNews;
}
}
2、用户登录
源代码:
inputRegisterMess.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<jsp:useBean id="userBean" class="mybean.data.Register" scope="request"/>
<HEAD><%@ include file="head.txt" %></HEAD>
<title>注册页面</title>
<HTML><BODY bgcolor=pink><Font size=2>
<div align="center">
<FORM action="registerServlet" method="post" name=form>
<table>
用户名由字母、数字、下划线构成,*注释的项必须填写。
<tr><td>*用户名称:</td><td><Input type=text name="logname" ></td>
<td>*用户密码:</td><td><Input type=password name="password">
</td></tr>
<tr><td>*重复密码:</td><td>
<Input type=password name="again_password"></td>
<td>联系电话:</td><td><Input type=text name="phone"></td></tr>
<tr><td>邮寄地址:</td><td><Input type=text name="address"></td>
<td>真实姓名:</td><td><Input type=text name="realname"></td>
<td><Input type=submit name="g" value="提交"></td> </tr>
</table>
</Form>
</div >
<div align="center">
<p> 注册反馈:
<jsp:getProperty name="userBean" property="backNews" />
<table border=3>
<tr><td>会员名称:</td>
<td><jsp:getProperty name="userBean" property="logname"/></td>
</tr>
<tr><td>姓名:</td>
<td><jsp:getProperty name="userBean" property="realname"/></td>
</tr>
<tr><td>地址:</td>
<td><jsp:getProperty name="userBean" property="address"/></td>
</tr>
<tr><td>电话:</td>
<td><jsp:getProperty name="userBean" property="phone"/></td>
</tr>
</table></div >
</Body></HTML>
Login.java
package mybean.data;
import java.util.*;
public class Login {
String logname="",
backNews="未登录";
ArrayList<String> list;
public Login() {
list = new ArrayList<String>();
}
public void setLogname(String logname){
this.logname=logname;
if(!(list.contains(logname)))
list.add(logname);
}
public String getLogname(){
return logname;
}
public void setBackNews(String s) {
backNews=s;
}
public String getBackNews() {
return backNews;
}
public ArrayList<String> getArrayList() {
return list;
}
}
3、浏览手机
源代码:
lookMobile.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML><HEAD><%@ include file="head.txt" %></HEAD>
<BODY bgcolor=pink><font size=2>
<div align="center">
<h2>选择某类手机<br>分页显示这类手机</h2>
<% try { Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri="jdbc:mysql://127.0.0.1/mobileshop?"+
"user=root&password=666666&characterEncoding=gb2312";
Connection con;
Statement sql;
ResultSet rs;
try {
con=DriverManager.getConnection(uri);
sql=con.createStatement();
//读取mobileClassify表,获得分类:
rs=sql.executeQuery("SELECT * FROM mobileClassify ");
out.print("<form action='queryServlet' method ='post'>") ;
out.print("<select name='fenleiNumber'>") ;
while(rs.next()){
int id = rs.getInt(1);
String mobileCategory = rs.getString(2);
out.print("<option value ="+id+">"+mobileCategory+"</option>");
}
out.print("</select>");
out.print("<input type ='submit' value ='提交'>");
out.print("</form>");
con.close();
}
catch(SQLException e){
out.print(e);
}
%>
</div></font>
</BODY></HTML>
byPageShow.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="mybean.data.DataByPage" %>
<%@ page import="com.sun.rowset.*" %>
<jsp:useBean id="dataBean" class="mybean.data.DataByPage" scope="session"/>
<%@ include file="head.txt" %></HEAD>
<HTML><Body bgcolor=pink><center>
<BR>当前显示的内容是:
<table border=2>
<tr>
<th>手机标识号</th>
<th>手机名称</th>
<th>手机制造商</th>
<th>手机价格</th>
<th>查看详情</th>
<td><font color=blue>添加到购物车</font></td>
</tr>
<jsp:setProperty name="dataBean" property="pageSize" param="pageSize"/>
<jsp:setProperty name="dataBean" property="currentPage" param="currentPage"/>
<%
CachedRowSetImpl rowSet=dataBean.getRowSet();
if(rowSet==null) {
out.print("没有查询到结果,无法浏览");
return;
}
rowSet.last();
int totalRecord=rowSet.getRow();
out.println("全部记录数"+totalRecord); //全部记录数
int pageSize=dataBean.getPageSize(); //每页显示的记录数
int totalPages = dataBean.getTotalPages();
if(totalRecord%pageSize==0)
totalPages = totalRecord/pageSize;//总页数
else
totalPages = totalRecord/pageSize+1;
dataBean.setPageSize(pageSize);
dataBean.setTotalPages(totalPages);
if(totalPages>=1) {
if(dataBean.getCurrentPage()<1)
dataBean.setCurrentPage(dataBean.getTotalPages());
if(dataBean.getCurrentPage()>dataBean.getTotalPages())
dataBean.setCurrentPage(1);
int index=(dataBean.getCurrentPage()-1)*pageSize+1;
rowSet.absolute(index); //查询位置移动到currentPage页起始位置
boolean boo=true;
for(int i=1;i<=pageSize&&boo;i++) {
String number=rowSet.getString(1);
String name=rowSet.getString(2);
String maker=rowSet.getString(3);
String price=rowSet.getString(4);
String goods =
"("+number+","+name+","+maker+
","+price+")#"+price;//便于购物车计算价格,尾缀上"#价格值"
goods = goods.replaceAll("\\p{Blank}","");
String button="<form action='putGoodsServlet' method = 'post'>"+
"<input type ='hidden' name='java' value= "+goods+">"+
"<input type ='submit' value='放入购物车' ></form>";
String detail="<form action='showDetail.jsp' method = 'post'>"+
"<input type ='hidden' name='xijie' value= "+number+">"+
"<input type ='submit' value='查看细节' ></form>";
out.print("<tr>");
out.print("<td>"+number+"</td>");
out.print("<td>"+name+"</td>");
out.print("<td>"+maker+"</td>");
out.print("<td>"+price+"</td>");
out.print("<td>"+detail+"</td>");
out.print("<td>"+button+"</td>");
out.print("</tr>");
boo=rowSet.next();
}
}
%>
</table>
<BR>每页最多显示<jsp:getProperty name="dataBean" property="pageSize"/>条信息
<BR>当前显示第<Font color=blue>
<jsp:getProperty name="dataBean" property="currentPage"/>
</Font>页,共有
<Font color=blue><jsp:getProperty name="dataBean" property="totalPages"/>
</Font>页。
<Table>
<tr><td><FORM action="" method=post>
<Input type=hidden name="currentPage" value=
"<%=dataBean.getCurrentPage()-1 %>">
<Input type=submit name="g" value="上一页"></FORM></td>
<td><FORM action="" method=post>
<Input type=hidden name="currentPage"
value="<%=dataBean.getCurrentPage()+1 %>">
<Input type=submit name="g" value="下一页"></FORM></td></tr>
<tr><td> <FORM action="" method=post>
每页显示<Input type=text name="pageSize" value =2 size=3>
条记录<Input type=submit name="g" value="确定"></FORM></td>
<td> <FORM action="" method=post>
输入页码:<Input type=text name="currentPage" size=2 >
<Input type=submit name="g" value="提交"></FORM></td></tr>
</Table>
</Center>
</BODY></HTML>
showDetail.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="mybean.data.Login" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="loginBean" class="mybean.data.Login" scope="session"/>
<%@ include file="head.txt" %></HEAD>
<HTML><Body bgcolor=pink ><center>
<% if(loginBean==null){
response.sendRedirect("login.jsp");//重定向到登录页面
}
else {
boolean b =loginBean.getLogname()==null||
loginBean.getLogname().length()==0;
if(b)
response.sendRedirect("login.jsp");//重定向到登录页面
}
String mobileID = request.getParameter("xijie");
out.print("<th>产品号"+mobileID);
if(mobileID==null) {
out.print("没有产品号,无法查看细节");
return;
}
Connection con;
Statement sql;
ResultSet rs;
try { Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
String uri="jdbc:mysql://127.0.0.1/mobileshop";
try{
con=DriverManager.getConnection(uri,"root","");
sql=con.createStatement();
String cdn="SELECT * FROM mobileForm where mobile_version = '"+mobileID+"'";
rs=sql.executeQuery(cdn);
out.print("<table border=2>");
out.print("<tr>");
out.print("<th>产品号");
out.print("<th>名称");
out.print("<th>制造商");
out.print("<th>价格");
out.print("<th><font color=blue>放入购物车</font>");
out.print("</TR>");
String picture="welcome.jpg";
String detailMess="";
while(rs.next()){
String number=rs.getString(1);
String name=rs.getString(2);
String maker=rs.getString(3);
String price=rs.getString(4);
detailMess=rs.getString(5);
picture=rs.getString(6);
String goods =
"("+number+","+name+","+maker+
","+price+")#"+price;//便于购物车计算价格,尾缀上"#价格值"
goods = goods.replaceAll("\\p{Blank}","");
String button="<form action='putGoodsServlet' method = 'post'>"+
"<input type ='hidden' name='java' value= "+goods+">"+
"<input type ='submit' value='放入购物车' ></form>";
out.print("<tr>");
out.print("<td>"+number+"</td>");
out.print("<td>"+name+"</td>");
out.print("<td>"+maker+"</td>");
out.print("<td>"+price+"</td>");
out.print("<td>"+button+"</td>");
out.print("</tr>");
}
out.print("</table>");
out.print("产品详情:<br>");
out.println("<div align=center>"+detailMess+"<div>");
String pic ="<img src='image/"+picture+"' width=260 height=200 ></img>";
out.print(pic); //产片图片
con.close();
}
catch(SQLException exp){}
%>
</Center>
</BODY></HTML>
4、查询手机
(1)手机版本号
(2)手机名称
(3)手机价格
源代码:
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><HEAD><%@ include file="head.txt" %></HEAD>
<BODY bgcolor=pink><font size=2>
<div align="center">
<br>查询时可以输入手机的版本号或手机名称及价格。<br>
手机名称支持模糊查询。
<br>输入价格是在2个值之间的价格,格式是:价格1-价格2<br>
例如 3987-8976
<FORM action="searchByConditionServlet" Method="post" >
<br>输入查询信息:<Input type=text name="searchMess"><br>
<Input type =radio name="radio" value="mobile_version">手机版本号
<Input type =radio name="radio" value="mobile_name" checked="ok">手机名称
<Input type =radio name="radio" value="mobile_price">手机价格
<br><Input type=submit name="g" value="提交">
</Form>
</div>
</Font></BODY></HTML>
5、查看购物车
源代码:
lookshoppingcar.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="mybean.data.Login" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="loginBean" class="mybean.data.Login" scope="session"/>
<HTML><HEAD><%@ include file="head.txt" %></HEAD>
<BODY bgcolor=pink><font size=2>
<div align="center">
<% if(loginBean==null){
response.sendRedirect("login.jsp");//重定向到登录页面
}
else {
boolean b =loginBean.getLogname()==null||
loginBean.getLogname().length()==0;
if(b)
response.sendRedirect("login.jsp");//重定向到登录页面
}
LinkedList car =loginBean.getCar();
if(car==null)
out.print("<h2> 购物车没有物品.</h2>");
else {
Iterator<String> iterator=car.iterator();
StringBuffer buyGoods = new StringBuffer();
int n=0;
double priceSum =0;
out.print("购物车中的物品:<table border=2>");
while(iterator.hasNext()) {
String goods=iterator.next();
String showGoods="";
n++;
//购车车物品的后缀是“#价格数字",比如“iPhone手机价格3989 #3989”
int index=goods.lastIndexOf("#");
if(index!=-1){
priceSum+=Double.parseDouble(goods.substring(index+1));
showGoods = goods.substring(0,index);
}
buyGoods.append(n+":"+showGoods);
String del="<form action='deleteServlet' method = 'post'>"+
"<input type ='hidden' name='delete' value= "+goods+">"+
"<input type ='submit' value='删除' ></form>";
out.print("<tr><td>"+showGoods+"</td>");
out.print("<td>"+del+"</td></tr>");
}
out.print("</table>");
String orderForm = "<form action='buyServlet' method='post'>"+
" <input type ='hidden' name='buy' value= "+buyGoods+" >"+
" <input type ='hidden' name='price' value= "+priceSum+" >"+
"<input type ='submit' value='生成订单'></form>";
out.print(orderForm);
}
%>
</div></font>
</BODY></HTML>
6、查看订单
源代码:
lookOrderForm.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<jsp:useBean id="loginBean" class="mybean.data.Login" scope="session"/>
<%@ page import="java.sql.*" %>
<HTML><HEAD><%@ include file="head.txt" %></HEAD>
<BODY bgcolor=pink>
<div align="center">
<% if(loginBean==null){
response.sendRedirect("login.jsp");//重定向到登录页面
}
else {
boolean b =loginBean.getLogname()==null||
loginBean.getLogname().length()==0;
if(b)
response.sendRedirect("login.jsp");//重定向到登录页面
}
Connection con;
Statement sql;
ResultSet rs;
try{ Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e){}
try { String uri= "jdbc:mysql://127.0.0.1/mobileshop";
String user="root";
String password="666666";
con=DriverManager.getConnection(uri,user,password);
sql=con.createStatement();
String cdn=
"SELECT id,mess,sum FROM orderform where logname= '"+loginBean.getLogname()+"'";
rs=sql.executeQuery(cdn);
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>"+"订单号");
out.print("<th width=100>"+"信息");
out.print("<th width=100>"+"价格");
out.print("</TR>");
while(rs.next()){
out.print("<tr>");
out.print("<td >"+rs.getString(1)+"</td>");
out.print("<td >"+rs.getString(2)+"</td>");
out.print("<td >"+rs.getString(3)+"</td>");
out.print("</tr>") ;
}
out.print("</table>");
con.close();
}
catch(SQLException e){
out.print(e);
}
%>
</div">
</BODY></HTML>