web开发技术实验报告---前台部分
程序员文章站
2022-05-05 16:33:50
...
web开发---购物系统前台部分
系统所涉及文件展示
主文件:
beans包:
servlets包:
webContent文件夹:
web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>registerServlet</servlet-name>
<servlet-class>servlets.HandleRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerServlet</servlet-name>
<url-pattern>/registerServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>servlets.HandleLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteServlet</servlet-name>
<servlet-class>servlets.HandleDelete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteServlet</servlet-name>
<url-pattern>/deleteServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>buyServlet</servlet-name>
<servlet-class>servlets.HandleBuyGoods</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>buyServlet</servlet-name>
<url-pattern>/buyServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>queryServlet</servlet-name>
<servlet-class>servlets.QueryAllRecord</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>queryServlet</servlet-name>
<url-pattern>/queryServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>putGoodsServlet</servlet-name>
<servlet-class>servlets.PutGoodsToCar</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>putGoodsServlet</servlet-name>
<url-pattern>/putGoodsServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>searchByConditionServlet</servlet-name>
<servlet-class>servlets.SearchByCondition</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>searchByConditionServlet</servlet-name>
<url-pattern>/serachByConditionServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>exitServlet</servlet-name>
<servlet-class>servlets.HandleExit</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exitServlet</servlet-name>
<url-pattern>/exitServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>manageLogin</servlet-name>
<servlet-class>servlets.HandleManageLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>manageLogin</servlet-name>
<url-pattern>/manageLoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>userManagement</servlet-name>
<servlet-class>servlets.HandleUserManagement</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userManagement</servlet-name>
<url-pattern>/userManagementServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>orderManagement</servlet-name>
<servlet-class>servlets.HandleOrderManagement</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>orderManagement</servlet-name>
<url-pattern>/orderManagementServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mobileClassifyManagement</servlet-name>
<servlet-class>servlets.HandleMobileClassify</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mobileClassifyManagement</servlet-name>
<url-pattern>/MobileClassifyManagementServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>productManagement</servlet-name>
<servlet-class>servlets.HandleProductManagement</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>productManagement</servlet-name>
<url-pattern>/productManagementServlet</url-pattern>
</servlet-mapping>
</web-app>
前台
前台主页模块
效果展示:
涉及源代码:
//---index.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>首页</title>
</head>
<body>
<%@ include file="head.txt" %>
<center><h1><font size=4 color=blue>欢迎光临“智多星”智能手机销售网</font></h1>
<img src="image/welcome.jpg" width=500 height=400></img></center>
</body>
</html>
---head.txt
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<div align="center">
<H2>"智多星"智能手机销售网</H2>
<table cellSpcing="1" cellPadding="1" width="660" align="center" border="0">
<tr valign="bottom">
<td><A href="inputRegisterMess.jsp"><font size=2>注册</font></A></td>
<td><A href="login.jsp"><font size=2>登录</font></A></td>
<td><A href="lookMobile.jsp"><font size=2>浏览手机</font></A></td>
<td><A href="searMobile.jsp"><font size=2>查询手机</font></A></td>
<td><A href="lookShoppingCar.jsp"><font size=2>查看购物车</font></A></td>
<td><A href="lookOrderForm.jsp"><font size=2>查看订单</font></A></td>
<td><A href="exitServlet"><font size=2>退出</font></A></td>
<td><A href="index.jsp"><font size=2>主页</font></A></td>
<td><A href="manageLogin.jsp"><font size=2>管理员登录</font></A></td>
</tr>
</table>
</div>
前台注册模块
效果展示:
功能测试:
~~~注册成功
~~~注册失败
涉及源代码:
//---inputRegisterMess.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<jsp:useBean id="userBean" class="beans.Register" scope="request" />
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>注册页面</title>
</head>
<body>
<%@ include file="head.txt" %>
<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=text name="password"></td>
</tr>
<tr>
<td>*重复密码:</td><td><Input type=text 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>
</font>
</body>
</html>
//---beans.Register
package beans;
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;
}
}
//---servlet.HandleRegister.java
package servlets;
import beans.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleRegister extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
}
public String handleString(String s) {
try {byte bb[] =s.getBytes("iso-8859-1");
s=new String(bb);
}catch(Exception e) {e.printStackTrace();}
return s;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
Connection con;
PreparedStatement sql;
Register userBean=new Register();
request.setAttribute("userBean", userBean);
String logname=request.getParameter("logname").trim();
String password=request.getParameter("password").trim();
String again_password=request.getParameter("again_password").trim();
String phone=request.getParameter("phone").trim();
String address=handleString(request.getParameter("address").trim());
String realname=handleString(request.getParameter("realname").trim());
if(logname==null) logname="";
if(password==null) password="";
if(!password.equals(again_password)) {
userBean.setBackNews("两次密码不同,注册失败");
RequestDispatcher dispatcher=request.getRequestDispatcher("inputRegisterMess.jsp");
dispatcher.forward(request,response);
return;
}
boolean isLD=true;
for(int i=0;i<logname.length();i++) {
char c=logname.charAt(i);
if(!((c<='z'&&c>='a')||(c<='Z'&&c>='A')||(c<='9'&&c>='0'))) isLD=false;
}
boolean boo=logname.length()>0&&password.length()>0&&isLD;
String backNews="";
try {
con=DriverManager.getConnection(uri,"root","123456");
String insertCondition="Insert into user values(?,?,?,?,?)";
sql=con.prepareStatement(insertCondition);
if(boo) {
sql.setString(1, handleString(logname));
sql.setString(2, handleString(password));
sql.setString(3, handleString(phone));
sql.setString(4, address);
sql.setString(5, realname);
int m=sql.executeUpdate();
if(m!=0) {
backNews="注册成功";
userBean.setBackNews(backNews);
userBean.setAddress(address);
userBean.setLogname(logname);
userBean.setRealName(realname);
userBean.setPhone(phone);
}
}
else {backNews="信息不完整或名称含有非法字符";
userBean.setBackNews(backNews);
}
con.close();
}catch(SQLException exp) {
backNews="该会员已经被使用,请更换名称"+exp;
userBean.setBackNews(backNews);
}
RequestDispatcher dispatcher=request.getRequestDispatcher("inputRegisterMess.jsp");
dispatcher.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
登录模块
效果展示:
功能测试:
~~~登录成功
~~~登录失败
①用户已登录
②用户不存在或密码错误
涉及源代码:
//---login.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<jsp:useBean id="loginBean" class="beans.Login" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>登录</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align="center">
<table border=2>
<tr><th>登录</th></tr>
<form action="loginServlet" method="post">
<tr>
<td>用户名</td>
<td><input type=text width=300 name="logname" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type=password width=300 name="password" /></td>
</tr>
</table>
<input type=submit name="g" value="提交">
</form>
</div>
<div align=center>
登录结果:<br>
<jsp:getProperty name="loginBean" property="backNews" />
<br>登录名称:<br><jsp:getProperty name="loginBean" property="logname" />
</div>
</body>
</html>
//---beans.Login
package beans;
import java.util.*;
public class Login {
String logname="",backNews="未登录";
LinkedList<String> car;
public Login() {
car =new LinkedList<String>();
} public void setLogname(String logname) {
this.logname=logname;
}
public String getLogname() {
return logname;
}
public void setBackNews(String backNews) {
this.backNews=backNews;
}
public String getBackNews() {
return backNews;
}
public LinkedList<String> getCar() {
return car;
}
}
//---servlets.HandleLogin.java
package servlets;
import beans.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleLogin extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
}
public String handleString(String s) {
try {byte bb[] =s.getBytes("iso-8859-1");
s=new String(bb);
}catch(Exception e) {e.printStackTrace();}
return s;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
Connection con;
Statement sql;
String backNews;
String logname=handleString(request.getParameter("logname").trim());
String password=handleString(request.getParameter("password").trim());
boolean boo=(logname.length()>0)&&(password.length()>0);
try {
con=DriverManager.getConnection(uri,"root","123456");
String condition ="select * from user where logname='"+logname+"' and password='"+password+"'";
sql=con.createStatement();
if(boo) {
ResultSet rs=sql.executeQuery(condition);
boolean m=rs.next();
if(m==true) {
success(request,response,logname,password);
RequestDispatcher dispatcher=request.getRequestDispatcher("login.jsp");
dispatcher.forward(request,response);
}
else {
backNews="你输入的用户名不存在,或密码不匹配";
fail(request,response,logname,backNews);
}
}
else {
backNews="账号或者密码为空";
fail(request,response,logname,backNews);
}
con.close();
}
catch(SQLException exp) {
backNews="该会员已经被使用,请更换名称"+exp;
fail(request,response,logname,backNews);
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void success(HttpServletRequest request,HttpServletResponse response,String logname,String password) throws ServletException,IOException{
Login loginBean=null;
HttpSession session =request.getSession(true);
try {
loginBean=(Login)session .getAttribute("loginBean");
if(loginBean==null) {
loginBean=new Login();
session.setAttribute("loginBean", loginBean);
}
String name=loginBean.getLogname();
if(name.equals(logname)) {
loginBean.setBackNews(logname+"已经登录了");
loginBean.setLogname(logname);
}
else {
loginBean.setBackNews(logname+"登录成功");
loginBean.setLogname(logname);
}
}
catch(Exception ee) {
loginBean=new Login();
session.setAttribute("loginBean", loginBean);
loginBean.setBackNews(logname+"登录成功");
loginBean.setLogname(logname);
}
}
public void fail(HttpServletRequest request,HttpServletResponse response,String logname,String backNews) throws ServletException,IOException{
response.setContentType("text/html;charset=GB2312;pageEncoding=GB2312");
try {
PrintWriter out =response.getWriter();
out.println("<html><body>");
out.println("<h2>"+logname+"登录反馈结果:"+backNews+"</h2>");
out.println("<a href=index.jsp>主页</a>");
out.println("</html></body>");
}catch(Exception ee) {
ee.printStackTrace();
}
}
}
浏览手机模块
效果展示:
功能测试:
涉及源代码:
//---lookMoblie.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>浏览手机</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<%
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
Connection con;
Statement sql;
ResultSet rs;
try{
con=DriverManager.getConnection(uri,"root","123456");
sql=con.createStatement();
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(Exception e) {e.printStackTrace();}
%>
</div>
</body>
</html>
//---servlets.QueryAllRecord.java
package servlets;
import beans.*;
import com.sun.rowset.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class QueryAllRecord extends HttpServlet{
CachedRowSetImpl rowSet=null;
public void init(ServletConfig config) throws ServletException{
super.init(config);
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String idNumber=request.getParameter("fenleiNumber");
if(idNumber==null) idNumber="0";
int id=Integer.parseInt(idNumber);
HttpSession session=request.getSession(true);
Connection con=null;
DataByPage dataBean=null;
try {
dataBean=(DataByPage)session.getAttribute("dataBean");
if(dataBean==null) {
dataBean=new DataByPage();
session.setAttribute("dataBean",dataBean);
}
}catch(Exception exp) {
dataBean=new DataByPage();
session.setAttribute("dataBean",dataBean);
}
try {
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
con=DriverManager.getConnection(uri,"root","123456");
Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=sql.executeQuery("Select * from mobileForm where id="+id);
rowSet=new CachedRowSetImpl();
rowSet.populate(rs);
dataBean.setRowSet(rowSet);
con.close();
}catch(Exception exp) {
exp.printStackTrace();
}
response.sendRedirect("byPageShow.jsp");
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
//---byPageShow.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="beans.DataByPage" %>
<%@ page import="com.sun.rowset.*" %>
<jsp:useBean id="dataBean" class="beans.DataByPage" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>登录</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<br/>
<table border=2>
<tr>
<th>手机标识码</th>
<th>手机名称</th>
<th>手机制造商</th>
<th>手机价格</th>
<th>查看详细</th>
<th>添加至购物车</th>
</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);
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;
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>当前显示第<jsp:getProperty name="dataBean" property="currentPage"/>
页,共有<jsp:getProperty name="dataBean" property="totalPages"/>页
<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" size=3>条记录
<input type=submit name="g" value="确定"></form></td>
<td><form action="" method=post>
输入页码<input type=text name="currentSize" >
<input type=submit name="g" value="确定"></form></td>
</tr>
</table>
</div>
</body>
</html>
//---beans.DataByPage.java
package beans;
import com.sun.rowset.*;
public class DataByPage {
CachedRowSetImpl rowSet=null;
int pageSize=1;
int totalPages=1;
int currentPage=1;
public CachedRowSetImpl getRowSet() {
return rowSet;
}
public void setRowSet(CachedRowSetImpl rowSet) {
this.rowSet = rowSet;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
//---showDetail.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="beans.Login" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="loginBean" class="beans.Login" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>Insert title here</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<% boolean boo=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(boo) response.sendRedirect("login.jsp");
String mobileID=request.getParameter("xijie");
out.print("<th>产品号"+mobileID);
if(mobileID==null){
out.print("无该产品号"); return;
}
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
try{
Connection con;
Statement sql;
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
con=DriverManager.getConnection(uri,"root","123456");
sql=con.createStatement();
String codition="Select * from mobileform where mobile_version='"+mobileID+"'";
ResultSet rs=sql.executeQuery(codition);
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 name=rs.getString(1);
String number=rs.getString(2);
String maker=rs.getString(3);
String price=rs.getString(4);
detailMess=rs.getString(5);
picture=rs.getString(7);
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(Exception exp){}
%>
</div>
</body>
</html>
//---servlets.PutGoodsToCar.java
package servlets;
import beans.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class PutGoodsToCar extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String goods=request.getParameter("java");
Login loginBean=null;
HttpSession session=request.getSession(true);
try {loginBean=(Login)session.getAttribute("loginBean");
boolean b=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(b) response.sendRedirect("login.jsp");
LinkedList<String> car=loginBean.getCar();
car.add(goods);
speakSomeMess(request,response,goods);
}catch(Exception exp) {
response.sendRedirect("login.jsp");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void speakSomeMess(HttpServletRequest request,HttpServletResponse response,String goods) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<h2>"+goods+"放入购物车</h2>");
out.print("<a href=lookShoppingCar.jsp>查看购物车</a>");
out.print("<a href=index.jsp>主页</a>");
out.print("</body>");
out.print("</html>");
}catch(Exception e) {
e.printStackTrace();
}
}
}
查询手机模块
效果展示:
功能测试:
~~~通过版本号
~~~通过手机名称
~~~通过价格
涉及源代码:
//---searMobile.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>查询手机</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<Form action="serachByConditionServlet" 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>
</body>
</html>
//---servlets.SearchByCondition.java
package servlets;
import beans.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.sun.rowset.*;
public class SearchByCondition extends HttpServlet{
CachedRowSetImpl rowSet=null;
public void init(ServletConfig config) throws ServletException{
CachedRowSetImpl rowSet=null;
super.init(config);
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String searchMess=request.getParameter("searchMess");
String radioMess=request.getParameter("radio");
if(searchMess==null||searchMess.length()==0) {
fail(request,response,"没有查询信息,无法查询");
return;
}
String condition="";
if(radioMess.equals("mobile_version")) condition="Select * from mobileForm where mobile_version='"+searchMess+"'";
else if(radioMess.equals("mobile_name")) condition="Select * from mobileForm where mobile_name like'%"+searchMess+"%'";
else if(radioMess.equals("mobile_price")) {
double max=0,min=0;
String regex="[^0123456789.]";
String[] priceMess=searchMess.split(regex);
if(priceMess.length==1) max=min=Double.parseDouble(priceMess[0]);
else if(priceMess.length==2) {
min=Double.parseDouble(priceMess[0]);
max=Double.parseDouble(priceMess[1]);
if(max<min) {
double t=max;
max=min;
min=t;
}
}
else {
fail(request,response,"没有查询信息,无法查询");
return;
}
condition="Select * from mobileForm where mobile_price<="+max+"AND mobile_price>="+min;
}
HttpSession session=request.getSession(true);
Connection con=null;
DataByPage dataBean=null;
try {
dataBean =(DataByPage)session.getAttribute("dataBean");
if(dataBean==null) {
dataBean=new DataByPage();
session.setAttribute("dataBean", dataBean);
}
}catch(Exception exp) {
dataBean=new DataByPage();
session.setAttribute("dataBean", dataBean);
}
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
try {
con=DriverManager.getConnection(uri,"root","123456");
Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=sql.executeQuery(condition);
rowSet=new CachedRowSetImpl();
rowSet.populate(rs);
dataBean.setRowSet(rowSet);
con.close();
}catch(Exception exp) {exp.printStackTrace();}
response.sendRedirect("byPageShow.jsp");
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void fail(HttpServletRequest request,HttpServletResponse response,String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<h2>"+backNews+"</h2>");
out.print("<a href=searchMobile.jsp>查询手机</a>");
out.print("</body>");
out.print("</html>");
}catch(Exception e) {
e.printStackTrace();
}
}}
查看购物车模块
效果展示:
功能测试:
~~~删除物品
~~~生成订单(无物品)
~~~生成订单(有物品)
涉及源代码:
//---lookShoppingCar.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="beans.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="loginBean" class="beans.Login" scope="session" />
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>查看购物车</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<% boolean boo=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(boo) response.sendRedirect("login.jsp");
LinkedList<String> 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++;
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>
</body>
</html>
//---servlets.HandleDelete.java
package servlets;
import beans.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class HandleDelete extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String delete=request.getParameter("delete");
Login loginBean=null;
HttpSession session=request.getSession(true);
try {
loginBean=(Login)session.getAttribute("loginBean");
boolean boo=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(boo) response.sendRedirect("login.jsp");
LinkedList<String> car=loginBean.getCar();
car.remove(delete);
}catch(Exception exp) {
response.sendRedirect("login.jsp");
}
RequestDispatcher dispatcher=request.getRequestDispatcher("lookShoppingCar.jsp");
dispatcher.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
//---servlets.HandleBuyGoods.java
package servlets;
import beans.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class HandleBuyGoods extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String buyGoodsMess=request.getParameter("buy");
if(buyGoodsMess==null||buyGoodsMess.length()==0) {
fail(request,response,"购物车没有物品,无法生成订单");
return;
}
String price=request.getParameter("price");
if(price==null||price.length()==0) {
fail(request,response,"没有计算价格和,无法生成订单");
return;
}
float sum=Float.parseFloat(price);
Login loginBean=null;
HttpSession session=request.getSession(true);
try {
loginBean=(Login)session.getAttribute("loginBean");
boolean boo=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(boo) response.sendRedirect("login.jsp");
}catch(Exception exp) {response.sendRedirect("login.jsp");}
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
Connection con;
PreparedStatement sql;
try {
con=DriverManager.getConnection(uri,"root","123456");
String insertCondition="insert into orderform values(?,?,?,?)";
sql=con.prepareStatement(insertCondition);
sql.setInt(1, 0);
sql.setString(2, loginBean.getLogname());
sql.setString(3, buyGoodsMess);
sql.setFloat(4, sum);
sql.executeUpdate();
LinkedList<String> car=loginBean.getCar();
car.clear();
success(request,response,"生成订单成功");
}catch(Exception exp) {
fail(request,response,"生成订单失败"+exp);
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void success(HttpServletRequest request,HttpServletResponse response,String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<h2>"+backNews+"</h2>");
out.print("<a href=index.jsp>主页</a>");
out.print("<a href=lookOrderForm.jsp>查看订单</a>");
out.print("</body>");
out.print("</html>");
}catch(Exception e) {
e.printStackTrace();
}
}
public void fail(HttpServletRequest request,HttpServletResponse response,String backNews) {
response.setContentType("text/html;charset=GB2312");
try {
PrintWriter out=response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<h2>"+backNews+"</h2>");
out.print("<a href=index.jsp>主页</a>");
out.print("</body>");
out.print("</html>");
}catch(Exception e) {
e.printStackTrace();
}
}
}
订单模块
效果展示:
涉及源代码:
//---lookOrderForm.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<jsp:useBean id="loginBean" class="beans.Login" scope="session"/>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>首页</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<%
boolean boo=loginBean.getLogname()==null||loginBean.getLogname().length()==0;
if(boo) response.sendRedirect("login.jsp");
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
Connection con;
Statement sql;
ResultSet rs;
try{
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
con=DriverManager.getConnection(uri,"root","123456");
sql=con.createStatement();
String condition="select id,mess,sum from orderform where logname='"+loginBean.getLogname()+"'";
rs=sql.executeQuery(condition);
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>订单号</th>");
out.print("<th width=100>信息</th>");
out.print("<th width=100>价格</th>");
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(Exception e){e.printStackTrace();}
%></div>
</body>
</html>
退出模块
涉及源代码:
//---servlets.HandleExit.java
package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleExit extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
HttpSession session=request.getSession(true);
session.invalidate();
response.sendRedirect("index.jsp");
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
管理员登录模块
效果展示:
功能测试:
~~~内容不全时自动调整会登录页面
~~~账户或密码名错误时
~~~登录成功则进入后台管理系统
涉及源代码:
//---managerLogin.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="GB2312">
<title>管理者登录界面</title>
</head>
<body>
<%@ include file="head.txt" %>
<div align=center>
<h2>管理员登录界面</h2>
<form action="manageLoginServlet" method=post>
账号:<input type=text name="manager">
<br>
密码:<input type=password name="password">
<br>
<input type=submit value="提交">
<input type=reset value="重置">
</form>
</div>
</body>
</html>
//---servlets.HandleManageLogin.java
package servlets;
import beans.*;
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleManageLogin extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312");
String manager=request.getParameter("manager");
String password=request.getParameter("password");
if(manager==null||manager.equals("")||password==null||password.equals("")) {
response.sendRedirect("manageLogin.jsp");
}
//驱动JDBC
try {Class.forName("com.mysql.cj.jdbc.Driver");}
catch(Exception e) {e.printStackTrace();}
//连接数据库
String uri="jdbc:mysql://localhost:3306/li?serverTimezone=GMT%2B8&characterEncoding=gb2312";
Connection con;
Statement sql;
try {
con=DriverManager.getConnection(uri,"root","123456");
sql=con.createStatement();
String condition1="Select * from manager where manager='"+manager+"' and password='"+password+"'";
ResultSet rs1=sql.executeQuery(condition1);
if(rs1.next()) {
Manager managerBean=null;
HttpSession session =request.getSession(true);
try {
managerBean=(Manager)session.getAttribute("manager");
if(managerBean==null) {
managerBean=new Manager();
session.setAttribute("manager", managerBean);
}
String name=managerBean.getManager();
if(name.equals(manager)) {
managerBean.setBackNews(manager+"已登录");
managerBean.setManager(manager);
}else {
managerBean.setBackNews(manager+"登录成功");
managerBean.setManager(manager);
}
}catch(Exception e) {
e.printStackTrace();
}
RequestDispatcher dispatcher=request.getRequestDispatcher("manageInterface.jsp");
dispatcher.forward(request,response);
}
else {
response.setContentType("text/html;charset=GB2312;pageEncoding=GB2312");
try {
PrintWriter out =response.getWriter();
out.println("<html><body>");
out.println("<h2>"+manager+"登录反馈结果:"+"失败"+"</h2>");
out.println("<a href=manageLogin.jsp>返回登录界面</a>");
out.println("</html></body>");
}catch(Exception ee) {
ee.printStackTrace();
}
}
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
}
上一篇: linux网络高级编程之非阻塞
下一篇: MySQL数据管理(外键的创建与删除)