学完了servlet、数据库、以及之前的jsp。我们可以 做一个管理系统,将知识综合起来运用!
一、创建数据库,表名 easyui。
二、实现增删改查。
1.新建web项目,导入所需要的4个包
2.包的分层 工具包将之前的复制过来就好了。
dao部分 创建 接口和 实现类。实现类在dao包下新建包impl。
package com.aaa.dao;import com.aaa.entity.Employee;import java.util.List;
import java.util.Map;public interface IDengDAO {boolean goDeng(String username,String password);//查询所有员工List
impl部分
package com.aaa.dao.Impl;import com.aaa.dao.IDengDAO;
import com.aaa.entity.Employee;
import com.aaa.util.DBUtil;import java.util.List;
import java.util.Map;public class DengDAOImpl implements IDengDAO {@Overridepublic boolean goDeng(String username, String password) {String sql="select * from login where username=? and password=?";List
entity实体类部分
package com.aaa.entity;public class Employee {private int id;private String name;private String telephone;private int deptID;private int roleID;private int state;public Employee() {}public Employee(int id, String name, String telephone, int deptID, int roleID, int state) {this.id = id;this.name = name;this.telephone = telephone;this.deptID = deptID;this.roleID = roleID;this.state = state;}public Employee(String name, String telephone, int deptID, int roleID, int state) {this.name = name;this.telephone = telephone;this.deptID = deptID;this.roleID = roleID;this.state = state;}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 getTelephone() {return telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public int getDeptID() {return deptID;}public void setDeptID(int deptID) {this.deptID = deptID;}public int getRoleID() {return roleID;}public void setRoleID(int roleID) {this.roleID = roleID;}public int getState() {return state;}public void setState(int state) {this.state = state;}@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +", telephone='" + telephone + '\'' +", deptID=" + deptID +", roleID=" + roleID +", state=" + state +'}';}
}
3.创建登录界面index.jsp,在web目下。并创建LoginServlet 进行业务操作。
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/4/18Time: 20:06To change this template use File | Settings | File Templates.
--%>
<%&#64; page contentType&#61;"text/html;charset&#61;UTF-8" language&#61;"java" %>
<html><head><%-- 此时 这里仅仅只是登录界面 不通过servlet 用户直接在浏览器中访问index.jsp --%><title>用户登录界面title><style>fieldset{width: 300px;margin: 100px auto;}style>head><body><fieldset><legend>用户登录legend><%--密码错误 通过 error 关键字 获得 错误提示信息--%><div style&#61;"color: red">${error}div><%--提交的地址 是我们所写的loginServlet 注意路径格式--%><form action&#61;"/zxf/deng" method&#61;"post"><div>姓名&#xff1a;<input type&#61;"type" name&#61;"username"/>div><div>密码&#xff1a; <input type&#61;"password" name&#61;"password"/>div><button >登录button>form>fieldset>body>
html>
package com.aaa.servelt;import com.aaa.dao.IDengDAO;
import com.aaa.dao.Impl.DengDAOImpl;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;
&#64;WebServlet("/deng") //虚拟路径 form表单的提交地址 /zxf/deng
public class LoginServlet extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{//1.获取 我们在登录界面 输入的密码和用户名String username &#61; req.getParameter("username");String password &#61; req.getParameter("password");//2.调用dao方法进行验证IDengDAO dao&#61;new DengDAOImpl();boolean goDeng &#61; dao.goDeng(username, password);//3.判断密码 是否正确if (goDeng){//4.正确 重定向 到main.jsp页面resp.sendRedirect("/zxf/main.jsp");}else{//5.错误 共享数据 请求转发到 index.jspreq.setAttribute("error","账号密码错误&#xff0c;请重试&#xff01;");req.getRequestDispatcher("/index.jsp").forward(req,resp);}}
}
登录不成功 会提示错误。登陆成功&#xff0c;就跳转到main.jsp部分
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/4/18Time: 20:53To change this template use File | Settings | File Templates.
--%>
<%&#64; page contentType&#61;"text/html;charset&#61;UTF-8" language&#61;"java" %>
<html>
<head><title>员工数据库系统title><style>ul,iframe{float: left;}ul{width: 15%;}iframe{width: 80%;}style>
head>
<body><h1>欢迎来到员工管理系统h1><div><ul><li><a href&#61;"/zxf/emp" target&#61;"show">员工管理a>li><li><a href&#61;"/zxf/add.jsp" target&#61;"show">添加员工a>li>ul><iframe name&#61;"show" height&#61;"800px">iframe>div>
body>
html>
点击员工管理&#xff0c;跳转到
package com.aaa.servelt;import com.aaa.dao.IDengDAO;
import com.aaa.dao.Impl.DengDAOImpl;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;
import java.util.Map;&#64;WebServlet("/emp") //main.jsp 的员工管理 跳转到这
public class EmpServlet extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取所有数据IDengDAO dao&#61;new DengDAOImpl();List
}
转发到 emp.jsp
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/4/19Time: 16:37To change this template use File | Settings | File Templates.
--%>
<%&#64; page contentType&#61;"text/html;charset&#61;UTF-8" language&#61;"java" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c"%>
<html>
<head><title>Titletitle>
head>
<body>
<table border&#61;"1" cellspacing&#61;"0" width&#61;"95%" align&#61;"center"><tr><th>员工编号th><th>员工姓名th><th>员工手机号th><th>入职时间th><th>员工部门th><th>员工角色th><th>员工状态th><th>员工操作th>tr><c:forEach var&#61;"emp" items&#61;"${list}"><tr><th>${emp.id}th><th>${emp.name}th><th>${emp.telephone}th><th>${emp.hiredate}th><th>${emp.dname}th><th>${emp.rname}th><th>${emp.state&#61;&#61;1?"在职":"离职"}th><th><%-- 修改和删除 有各自要跳转的servlet页面--%><a href&#61;"/zxf/delete?id&#61;${emp.id}">删除a><a href&#61;"/zxf/toUpdate?id&#61;${emp.id}">修改a>th>tr>c:forEach>table>
body>
html>
点节删除 跳转到DeleteEmpServlet
package com.aaa.servelt;import com.aaa.dao.IDengDAO;
import com.aaa.dao.Impl.DengDAOImpl;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;
&#64;WebServlet("/delete")
public class DeleteEmpServlet extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求参数String id &#61; req.getParameter("id");//调用dao 根据id删除员工。IDengDAO dao&#61;new DengDAOImpl();boolean delete &#61; dao.delete(Integer.parseInt(id));//重定向到 empresp.sendRedirect("/zxf/emp");}
}
点击修改员工 &#xff0c;跳转到ToUpdateServlet
package com.aaa.servelt;import com.aaa.dao.IDengDAO;
import com.aaa.dao.Impl.DengDAOImpl;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;&#64;WebServlet("/toUpdate")
/*修改 用户点击修改按钮请求到servlet先要获取 要修改的员工信息 共享给jsp 请求转发过去 展示给用户看*/
public class ToUpdate extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取参数String id &#61; req.getParameter("id");//根据ID获取员工信息IDengDAO dao&#61;new DengDAOImpl();Map
}
他会请求转发到 update.jsp
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/4/18Time: 9:14To change this template use File | Settings | File Templates.
--%>
<%&#64; page contentType&#61;"text/html;charset&#61;UTF-8" language&#61;"java" %>
<html>
<head><title>修改title><style type&#61;"text/css">fieldset{width: 300px;margin: 100px auto;}style>
head>
<body>
<fieldset id&#61;""><legend>修改员工legend><form action&#61;"/zxf/update" method&#61;"post"><input type&#61;"hidden" name&#61;"id" value&#61;"${emp.id}" /><div>员工姓名&#xff1a; <input name&#61;"name" value&#61;"${emp.name}" />div><div>员工手机&#xff1a; <input name&#61;"telephone" value&#61;"${emp.telephone}" />div><div>员工部门&#xff1a;<select name&#61;"deptID" id&#61;"deptID" ><option value&#61;"1" ${emp.deptID&#61;&#61;1?"selected":""} >总经办option><option value&#61;"2" ${emp.deptID&#61;&#61;2?"selected":""} >人事部option><option value&#61;"3" ${emp.deptID&#61;&#61;3?"selected":""} >业务部option>select>div><div>员工角色&#xff1a;<select name&#61;"roleID" id&#61;"roleID" ><option value&#61;"1" id&#61;"r1">超级管理员option><option value&#61;"2" id&#61;"r2">员工管理员option><option value&#61;"3" id&#61;"r3">业务管理员option>select>div><div>员工状态&#xff1a;<select name&#61;"state" ><option value&#61;"0" id&#61;"s0">离职option><option value&#61;"1" id&#61;"s1">在职option>select>div><button>修改button>form>fieldset>
<script>// 当前表单的表单元素 够不够// 让 角色下拉框中的 1 2 3
document.getElementById("r"&#43;${emp.roleID}).selected &#61; "selected";document.getElementById("s"&#43;${emp.state}).selected &#61; "selected";
script>body>
html>
这里的jsp页面会跳转到UpdateServlet
package com.aaa.servelt;import com.aaa.dao.IDengDAO;
import com.aaa.dao.Impl.DengDAOImpl;
import com.aaa.entity.Employee;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;
&#64;WebServlet("/update")
public class UpdateEmpServlet extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求参数 处理请求乱码req.setCharacterEncoding("UTF-8");req.setCharacterEncoding("UTF-8");// 1 获取请求参数String name &#61; req.getParameter("name");String telephone &#61; req.getParameter("telephone");String deptID &#61; req.getParameter("deptID");String roleID &#61; req.getParameter("roleID");String state &#61; req.getParameter("state");String id &#61; req.getParameter("id");Employee employee &#61; new Employee(Integer.parseInt(id), name, telephone, Integer.parseInt(deptID), Integer.parseInt(roleID), Integer.parseInt(state));//2 通过dao将数据添加到数据库中IDengDAO dao&#61;new DengDAOImpl();boolean update &#61; dao.update(employee);//3 重定向到/empresp.sendRedirect("/zxf/emp");}
}
他又回到了emp 就是EmpServlet。
到此 删除和修改的功能完成。
回到 man.jsp 点击添加 员工。他会跳转到 add.jsp 页面
<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/4/17Time: 11:48To change this template use File | Settings | File Templates.
--%>
<%&#64; page contentType&#61;"text/html;charset&#61;UTF-8" language&#61;"java" %>
<html>
<head><title>Titletitle><style type&#61;"text/css">fieldset{width: 300px;margin: 100px auto;}style>
head>
<body><fieldset id&#61;""><legend>添加员工legend><%-- 跳转到 /zxf/addEmp 这个servlet--%><form action&#61;"/zxf/addEmp" method&#61;"post"><div>员工姓名&#xff1a; <input name&#61;"name" />div><div>员工手机&#xff1a; <input name&#61;"telephone" />div><div>员工部门&#xff1a;<select name&#61;"deptID"><option value&#61;"1">总经办option><option value&#61;"2">人事部option><option value&#61;"3">业务部option>select>div><div>员工角色&#xff1a;<select name&#61;"roleID"><option value&#61;"1">超级管理员option><option value&#61;"2">员工管理员option><option value&#61;"3">业务管理员option>select>div><div>员工状态&#xff1a;<select name&#61;"state"><option value&#61;"0">离职option><option value&#61;"1">在职option>select>div><button>添加button>form>fieldset>body>
html>
这里的jsp 会跳转到AddServlet
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
&#64;WebServlet("/addEmp")
public class AddEmpServlet extends HttpServlet {&#64;Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//处理1.请求乱码---->浏览器发给 servlet 乱码// 2.响应乱码----->servlet 回传给浏览器之后乱码 不用处理 为啥&#xff1f; 因为我们将其交给了 jsp 来解决&#xff01;req.setCharacterEncoding("utf-8");// 1 获取请求参数String name &#61; req.getParameter("name");String telephone &#61; req.getParameter("telephone");String deptID &#61; req.getParameter("deptID");String roleID &#61; req.getParameter("roleID");String state &#61; req.getParameter("state");Employee employee &#61; new Employee(name,telephone,Integer.parseInt(deptID),Integer.parseInt(roleID),Integer.parseInt(state));// 2 调用dao 将数据添加到数据库中IDengDAO dao&#61;new DengDAOImpl();dao.add(employee);// 3 回传信息 成功/失败req.setAttribute("msg","添加成功");// 4 请求转发到 add.jspreq.getRequestDispatcher("/add.jsp").forward(req,resp);}
}
这里会将数据 传送给add.jsp。
好了&#xff0c;添加员工完成! 增删改查实现&#xff0c;舒服。
三、总结。
1.jsp是用来展示数据的
2.servlet 是用来进行业务操作的 请求转发和重定向。
3.bug虽然痛苦&#xff0c;但自己解决 的时候&#xff0c;也很开心。-------菜鸟的心得&#xff01;
四&#xff0c;展示所有的文件目录