该项目源码地址:源码地址请点击这里哟!
AdminBean.java
对数据库里的用户名的表的数据进行封装。
package com.bean; public class AdminBean { String admin; String password; String type; public String getAdmin() { return admin; } public void setAdmin(String admin) { this.admin = admin; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
StudentBean.java
对数据库里的学生信息的表的数据进行封装。
package com.bean; public class StudentBean { private String sid; private String sname; private String scls; private String age; private String major; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getScls() { return scls; } public void setScls(String scls) { this.scls = scls; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } }
AdminDao.java
实现登录和注册的方法。
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.bean.AdminBean; import com.util.DBUtil; public class AdminDao { //注册 public static boolean register(String admin,String password,String type) { Connection conn = DBUtil.getConnectDb(); String sql = "insert into auser (admin,password,type) values(?,?,?)"; int a = 0; PreparedStatement stm = null; boolean sta = false; try { stm = conn.prepareStatement(sql); stm.setString(1, admin); stm.setString(2, password); stm.setString(3, type); a = stm.executeUpdate(); if(a == 0) { sta = false; }else { sta = true; } return sta; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } //登录 public static AdminBean login(String admin,String password) { Connection conn = DBUtil.getConnectDb(); String sql = "select * from auser where admin='"+admin+"' and password='"+password+"'"; ResultSet rs; PreparedStatement stm = null; AdminBean adimBean = new AdminBean(); try { stm = conn.prepareStatement(sql); rs = stm.executeQuery(); if(rs.next()) { adimBean.setAdmin(rs.getString("admin")); adimBean.setPassword(rs.getString("password")); adimBean.setType(rs.getString("type")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return adimBean; } }
StudentDao.java
对学生信息进行增加、删除(根据学生学号删除某一条信息+批量删除)、修改、查询(查询数据库中所有的学生信息+根据学生的学号查询某一条信息)
package com.dao; 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 com.bean.StudentBean; import com.util.DBUtil; public class StudentDao { // 添加 public static boolean addStudent(String sid, String sname, String scls, String age, String major) { Connection conn = DBUtil.getConnectDb(); String sql = "INSERT INTO stus VALUES(?,?,?,?,?)"; int rs; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.setString(1, sid); stm.setString(2, sname); stm.setString(3, scls); stm.setString(4, age); stm.setString(5, major); boolean a = false; rs = stm.executeUpdate(); if (rs == 1) { return true; } else { return false; } } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); return false; } } // 更改 public static boolean updateStudent(String sid, String sname, String scls, String age, String major) { Connection conn = DBUtil.getConnectDb(); String sql = "UPDATE stus SET sname=?,scls=?,age=?,major=? WHERE sid=?"; int rs; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.setString(1, sname); stm.setString(2, scls); stm.setString(3, age); stm.setString(4, major); stm.setString(5, sid); boolean a = false; rs = stm.executeUpdate(); if (rs == 1) { return true; } else { return false; } } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); return false; } } // 删除 public static boolean deleteStudent(String sid) { Connection conn = DBUtil.getConnectDb(); String sql = "DELETE FROM stus WHERE sid=?"; int rs; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.setString(1, sid); boolean a = false; rs = stm.executeUpdate(); if (rs == 1) { return true; } else { return false; } } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); return false; } } // 查询所有信息 public static ListgetList() { List studentArray = new ArrayList (); // ArrayList studentArray = new ArrayList(); Connection conn = DBUtil.getConnectDb(); String sql = "SELECT * FROM stus"; PreparedStatement stm = null; ResultSet rs = null; try { stm = conn.prepareStatement(sql); rs = stm.executeQuery(); while (rs.next()) { StudentBean m = new StudentBean(); m.setSid(rs.getString("sid")); m.setSname(rs.getString("sname")); m.setScls(rs.getString("scls")); m.setAge(rs.getString("age")); m.setMajor(rs.getString("major")); studentArray.add(m); } rs.close(); stm.close(); return studentArray; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // 根据学号查询单个学生信息 public static ArrayList getList(String sid) { ArrayList studentArray = new ArrayList(); // 建立一个数组集合 Connection conn = DBUtil.getConnectDb(); String sql = "SELECT * FROM stus WHERE sid=?"; PreparedStatement stm = null; ResultSet rs = null; try { stm = conn.prepareStatement(sql); stm.setString(1, sid); rs = stm.executeQuery(); while (rs.next()) { StudentBean m = new StudentBean(); m.setSid(rs.getString("sid")); m.setSname(rs.getString("sname")); m.setScls(rs.getString("scls")); m.setAge(rs.getString("age")); m.setMajor(rs.getString("major")); studentArray.add(m); } rs.close(); stm.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return studentArray; } //批量删除 public void piLiangDelete(String ids) { // TODO Auto-generated method stub Connection conn = DBUtil.getConnectDb(); String sql = "DELETE FROM stus WHERE sid in ("+ids+")"; PreparedStatement stm = null; try { stm = conn.prepareStatement(sql); stm.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
LoginFilter.java
对该信息管理系统进行过滤,当没有登录的情况下是不能访问其他页面(包括增删改查+学生界面+教师界面),这样做相对来说比较安全。
package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebFilter("/*") public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; HttpSession session = request.getSession(); String requestURI = request.getRequestURI(); System.out.println(requestURI); if(requestURI.contains("/login.jsp") ||requestURI.contains("/LoginServlet") ||requestURI.contains("/LoginDelete") ||requestURI.contains("/register.jsp") ||requestURI.contains("/Login.css") ||requestURI.contains("/Register.css") ||requestURI.contains("/RegisterServlet")) { chain.doFilter(req, res); return; } if(session.getAttribute("username")!=null) { chain.doFilter(req, res); }else{ response.sendRedirect("login.jsp"); } } }
AddStudentServlet.java
添加学生信息。
package com.servlet; import java.io.IOException; 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 com.dao.StudentDao; /** * Servlet implementation class AddStudentServlet */ @WebServlet("/AddStudentServlet") public class AddStudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AddStudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String id = request.getParameter("sid"); String name = request.getParameter("sname"); String cls = request.getParameter("scls"); String age = request.getParameter("age"); String major = request.getParameter("major"); boolean a = StudentDao.addStudent(id, name, cls, age, major); if(a == true) { request.getSession().setAttribute("B2", "添加学生信息成功!"); response.sendRedirect("/StudentsMessageDemo/index.jsp"); }else { request.getSession().setAttribute("B2", "添加失败,请检查学号!"); response.sendRedirect("/StudentsMessageDemo/index.jsp"); } } }
DeleteStudentServlet.java
根据学生学号sid对学生信息进行删除。
package com.servlet; import java.io.IOException; 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 com.dao.StudentDao; /** * Servlet implementation class DeleteStudentServlet */ @WebServlet("/DeleteStudentServlet") public class DeleteStudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteStudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String id = request.getParameter("sid"); System.out.println("id="+id); StudentDao studentDao = new StudentDao(); boolean a; a = studentDao.deleteStudent(id); if(a == true) { request.getRequestDispatcher("SelectServlet1").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
LoginOut.java
销毁当前会话域中的所有属性,对用户进行注销,退出用户访问的界面,返回到登录界面。
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginOut */ @WebServlet("/LoginOut") public class LoginOut extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginOut() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.getSession().invalidate(); response.sendRedirect("/StudentsMessageDemo/login.jsp"); } }
LoginServlet.java
用户的登录。
package com.servlet; import java.io.IOException; 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 com.bean.AdminBean; import com.dao.AdminDao; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); //获取注册信息 String username = request.getParameter("u"); String password = request.getParameter("p"); String type = request.getParameter("type"); AdminDao dao = new AdminDao(); try { AdminBean bean = dao.login(username, password); HttpSession session = request.getSession(); session.setAttribute("username",username); String type1 = bean.getType(); if(type1.equals(type)){ if(type.equals("学生")) response.sendRedirect("/StudentsMessageDemo/indexStu.jsp"); if(type.equals("教师")) response.sendRedirect("/StudentsMessageDemo/index.jsp"); } } catch (Exception e) { request.getSession().setAttribute("A2", "账号或密码错误请重新登录!"); response.sendRedirect("/StudentsMessageDemo/login.jsp"); } } }
PiLiangDeleteServlet.java
批量删除学生信息(也可以进行全部删除和单条数据删除)。
package com.servlet; import java.io.IOException; 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 com.dao.StudentDao; /** * Servlet implementation class PiLiangDeleteServlet */ @WebServlet("/PiLiangDeleteServlet") public class PiLiangDeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public PiLiangDeleteServlet() { super(); // TODO Auto-generated constructor stub } /** * @param action * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String action = request.getParameter("action"); if("deleteusers".equals(action)) { String sid = request.getParameter("sid"); StudentDao studentDao = new StudentDao(); studentDao.piLiangDelete(sid); response.sendRedirect("/StudentsMessageDemo/index.jsp"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
RegisterServlet.java
注册用户信息。
package com.servlet; import java.io.IOException; 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 com.dao.AdminDao; /** * Servlet implementation class RegisterServlet */ @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RegisterServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); //获取注册信息 String username = request.getParameter("u"); String password = request.getParameter("p"); String type = request.getParameter("type"); boolean a = AdminDao.register(username, password,type); //将注册信息存入数据库,在返回登录 if(a == true) { request.getSession().setAttribute("A1", "注册账号成功!"); response.sendRedirect("/StudentsMessageDemo/register.jsp"); }else { request.getSession().setAttribute("A1", "注册账号失败!"); response.sendRedirect("/StudentsMessageDemo/register.jsp"); } } }
SelectServlet.java
学生界面显示的列表数据查询。
package com.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; 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 com.bean.StudentBean; import com.dao.StudentDao; /** * Servlet implementation class SelectServlet */ @WebServlet("/SelectServlet") public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); Lista = StudentDao.getList(); request.setAttribute("studentArray", a); RequestDispatcher dispatcher = request.getRequestDispatcher("indexStu.jsp"); dispatcher.forward(request, response); } }
SelectServlet1.java
教师界面显示的列表数据查询。
package com.servlet; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; 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 com.bean.StudentBean; import com.dao.StudentDao; /** * Servlet implementation class SelectServlet1 */ @WebServlet("/SelectServlet1") public class SelectServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectServlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); Lista = StudentDao.getList(); request.setAttribute("studentArray", a); RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); dispatcher.forward(request, response); } }
SelectStudentServlet.java
教师界面查询按钮的实现(当文本框中有数据<学号>时,查询的时单条学生信息,当文本框数据为空时,查询的则是数据库中的所有数据)。
package com.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; 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 com.bean.StudentBean; import com.dao.StudentDao; /** * Servlet implementation class SelectStudentServlet */ @WebServlet("/SelectStudentServlet") public class SelectStudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectStudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); //获取注册信息 String id = request.getParameter("sid"); Lista; if(id.equals("")) { a = StudentDao.getList(); }else { a = StudentDao.getList(id); } request.setAttribute("studentArray", a); RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); dispatcher.forward(request, response); } }
SelectStudentServlet2.java
学生界面查询按钮的实现(当文本框中有数据<学号>时,查询的时单条学生信息,当文本框数据为空时,查询的则是数据库中的所有数据)。
package com.servlet; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; 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 com.bean.StudentBean; import com.dao.StudentDao; /** * Servlet implementation class SelectStudentServlet2 */ @WebServlet("/SelectStudentServlet2") public class SelectStudentServlet2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectStudentServlet2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); //获取注册信息 String id = request.getParameter("sid"); Lista; if(id.equals("")) { a = StudentDao.getList(); }else { a = StudentDao.getList(id); } request.setAttribute("studentArray", a); RequestDispatcher dispatcher = request.getRequestDispatcher("indexStu.jsp"); dispatcher.forward(request, response); } }
UpdateStudentServlet.java
修改时对丢改的数据进行回显(回显到修改页面)。
package com.servlet; import java.io.IOException; import java.util.ArrayList; 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 com.bean.StudentBean; import com.dao.StudentDao; /** * Servlet implementation class UpdateStudentServlet */ @WebServlet("/UpdateStudentServlet") public class UpdateStudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateStudentServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String sid = request.getParameter("sid"); //System.out.println("id="+id); StudentDao studentDao = new StudentDao(); ArrayList stu = studentDao.getList(sid); request.setAttribute("studentArray", stu); request.getRequestDispatcher("update.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
UpdateStudentServlet1.java
对学生信息进行修改。
package com.servlet; import java.io.IOException; 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 com.dao.StudentDao; /** * Servlet implementation class UpdateStudentServlet1 */ @WebServlet("/UpdateStudentServlet1") public class UpdateStudentServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateStudentServlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String id = request.getParameter("sid"); String name = request.getParameter("sname"); String cls = request.getParameter("scls"); String age = request.getParameter("age"); String major = request.getParameter("major"); boolean a = StudentDao.updateStudent(id, name, cls, age, major); if(a == true) { response.sendRedirect("/StudentsMessageDemo/index.jsp"); }else { response.sendRedirect("/StudentsMessageDemo/index.jsp"); } } }
DBUtil.java
连接MYSQL数据库。
package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static String url="jdbc:mysql://localhost:3306/students?serverTimezone=UTC&useSSL=true&characterEncoding=utf-8"; public static String username="root"; public static String password="1234"; static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { System.out.println(e); } } public static Connection getConnectDb() { Connection conn = null; try { System.out.println("正在连接数据库...."); conn=DriverManager.getConnection(url,username,password); System.out.println("连接成功"); } catch (SQLException e) { System.out.println(e); } return conn; } }
StudentsMessageDemo index.html index.jsp index.htm default.html default.jsp default.htm LoginFilter com.filter.LoginFilter LoginFilter /*
Login.css
登陆界面的css样式。
@charset "UTF-8"; html{ width: 100%; height: 100%; overflow: hidden; font-style: sans-serif; } body{ width: 100%; height: 100%; font-family: 'Open Sans',sans-serif; margin: 0; background-color: #808069; } #login{ position: absolute; top: 50%; left:50%; margin: -150px 0 0 -150px; width: 300px; height: 300px; } #login h1{ color: #fff; text-shadow:0 0 10px; letter-spacing: 1px; text-align: center; } h1{ font-size: 2em; margin: 0.67em 0; } input{ width: 278px; height: 18px; margin-bottom: 10px; outline: none; padding: 10px; font-size: 13px; color: #fff; text-shadow:1px 1px 1px; border-top: 1px solid #312E3D; border-left: 1px solid #312E3D; border-right: 1px solid #312E3D; border-bottom: 1px solid #56536A; border-radius: 4px; background-color: #2D2D3F; } .but{ width: 300px; min-height: 20px; display: block; background-color: #4a77d4; border: 1px solid #3762bc; color: #fff; padding: 9px 14px; font-size: 15px; line-height: normal; border-radius: 5px; margin: 0; }
Register.css
注册界面的css样式。
@charset "UTF-8"; html{ width: 100%; height: 100%; overflow: hidden; font-style: sans-serif; } body{ width: 100%; height: 100%; font-family: 'Open Sans',sans-serif; margin: 0; background-color: #808A87; } #login{ position: absolute; top: 50%; left:50%; margin: -150px 0 0 -150px; width: 300px; height: 300px; } #login h1{ color: #fff; text-shadow:0 0 10px; letter-spacing: 1px; text-align: center; } h1{ font-size: 2em; margin: 0.67em 0; } input{ width: 278px; height: 18px; margin-bottom: 10px; outline: none; padding: 10px; font-size: 13px; color: #fff; text-shadow:1px 1px 1px; border-top: 1px solid #312E3D; border-left: 1px solid #312E3D; border-right: 1px solid #312E3D; border-bottom: 1px solid #56536A; border-radius: 4px; background-color: #2D2D3F; } .but{ width: 300px; min-height: 20px; display: block; background-color: #4a77d4; border: 1px solid #3762bc; color: #fff; padding: 9px 14px; font-size: 15px; line-height: normal; border-radius: 5px; margin: 0; }
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>增加操作
index.jsp
教师访问的主界面。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.bean.StudentBean" %> <%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>你好!欢迎${username} 退出增删改查界面 学生信息管理系统后台
学号 | 姓名 | 班级 | 年龄 | 专业 | 操作 | |
---|---|---|---|---|---|---|
${stu.sid } | ${stu.sname } | ${stu.scls } | ${stu.age } | ${stu.major } | 修改 删除 |
indexStu.jsp
学生访问的主界面。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.bean.StudentBean" %> <%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>你好!欢迎${username} 退出学生信息管理系统 学生信息管理系统
学号 | 姓名 | 班级 | 年龄 | 专业 |
---|---|---|---|---|
${stu.sid } | ${stu.sname } | ${stu.scls } | ${stu.age } | ${stu.major } |
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>登陆界面 学生信息管理系统
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>注册界面 注册
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>修改操作
登录界面:
注册界面:
学生界面:
教师界面(前面的复选框可进行批量删除):
修改界面(没有显示学号是因为对学号进行了隐藏):
添加页面:
数据库: