<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/FindAllServlet">查询所有</a>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>用户信息管理系统</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
td, th {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h3 style="text-align: center">用户信息列表</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>QQ</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<%--遍历展示数据--%>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
<td>
<%-- <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/UpdateFindByIdServlet?id=${user.id}">修改</a> --%>
<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/UpdateFindByIdServlet?id=${user.id}">修改</a>
<a class="btn btn-default btn-sm" onclick="deleteById(${user.id})" href="javascript:void(0);">删除</a></td>
</tr>
</c:forEach>
<%--遍历展示数据--%>
<script>
function deleteById(userId) {
let flag = window.confirm("您确定是否删除此数据?");
if(flag){
//true的情况 用户点击的是确定 , 表示 确定删除
location.href ="${pageContext.request.contextPath}/DeleteServlet?id="+userId;
}
}
</script>
<tr>
<td colspan="8" align="center"><a class="btn btn-primary" href="add.jsp">添加联系人</a></td>
</tr>
</table>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- HTML5文档-->
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>添加用户</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<center><h3>添加联系人页面</h3></center>
<form action="${pageContext.request.contextPath}/AddServlet" method="post">
<div class="form-group">
<label for="name">姓名:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
</div>
<div class="form-group">
<label>性别:</label>
<input type="radio" name="gender" value="男" checked="checked"/>男
<input type="radio" name="gender" value="女"/>女
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
</div>
<div class="form-group">
<label for="address">籍贯:</label>
<select name="address" class="form-control" id="jiguan">
<option value="广东">广东</option>
<option value="广西">广西</option>
<option value="湖南">湖南</option>
</select>
</div>
<div class="form-group">
<label for="qq">QQ:</label>
<input type="text" class="form-control" name="qq" placeholder="请输入QQ号码"/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" name="email" placeholder="请输入邮箱地址"/>
</div>
<div class="form-group" style="text-align: center">
<input class="btn btn-primary" type="submit" value="提交" />
<input class="btn btn-default" type="reset" value="重置" />
<input class="btn btn-default" type="button" value="返回" />
</div>
</form>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>修改用户</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-2.1.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="width: 400px;">
<h3 style="text-align: center;">修改联系人</h3>
<form action="${pageContext.request.contextPath}/UpdateServlet" method="post">
<%--
传入id属性 增加一个标签 用户不能看 用户不能操作 type="hidden" 隐藏
隐藏域: 隐藏页面的标签
--%>
<input type="hidden" name="id" value="${user.id}">
<div class="form-group">
<label for="name">姓名:</label>
<input type="text" class="form-control" id="name" value="${user.name}" name="name" readonly="readonly" placeholder="请输入姓名" />
</div>
<div class="form-group">
<label>性别:</label>
<input type="radio" name="gender" value="男" ${user.gender=="男"?'checked="checked"':''} />男
<input type="radio" name="gender" value="女" ${user.gender=="女"?'checked="checked"':''} />女
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="text" class="form-control" id="age" value="${user.age}" name="age" placeholder="请输入年龄" />
</div>
<div class="form-group">
<label for="address">籍贯:</label>
<select name="address" class="form-control" >
<option value="广东" ${user.address=="广东"?'selected="selected"':''}>广东</option>
<option value="广西" ${user.address=="广西"?'selected="selected"':''}>广西</option>
<option value="湖南" ${user.address=="湖南"? 'selected="selected"':''}>湖南</option>
</select>
</div>
<div class="form-group">
<label for="qq">QQ:</label>
<input type="text" class="form-control" name="qq" value="${user.qq}" placeholder="请输入QQ号码"/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" class="form-control" name="email" value="${user.email}" placeholder="请输入邮箱地址"/>
</div>
<div class="form-group" style="text-align: center">
<input class="btn btn-primary" type="submit" value="提交" />
<input class="btn btn-default" type="reset" value="重置" />
<input class="btn btn-default" type="button" value="返回"/>
</div>
</form>
</div>
</body>
</html>
(1)WEB-INF lib文件夹
commons-beanutils-1.8.3.jar
commons-logging-1.1.1.jar
javax.servlet.jsp.jstl.jar
jstl-impl.jar
(2)javascript文件夹
jquery-2.1.0.min.js
(3)css文件夹
bootstrap.css
bootstrap-theme.css
package com.it.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(urlPatterns="/*")
public class EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//0.处理编码
// 设置请求解码方式
req.setCharacterEncoding("utf-8");
// 设置响应编码方式
resp.setContentType("text/html;charset=utf-8");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
package com.it.web.servlet;
import com.it.domain.User;
import com.it.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
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.util.Map;
@WebServlet(urlPatterns="/AddServlet")
public class AddServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.获得浏览器的数据
Map<String, String[]> parameterMap = request.getParameterMap();
//1.1创建User对象
User user = new User();
//1.2使用BeanUtils工具类封装数据
BeanUtils.populate(user,parameterMap);
//2.处理数据
//创建一个UserService对象,并调用其save方法保存数据
UserService userService = new UserService();
userService.save(user);
//3.通知用户添加是否成功
//调用一下findAllServlet
//1.请求转发 -> 有问题, 由于地址栏没有变化 重新刷新还是访问addServlet 数据会重复添加
//增删改都不要用请求转发(重定向) 只有查询使用请求转发(存入数据)
//request.getRequestDispatcher("FindAllServlet").forward(request,response);
//2.重定向 -> 可以跳转外部地址 所以必须加上前缀 虚拟项目名称
//重定向到findAllServlet(查询)
response.sendRedirect(request.getContextPath()+"/FindAllServlet");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.it.web.servlet;
import com.it.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 java.io.IOException;
@WebServlet(urlPatterns="/DeleteServlet")
public class DeleteServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获得id
String id = request.getParameter("id");
//2.调用service删除
//2.1创建对象
UserService userService = new UserService();
//2.2删除数据
userService.deleteById(Integer.parseInt(id));
//3.重定向到查询的Servlet即可
response.sendRedirect(request.getContextPath() + "/FindAllServlet");
}
}
package com.it.web.servlet;
import com.it.domain.User;
import com.it.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 java.io.IOException;
@WebServlet(urlPatterns="/UpdateFindByIdServlet")
public class UpdateFindByIdServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
/**
* 修改-查询用户数据
* 目标: 根据id查询到数据 并且返回到update.jsp 展示数据
* //1.获得用户id
* //2.调用service 并且传入id 返回一个User
* //3.1 将user存入到request域
* //3.2 请求转发到update.jsp 展示user数据
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获得用户id
String id = request.getParameter("id");
//2.调用service 并且传入id 返回一个User
UserService userService = new UserService();
//根据用户id查询数据
User user = userService.findById(Integer.parseInt(id));
//3.1 将user存入到request域
request.setAttribute("user",user);
//3.2 请求转发到update.jsp 展示user数据
request.getRequestDispatcher("update.jsp").forward(request,response);
}
}
package com.it.web.servlet;
import com.it.domain.User;
import com.it.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
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.util.Map;
@WebServlet(urlPatterns="/UpdateServlet")
public class UpdateServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
/**
* 修改
* //1.获得浏览器传递的数据
* //1.1 创建User
* //1.2 获得请求的map数据
* //1.3 快速封装即可
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.获得浏览器传递的数据
//1.1 创建User
User user = new User();
//1.2 获得请求的map数据
Map<String, String[]> parameterMap = request.getParameterMap();
//1.3 快速封装即可
BeanUtils.populate(user,parameterMap);
//2.处理数据
//2.1 定义对象
UserService userService = new UserService();
//2.2 调用方法
userService.update(user);
//3.响应数据 - 重定向到查询的Servlet
response.sendRedirect(request.getContextPath() + "/FindAllServlet");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.it.web.servlet;
import com.it.domain.User;
import com.it.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 java.io.IOException;
import java.util.List;
@WebServlet(urlPatterns="/FindAllServlet")
public class FindAllServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获得浏览器传输的数据
//2.处理这些数据(调用service查询到数据)
UserService userService = new UserService();
//查询到数据库的数据
List<User> userList = userService.findAll();
//3.响应数据->请求转发给list.jsp显示数据
request.setAttribute("userList",userList);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
package com.it.service;
import com.it.dao.UserDao;
import com.it.domain.User;
import java.util.List;
public class UserService {
public void save(User user) {
UserDao userDao = new UserDao();
userDao.save(user);
}
public List<User> findAll() {
UserDao userDao = new UserDao();
List<User> userList = userDao.findAll();
return userList;
}
public User findById(int id) {
UserDao userDao = new UserDao();
User user = userDao.findById(id);
return user;
}
public void update(User user) {
UserDao userDao = new UserDao();
userDao.update(user);
}
public void deleteById(int id) {
UserDao userDao = new UserDao();
userDao.deleteById(id);
}
}
package com.it.dao;
import com.it.db.InitDB;
import com.it.domain.User;
import java.util.List;
public class UserDao {
public void save(User user) {
InitDB.save(user);
}
public List<User> findAll() {
List<User> userList = InitDB.getUserList();
return userList;
}
public User findById(int id) {
User user = InitDB.findById(id);
return user;
}
public void update(User user) {
InitDB.update(user);
}
public void deleteById(int id) {
InitDB.deleteById(id);
}
}
package com.it.domain;
public class User {
// 编号 学号 身份证号 唯一标识
private int id;
// 姓名
private String name;
// 性别
private String gender;
// 年龄
private Integer age;
// 籍贯
private String address;
// QQ
private String qq;
// 邮箱
private String email;
//无参
public User() {
}
//有参 构造 为了初始化数据方便
public User(Integer id, String name, String gender, Integer age, String address, String qq, String email) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.address = address;
this.qq = qq;
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.it.db;
import com.it.domain.User;
import java.util.ArrayList;
import java.util.List;
/**
* 初始化数据使用
*/
public class InitDB {
//定义对象
private static List<User> userList = null;
static {
//初始化数据
userList = new ArrayList<User>();
userList.add(new User(1 , "王老师" , "男" , 18 , "广东" , "12345678" , "[email protected]"));
userList.add(new User(2 , "小包同学" , "女" , 23 , "广西" , "2222222" , "[email protected]"));
userList.add(new User(3 , "肖同学" , "不明" , 22 , "湖南" , "33333" , "[email protected]"));
userList.add(new User(4 , "张三" , "男" , 19 , "湖南" , "12345678" , "[email protected]"));
userList.add(new User(5 , "李世伟" , "女" , 20 , "广西" , "12345678" , "[email protected]"));
userList.add(new User(6 , "王五" , "男" , 25 , "广东" , "12345678" , "[email protected]"));
}
//返回数据
public static List<User> getUserList(){
return userList;
}
public static void save(User user) {
if(userList.size() == 0 ){
user.setId(1);
}else {
User lastUser = userList.get(userList.size() - 1);
user.setId(lastUser.getId() + 1);
}
//保存到List集合中
userList.add(user);
}
public static User findById(int id) {
//1.遍历集合
for (User user : userList) {
//2.判断id是否相同
if(user.getId() == id){
return user;
}
}
return null;
}
public static void update(User user) {
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getId() == user.getId()){
userList.remove(i);
userList.add(i,user);
}
}
}
public static void deleteById(int id) {
//1.遍历集合
for (int i = 0; i < userList.size(); i++) {
if(userList.get(i).getId() == id){
userList.remove(i);//删除数据
break;
}
}
}
}