系统前台
前台用户操作:
账号:test密码:test
- 登录页面
- 商品浏览
- 商品详情
- 购物车
- 订单页面
系统后台
后台用户操作:
账号:admin密码:admin
- 用户登录页面
- 后台主页
- 添加商品
- 编辑商品
- 删除商品
数据库
部分代码
login.jsp
用户登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>用户登录页面</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body background="images/login.jpg">
<div class="container" style="height:auto;width:auto;margin:auto 24% auto 30%;">
<h1 class="text-center" style="margin-top: 5%;">用户登录</h1>
<form class="form-horizontal" role="form" name="form1" method="post" action="Login" style="margin-top: 10%;margin-left: 10%;">
<div class="form-group">
<label for="user" class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-6">
<input name="user_name" type="text" class="form-control" id="user" style="background-color: transparent;"/>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-6">
<input name="user_password" type="password" class="form-control" id="password" style="background-color: transparent;"/>
<c:choose>
<c:when test="${param.loginInfo eq 'wrong'}">
<strong style="color:red">用户名或密码错误!</strong>
</c:when>
<c:when test="${param.loginInfo eq 'no'}">
<strong style="color:red">请先登录!</strong>
</c:when>
</c:choose>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-10">
<button type="submit" class="btn btn-default btn-lg" style="background:rgba(255, 251, 240, 0.4);">
<span class="glyphicon glyphicon-log-in"></span> 登录</button>
</div>
</div>
</form>
</div>
</body>
</html>
LoginServlet.java
登录后端代码
package servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import domain.User;
import service.UserService;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserService userService=new UserService();
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");//解决post方式的汉字请求乱码
response.setContentType("text/html;charset=utf-8");
String name =request.getParameter("user_name");//获得参数,name=""
String pwd = request.getParameter("user_password");
System.out.println("账号:"+name+"密码:"+pwd);
try{
if(userService.login(name,pwd)){
User user = new User();
user.setUsername(name);
HttpSession session =request.getSession();//获取session对象
session.setAttribute("user", user);//设置属性名user,保存user对象
if(userService.isadmin(name)){
response.sendRedirect("productServlet?method=admin");
}else{
response.sendRedirect("productServlet?method=query");
}
}else{
response.sendRedirect("login.jsp?loginInfo=wrong");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
showpoducts.jsp
前台主页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品页面</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
body{
background: none repeat scroll 0 0 #fff;
color: #661;
font: 20px/200% Arial,Verdana,"宋体";
}
li{
display: inline-block;
}
.gl-i-wrap{
font-size:16px;
margin: 10px 18px 20px 18px;
}
.p-price{
color:red;
font-size:16px;
}
.p-name{
font-weight:bold;
font-size:16px;
display: block;
width:200px;/*对宽度的定义,根据情况修改*/
overflow: hidden;
white-space: nowrap;
text-overflow: clip;
}
.p-name a:link{/* 未访问链接*/
color:#000000;
}
.p-name a:visited {/* 已访问链接 */
color:#6633FF;
}
.p-name a:hover {/* 鼠标移动到链接上 */
color:#333399;
}
.p-name a:active { /* 鼠标点击时 */
color:#0000FF;
}
</style>
</head>
<h3 class="text-center">商品</h3>
<body>
<div style="padding-left: 75%;">
<strong>当前登录的用户为:${user.username }</strong>
<a href="productServlet?method=loginOut" >
<button class="btn btn-default btn-lg" style="color: rgb(255, 140, 60);font-size: 5px">
<span class="glyphicon glyphicon-log-out"></span> 退出登录
</button>
</a>
</div>
<hr style="border-top: 2px solid #333;">
<ul>
<c:forEach items="${requestScope.productList }" var="p">
<li>
<div class="gl-i-wrap">
<div>
<a class="thumbnail" href="productServlet?method=details&id=${p.id }" target="_blank">
<img width="190" height="190" src="${pageContext.request.contextPath}/images/${p.imgurl}">
</a>
</div>
<div class="p-name"><a href="productServlet?method=details&id=${p.id }">${p.name }</a></div>
<div class="p-price" >
<strong>¥${p.price }</strong>
</div>
</div>
</li>
</c:forEach>
</ul>
</body>
</html>
productList.jsp
后台主页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>后台管理</title>
</head>
<body style="background:rgba(100, 120, 255, 0.2);">
<div>
<div style="padding-left: 80%;">
<strong>当前登录的用户为:${user.username }</strong>
<a href="productServlet?method=loginOut" >
<button class="btn btn-default btn-lg" style="color: rgb(255, 140, 60);font-size: 5px;background:rgba(255, 251, 240, 0.4)">
<span class="glyphicon glyphicon-log-out"></span> 退出登录
</button>
</a>
</div>
<a href="productServlet?method=toAdd" >
<button class="btn btn-default btn-lg" style="color: rgb(255, 140, 60);font-size: 20px;background:rgba(255, 251, 240, 0.4)">
<span class="glyphicon glyphicon-plus"></span> 添加
</button>
</a>
</div>
<table class="table table-hover" style="width:98%;align:center;background:rgba(255, 251, 240, 0.4);" border="1" >
<tr align="center">
<th>商品ID</th>
<th></th>
<th>商品名</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.productList}" var="p">
<tr align="center">
<td >${p.id}</td>
<td><img src="images/${p.imgurl }" class="img-thumbnail" width="60" height="60"></td>
<td >${p.name}</td>
<td >${p.price}</td>
<td ><a href="productServlet?method=toEdit&id=${p.id }">编辑</a>
<a href="#" onclick="confirmDel(${p.id })">删除</a></td>
</tr>
</c:forEach>
</table>
<script>
function confirmDel(id){
if(window.confirm("您确定要删除该条商品信息吗?")){
document.location="productServlet?method=delete&id="+id
}
}
</script>
</body>
</html>