✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。
🍎个人主页:Java Fans的博客
🍊个人信条:不迁怒,不贰过。小知识,大智慧。
💞当前专栏:Java案例分享专栏
✨特色专栏:国学周更-心性养成之路
🥭本文内容:Servlet 综合案例(empProject)
更多内容点击👇
图书管理系统【Servlet+HTML综合应用】
【本文目录】
- 1.1 项目说明
- 1.2 数据库环境搭建
- 1.2.1 创建员工信息表emp并添加数据
- 1.2.2 创建系统管理员表empManager并添加数据
- 1.3 创建Web项目
- 1.4 基础环境搭建
- 1.5 管理员登录功能
- 1.6 查询所有员工功能
- 1.6.1 调用业务逻辑Controller
- 1.6.2 显示页面JSP
- 1.6.3 权限验证过滤器
- 1.6.4 字符编码过滤器
- 1.7 删除员工功能
- 1.7.1 删除员工Controller
- 1.8 修改员工功能
- 1.8.1 查询单个员工Controller
- 1.8.2 显示修改页面JSP
- 1.8.3 修改员工信息Controller
1.1 项目说明
使用管理员账号登录系统,实现对员工信息的增删改查
1.2 数据库环境搭建
该案例是empProject员工管理系统。使用了两张表
- 员工信息表emp
- 系统管理员表empManager
1.2.1 创建员工信息表emp并添加数据
# 创建表emp
CREATE TABLE IF NOT EXISTS `emp`(
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`salary` DOUBLE NOT NULL,
`age` INT NOT NULL
);
# 向emp表中添加数据
INSERT INTO `emp`(`id`,`name`,`salary`,`age`)VALUES(1001,'张三',5200,26);
INSERT INTO `emp`(`id`,`name`,`salary`,`age`)VALUES(1002,'李四',7200,28);
1.2.2 创建系统管理员表empManager并添加数据
#创建表empManager
CREATE TABLE IF NOT EXISTS `empManager`(
`username` VARCHAR(20) NOT NULL,
`password` VARCHAR(20) NOT NULL
);
# 向empManager表中添加数据
INSERT INTO `empManager`(`username`,`password`)VALUES('如花','123456');
INSERT INTO `empManager`(`username`,`password`)VALUES('似玉','654321');
1.3 创建Web项目
创建Web项目,导入相关jar包
- commons-dbutils-1.7.jar
- druid-1.1.5.jar
- mysql-connector-java-5.1.25-bin.jar
- ValidateCode.jar
1.4 基础环境搭建
在项目下创建包目录结构如下
- com.cxyzxc.www.controller包:调用业务逻辑Servlet
- com.cxyzxc.www.dao包:数据访问层接口
- com.cxyzxc.www.dao.impl包:数据访问层接口实现类
- com.cxyzxc.www.entity包:实体类
- com.cxyzxc.www.filter包:过滤器
- com.cxyzxc.www.jsp包:打印显示页面Servlet
- com.cxyzxc.www.service包:业务逻辑层接口
- com.cxyzxc.www.service.impl包:业务逻辑层接口实现类
- com.cxyzxc.www.utils包:工具类
- database.properties:数据库连接及连接池配置文件
1.5 管理员登录功能
login.html编写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
<link type="text/css" rel="stylesheet" href="css/login.css"/>
</head>
<body>
<div>
<form action="/empProject01_war_exploded/manager/EmpManagerLoginController" method="post">
<p>
用户名:<input type="text" name="username"/>
</p>
<p>
密码:<input type="password" name="password"/>
</p>
<p>
验证码:<input type="text" name="inputVcode">
<img src="/empProject01_war_exploded/createCode"/>
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</div>
</body>
</html>
EmpManagerLoginController.java编写
package com.cxyzxc.www.controller;
import com.cxyzxc.www.entity.EmpManager;
import com.cxyzxc.www.service.EmpManagerService;
import com.cxyzxc.www.service.impl.EmpManagerServiceImpl;
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;
@WebServlet(name = "EmpManagerLoginController",value = "/manager/EmpManagerLoginController")
public class EmpManagerLoginController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.收参
String username = request.getParameter("username");
String password = request.getParameter("password");
String inputVcode = request.getParameter("inputVcode");
//2.校验验证码
String codes = (String)request.getSession().getAttribute("codes");
if(!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes)){
//调用业务逻辑实现登录
EmpManagerService empManagerService = new EmpManagerServiceImpl();
EmpManager empManager = empManagerService.login(username,password);
if(empManager!=null){
//登录成功
//存储在session作用域
HttpSession session = request.getSession();
session.setAttribute("empManager",empManager);
//跳转到查询所有的controller
response.sendRedirect(request.getContextPath()+"/manager/safe/showAllEmpController");
}else{
response.sendRedirect(request.getContextPath()+"/login.html");
}
}else{
//验证码输入错误,跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
1.6 查询所有员工功能
1.6.1 调用业务逻辑Controller
ShowAllEmpController.java编写
package com.cxyzxc.www.controller;
import com.cxyzxc.www.entity.Emp;
import com.cxyzxc.www.service.EmpService;
import com.cxyzxc.www.service.impl.EmpServiceImpl;
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(name = "ShowAllEmpController",value = "/manager/safe/showAllEmpController")
public class ShowAllEmpController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EmpService empService = new EmpServiceImpl();
List<Emp> emps = empService.showAllEmp();
request.setAttribute("emps",emps);
request.getRequestDispatcher("/manager/safe/showAllEmpJSP").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.6.2 显示页面JSP
package com.cxyzxc.www.jsp;
import com.cxyzxc.www.entity.Emp;
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.io.PrintWriter;
import java.util.List;
@WebServlet(name = "ShowAllEmpJSP",value = "/manager/safe/showAllEmpJSP")
public class ShowAllEmpJSP extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取集合数据
List<Emp> emps = (List<Emp>)request.getAttribute("emps");
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println(" <head>");
printWriter.println(" <meta charset='UTF-8'>");
printWriter.println(" <title>查询所有员工页面</title>");
printWriter.println(" </head>");
printWriter.println(" <body>");
printWriter.println(" <table border='1'>");
printWriter.println(" <tr>");
printWriter.println(" <td>编号</td>");
printWriter.println(" <td>姓名</td>");
printWriter.println(" <td>工资</td>");
printWriter.println(" <td>年龄</td>");
printWriter.println(" <td colspan='2'>操作</td>");
printWriter.println(" </tr>");
for(Emp emp: emps){
printWriter.println(" <tr>");
printWriter.println(" <td>"+emp.getId()+"</td>");
printWriter.println(" <td>"+emp.getName()+"</td>");
printWriter.println(" <td>"+emp.getSalary()+"</td>");
printWriter.println(" <td>"+emp.getAge()+"</td>");
printWriter.println(" <td><a href='"+request.getContextPath()+"/manager/safe/removeEmpController?id="+emp.getId()+"'>删除<a></td>");
printWriter.println(" <td><a href='"+request.getContextPath()+"/manager/safe/showEmpController?id="+emp.getId()+"'>修改</a></td>");
printWriter.println(" </tr>");
}
printWriter.println(" </table>");
printWriter.println(" </body>");
printWriter.println("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.6.3 权限验证过滤器
验证登录的过滤器编写
package com.cxyzxc.www.filter;
import com.cxyzxc.www.entity.EmpManager;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(value = "/manager/safe/*")
public class CheckFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpSession session = request.getSession();
EmpManager empManager = (EmpManager)session.getAttribute("empManager");
if(empManager!=null){//登录过
filterChain.doFilter(request,response);
}else{
response.sendRedirect(request.getContextPath()+"/login.html");
}
}
@Override
public void destroy() {
}
}
1.6.4 字符编码过滤器
package com.cxyzxc.www.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(value = "/manager/*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
1.7 删除员工功能
1.7.1 删除员工Controller
package com.cxyzxc.www.controller;
import com.cxyzxc.www.service.EmpService;
import com.cxyzxc.www.service.impl.EmpServiceImpl;
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(name = "RemoveEmpController",value = "/manager/safe/removeEmpController")
public class RemoveEmpController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer.valueOf(request.getParameter("id"));
EmpService empService = new EmpServiceImpl();
empService.removeEmp(id);
response.sendRedirect(request.getContextPath()+"/manager/safe/showAllEmpController");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.8 修改员工功能
1.8.1 查询单个员工Controller
package com.cxyzxc.www.controller;
import com.cxyzxc.www.entity.Emp;
import com.cxyzxc.www.service.EmpService;
import com.cxyzxc.www.service.impl.EmpServiceImpl;
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(name = "ShowEmpController",value = "/manager/safe/showEmpController")
public class ShowEmpController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer.valueOf(request.getParameter("id"));
EmpService empService = new EmpServiceImpl();
Emp emp = empService.showEmp(id);
request.setAttribute("emp",emp);
request.getRequestDispatcher("/manager/safe/showUpdateEmpInfoJSP").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.8.2 显示修改页面JSP
package com.cxyzxc.www.jsp;
import com.cxyzxc.www.entity.Emp;
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.io.PrintWriter;
@WebServlet(name = "ShowUpdateEmpInfoController",value = "/manager/safe/showUpdateEmpInfoJSP")
public class ShowUpdateEmpInfoController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Emp emp = (Emp)request.getAttribute("emp");
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println(" <head>");
printWriter.println(" <meta charset='UTF-8'>");
printWriter.println(" <title>修改员工信息页面</title>");
printWriter.println(" </head>");
printWriter.println(" <body>");
printWriter.println(" <form action='/empProject01_war_exploded/manager/safe/updateEmpController' method='post'>");
printWriter.println(" 编号:<input type='text' name='id' value='"+emp.getId()+"' readonly/><br/>");
printWriter.println(" 姓名:<input type='text' name='name' value='"+emp.getName()+"'/><br/>");
printWriter.println(" 工资:<input type='text' name='salary' value='"+emp.getSalary()+"'/><br/>");
printWriter.println(" 年龄:<input type='text' name='age' value='"+emp.getAge()+"'/><br/>");
printWriter.println(" <input type='submit' value='修改'/><br/>");
printWriter.println(" </form>");
printWriter.println(" </body>");
printWriter.println("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.8.3 修改员工信息Controller
package com.cxyzxc.www.controller;
import com.cxyzxc.www.entity.Emp;
import com.cxyzxc.www.service.EmpService;
import com.cxyzxc.www.service.impl.EmpServiceImpl;
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(name = "UpdateEmpController",value = "/manager/safe/updateEmpController")
public class UpdateEmpController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.收参
Integer id = Integer.valueOf(request.getParameter("id"));
String name = request.getParameter("name");
Double salary = Double.valueOf(request.getParameter("salary"));
Integer age = Integer.valueOf(request.getParameter("age"));
Emp emp = new Emp(id,name,salary,age);
EmpService empService = new EmpServiceImpl();
empService.modify(emp);
response.sendRedirect(request.getContextPath()+"/manager/safe/showAllEmpController");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
码文不易,本篇文章就介绍到这里,如果想要学习更多Java系列知识,点击关注博主,博主带你零基础学习Java知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目:《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。