package net.hw.shop.servlet;
/**
* 功能:显示用户控制程序
* 日期:2019年12月18日
*/
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
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 net.hw.shop.bean.User;
import net.hw.shop.service.UserService;
@WebServlet("/addUser")
public class addUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取session对象
HttpSession session = request.getSession();
// 获取用户名
String username = request.getParameter("username");
// 获取密码
String password = request.getParameter("password");
// 获取电话号码
String telephone = request.getParameter("telephone");
// 设置注册时间(时间戳对象)
Timestamp registerTime = new Timestamp(System.currentTimeMillis());
int popedom = Integer.parseInt(request.getParameter("popedom"));
// 创建用户对象
User user = new User();
// 设置用户对象信息
user.setUsername(username);
user.setPassword(password);
user.setTelephone(telephone);
user.setRegisterTime(registerTime);
user.setPopedom(popedom);
// 创建UserService对象
UserService userService = new UserService();
// 调用UserService对象的添加用户方法
int count = userService.addUser(user);
// 判断是否注册成功
if (count > 0) {
// 设置session属性
session.setAttribute("registerMsg", "恭喜,注册成功!");
// 获取全部用户
List<User> users = userService.findAllUsers();
// 把用户列表以属性的方式保存到session里
session.setAttribute("users", users);
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/backend/showUser.jsp");
// 在控制台输出测试信息
System.out.println("恭喜,注册成功,跳转到登录页面!");
} else {
// 设置session属性
session.setAttribute("registerMsg", "遗憾,注册失败!");
// 重定向到注册页面
response.sendRedirect(request.getContextPath() + "/backend/showUser.jsp");
// 在控制台输出测试信息
System.out.println("遗憾,注册失败,跳转到注册页面!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>添加用户信息</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script src="scripts/check.js" type="text/javascript"></script>
</head>
<body>
<hr>
<table width="90%" border="0px">
<tr>
<td align="left">登录用户:<span style="color: red;">${username}</span></td>
<td align="right"><a href="user/logout" target="_parent">注销</a></td>
</tr>
</table>
<hr>
<div class="main">
<form action="addUser" method="post">
<table>
<tr>
<td>账号</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr>
<td align="center">电话</td>
<td><input id="telephone" type="text" name="telephone"/></td>
</tr>
<tr>
<td>权限</td>
<td><input id="popedom" type="text" name="popedom" /></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="注册" onclick="return checkLoginForm();"/>
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</div>
<hr>
</body>
</html>
package net.hw.shop.servlet;
import net.hw.shop.bean.User;
import net.hw.shop.service.UserService;
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 java.io.IOException;
import java.util.List;
@WebServlet("/deleteUser")
public class deleteUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
int id=Integer.parseInt(request.getParameter("id"));
UserService userService = new UserService();
int count = userService.deleteUserById(id);
if(count >0){
List<User> users=userService.findAllUsers();
session.setAttribute("users",users);
response.sendRedirect(request.getContextPath() + "/backend/deleteUser.jsp");
System.out.println("删除成功");
}else {
System.out.println("删除成功");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>删除用户信息</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script src="scripts/check.js" type="text/javascript"></script>
</head>
<body>
<hr>
<table width="90%" border="0px">
<tr>
<td align="left">登录用户:<span style="color: red;">${username}</span></td>
<td align="right"><a href="user/logout" target="_parent">注销</a></td>
</tr>
</table>
<hr>
<h3>用户列表</h3>
<hr>
<table border="1" width="90%" cellspacing="0">
<tr>
<th>编号</th>
<th>用户名</th>
<th>删除</th>
</tr>
<c:forEach var="user" items="${users}">
<tr align="center">
<td>${user.id}</td>
<td>${user.username}</td>
<td><a href="deleteUser?id=${user.id}">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
删除之前:
删除之后:
3.更新用户:
updateUserList
package net.hw.shop.servlet;
/**
* 功能:登录处理类
* 作者:cw
* 日期:2019年12月10日
*/
import net.hw.shop.dbutil.ConnectionManager;
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 java.io.IOException;
import java.sql.*;
@WebServlet("/update")
public class updataUserList extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取数据库
Connection conn = ConnectionManager.getConnection();
// 获取用户名
String telephone = request.getParameter("telephone");
// 获取密码
String password = request.getParameter("password");
String id = request.getParameter("showId");
//创建SQL语句
String strSQL1 = "UPDATE t_user SET telephone = ? WHERE id = ?";
String strSQL2 = "UPDATE t_user SET password = ? WHERE id = ?";
// 创建预备语句对象
PreparedStatement pstmt1 = null ;
PreparedStatement pstmt2 = null ;
try {
pstmt1 = conn.prepareStatement(strSQL1);
pstmt1.setString(1,telephone);
pstmt1.setString(2,id);
pstmt2 = conn.prepareStatement(strSQL2);
pstmt2.setString(1,password);
pstmt2.setString(2,id);
response.sendRedirect("updateUser.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
UpdateUserServlet
package net.hw.shop.servlet;
import net.hw.shop.bean.User;
import net.hw.shop.service.UserService;
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 java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取session对象
HttpSession session = request.getSession();
// 获取密码
String password = request.getParameter("password");
// 获取电话号码
String telephone = request.getParameter("telephone");
//获取ID
int id =Integer.parseInt(request.getParameter("id")) ;
// 创建UserService对象
UserService userService = new UserService();
// 创建用户对象
User user = userService.findUserById(id);
// 设置用户对象信息
user.setPassword(password);
user.setTelephone(telephone);
// 调用UserService对象的添加用户方法
int count = userService.updateUser(user);
if(count >0){
// 设置session属性
session.setAttribute("registerMsg", "恭喜,注册成功!");
// 获取全部用户
List<User> users = userService.findAllUsers();
// 把用户列表以属性的方式保存到session里
session.setAttribute("users", users);
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/backend/showUser.jsp");
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>添加用户信息</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script src="scripts/check.js" type="text/javascript"></script>
</head>
<body>
<hr>
<table width="90%" border="0px">
<tr>
<td align="left">登录用户:<span style="color: red;">${username}</span></td>
<td align="right"><a href="user/logout" target="_parent">注销</a></td>
</tr>
</table>
<hr>
<div class="main">
<form action="/simonshop/backend/updataUserList.jsp" method="post" >
<table>
<tr>
<td>ID:</td>
<td><input id="showId" type="text" name="showId" readonly="readonly"></td>
</tr>
<tr>
<td>密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr>
<td align="center">电话</td>
<td><input id="telephone" type="text" name="telephone"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="确定" onclick="return check();"/>
</tr>
</table>
</form>
</div>
<hr>
<script type="text/javascript">
var id = ${param.id};
document.getElementById("showId").value = id;
console.log(id);
function check() {
var telephone = document.getElementById("telephone");
var password = document.getElementById("password");
if (password.value == ""){
alert("密码不能为空!");
password.focus();
return false;
}
if (telephone.value == ""){
alert("电话不能为空");
telephone.focus();
return false;
}
return true;
}
</script>
</body>
</html>
updateUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>显示用户信息</title>
<base href="${basePath}/">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<hr>
<table width="90%" border="0px">
<tr>
<td align="left">登录用户:<span style="color: red;">${username}</span></td>
<td align="right"><a href="user/logout" target="_parent">注销</a></td>
</tr>
</table>
<hr>
<h3>用户列表</h3>
<hr>
<table border="1" width="90%" cellspacing="0">
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>电话</th>
<th>注册时间</th>
<th>权限</th>
<th>修改</th>
</tr>
<c:forEach var="user" items="${users}">
<tr align="center">
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.telephone}</td>
<td><fmt:formatDate value="${user.registerTime}" pattern="yyyy-MM-dd hh:mm:ss"/></td>
<td>
<c:choose>
<c:when test="${user.popedom==0}">
管理员
</c:when>
<c:otherwise>
普通用户
</c:otherwise>
</c:choose>
</td>
<td><a href="http://localhost:8080/simonshop/backend/update.jsp?id=${user.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<hr>
</body>
</html>
updateUserList.jsp
<%--
Created by IntelliJ IDEA.
User: 71090
Date: 2019/12/25
Time: 17:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="net.hw.shop.dbutil.ConnectionManager" %>
<%
request.setCharacterEncoding("utf-8");
// 获取数据库
Connection conn = ConnectionManager.getConnection();
// 获取用户名
String telephone = request.getParameter("telephone");
// 获取密码
String password = request.getParameter("password");
System.out.println("id:" + request.getParameter("showId"));
int id = Integer.parseInt(request.getParameter("showId"));
//创建SQL语句
String strSQL1 = "UPDATE t_user SET telephone = ? WHERE id = ?";
String strSQL2 = "UPDATE t_user SET password = ? WHERE id = ?";
// 创建预备语句对象
PreparedStatement pstmt1 = null ;
PreparedStatement pstmt2 = null ;
int result1= 0;
int result2 = 0;
try {
pstmt1 = conn.prepareStatement(strSQL1);
pstmt1.setString(1,telephone);
pstmt1.setInt(2,id);
pstmt2 = conn.prepareStatement(strSQL2);
pstmt2.setString(1,password);
pstmt2.setInt(2,id);
pstmt1.executeUpdate();
pstmt2.executeUpdate();
response.sendRedirect("updateUser.jsp");
} catch (SQLException e) {
e.printStackTrace();
}
%>