实现对部门信息的分类管理,对不同部门人员的管理(增删改查),新用户的注册和登陆等
功能分析:
- 实现用户的登陆
- 实现用户的注册(注册不同的部门和职位)
- 登陆之后会显示所有员工和经理的信息
- 实现对这些信息的删除和更新操作
下面看一下效果图吧
登陆页面
注册页面
登陆之后页面
点击删除ID为10之后的页面
点击更新之后的页面
这个系统一共有两大步
1 . 数据库
2 . 后台功能的实现
下面我们将实现这两大步
一 数据库内容
数据库内容自行设计和填充数据!!!
二 后台功能代码
目录结构图
下面正式欣赏代码
@1 bean包
bean包里的Department.java代码
package com.hnpi.bean;public class Department {private int id;private String branch;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getBranch() {return branch;}public void setBranch(String branch) {this.branch = branch;}public Department() {super();// TODO Auto-generated constructor stub}public Department(int id, String branch) {super();this.id = id;this.branch = branch;}}
bean包里的User.java代码
package com.hnpi.bean;public class User {private int id;private String name;private String pwd;private String rname;private String branch;private String role;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 getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getRname() {return rname;}public void setRname(String rname) {this.rname = rname;}public String getBranch() {return branch;}public void setBranch(String branch) {this.branch = branch;}public String getRole() {return role;}public void setRole(String role) {this.role = role;}public User() {super();// TODO Auto-generated constructor stub}public User(int id, String name, String pwd, String rname, String branch,String role) {super();this.id = id;this.name = name;this.pwd = pwd;this.rname = rname;this.branch = branch;this.role = role;}}
@2 servlet包
servlet 包里的LoginServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String name = request.getParameter("name");String pwd = request.getParameter("pwd");request.setCharacterEncoding("utf-8");response.setContentType("text/html");boolean flag = false;Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from users where name = ? and pwd = ?";try {ps = conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);rs = ps.executeQuery();if(rs.next()){flag = true;}else{flag = false;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}if(flag){response.sendRedirect("list");}else{response.sendRedirect("login.jsp");}}}
servlet 包里的RegisterServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class RegisterServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");String name = request.getParameter("name");String pwd = request.getParameter("pwd");String rname = request.getParameter("rname");String branch = request.getParameter("branch");String role = request.getParameter("role");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "insert into users(name,pwd,rname,branch,role) values(?,?,?,?,?)";try {ps = conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);ps.setString(3, rname);ps.setString(4, branch);ps.setString(5, role);ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("login.jsp");}}
servlet 包里的ToRegisterServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.hnpi.bean.Department;
import com.hnpi.util.DBUtil;public class ToRegisterServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from department";List<Department> depts = new ArrayList<Department>();try {ps = conn.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){Department dept = new Department();dept.setId(rs.getInt(1));dept.setBranch(rs.getString(2));depts.add(dept);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("deptList", depts);response.sendRedirect("register.jsp");}}
servlet 包里的UserDelServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class UserDelServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String id = request.getParameter("id");request.setCharacterEncoding("utf-8");response.setContentType("text/html");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "delete from users where id = ?";try {ps = conn.prepareStatement(sql);ps.setString(1, id);ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("list");}}
servlet 包里的UserListServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.hnpi.bean.Department;
import com.hnpi.bean.User;
import com.hnpi.util.DBUtil;public class UserListServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from users";List<User> users = new ArrayList<User>();try {ps = conn.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()){User user = new User();user.setId(rs.getInt(1));user.setName(rs.getString(2));user.setPwd(rs.getString(3));user.setRname(rs.getString(4));user.setBranch(rs.getString(5));user.setRole(rs.getString(6));users.add(user);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("userList", users);response.sendRedirect("userList.jsp");}}
servlet 包里的UserUpdateServlet.java代码
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class UserUpdateServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");String id = request.getParameter("id");String name = request.getParameter("name");String pwd = request.getParameter("pwd");String rname = request.getParameter("rname");String branch = request.getParameter("branch");String role = request.getParameter("role");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "update users set name = ?, pwd = ?, rname = ?, branch = ?, role = ? where id = ?";try {ps = conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);ps.setString(3, rname);ps.setString(4, branch);ps.setString(5, role);ps.setInt(6, Integer.parseInt(id));ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("list");}}
@3 util包
DBUtil.java
package com.hnpi.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {public static Connection getConn(){String url = "jdbc:sqlserver://localhost:1433;databaseName=Staff";String user = "sa";String pwd = "1";Connection conn = null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");conn = DriverManager.getConnection(url, user, pwd);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
@4 JSP页面
login.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="login" method="post">账号:<input type="text" name="name"><br>密码:<input type="password" name="pwd"><br><input type="submit" value="登陆"><a href="toRegister">注册</a></form></body>
</html>
register.jsp页面
<%@page import="com.hnpi.bean.Department"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'register.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action="register" method="post">账号:<input type="text" name="name"><br>密码:<input type="password" name="pwd"><br>真实姓名:<input type="text" name="rname"><br>部门:<select name="branch"><%List<Department> depts = (List)session.getAttribute("deptList");for(Department dept : depts){%><option id="<%=dept.getId() %>"><%=dept.getBranch() %></option><%}%></select><br>角色:<select name="role"><option>经理</option><option>员工</option></select><input type="submit" value="注册"></form></body>
</html>
userList.jsp页面
<%@page import="com.hnpi.bean.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'userList.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><table><thead><tr><th>ID</th><th>账号</th><th>密码</th><th>真实姓名</th><th>部门</th><th>角色</th><th colspan="2">操作</th></tr></thead><tbody><%List<User> users = (List)session.getAttribute("userList");for(User user : users){%><tr><td><%=user.getId() %></td><td><%=user.getName() %></td><td><%=user.getPwd() %></td><td><%=user.getRname() %></td><td><%=user.getBranch() %></td><td><%=user.getRole() %></td><td><a href="del?id=<%=user.getId() %>">删除</a></td><td><a href="userUpdate.jsp?id=<%=user.getId() %>">更新</a></td></tr><%}%></tbody></table></body>
</html>
userUpdate.jsp页面
<%@page import="com.hnpi.bean.Department"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.hnpi.util.DBUtil"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'userUpdate.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%String id = request.getParameter("id");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from users where id = ?";try {ps = conn.prepareStatement(sql);ps.setInt(1, Integer.parseInt(id));rs = ps.executeQuery();while(rs.next()){String name = rs.getString("name");String pwd = rs.getString("pwd");String rname = rs.getString("rname");String branch = rs.getString("branch");String role = rs.getString("role");%><form action="update" method="post">ID:<input type="text" readonly="readonly" name="id" value="<%=id %>"><br>账号:<input type="text" name="name" value="<%=name %>"><br>密码:<input type="password" name="pwd" value="<%=pwd %>"><br>真实姓名:<input type="text" name="rname" value="<%=rname %>"><br>部门:<select name="branch"><option value="生产部"<%if(branch.equals("生产部")){%>selected<%}%>>生产部</option><option value="销售部"<%if(branch.equals("销售部")){%>selected<%}%>>销售部</option><option value="研发部"<%if(branch.equals("研发部")){%>selected<%}%>>研发部</option></select><br>角色:<select name="role"><option>经理</option><option>员工</option></select><br><input type="submit" value="注册"></form><%}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}%></body>
</html>
看到这里相信你已经有深刻的了解,快去尝试一下吧。
获取更多关注我呦!!!