作为一位初学编程的学习者有哪些不好的地方还望各位海涵,感谢大家支持!
该项目源码地址:源码地址请点击这里哟!
项目布局:
Bean层:
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;
}
}
dao层:
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 List<StudentBean> getList() {
List<StudentBean> studentArray = new ArrayList<StudentBean>();
// 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();
}
}
}
Filter层:
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");
}
}
}
Servlet层:
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");
List<StudentBean> a = 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");
List<StudentBean> a = 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");
List<StudentBean> a;
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");
List<StudentBean> a;
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");
}
}
}
Util层:
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;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>StudentsMessageDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
static文件夹:
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;
}
JSP:
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加操作</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script>
//增加
function submitit1() {
document.form1.action = "AddStudentServlet"
//document.form1.submit();
}
</script>
</head>
<body>
<div class="container">
<div class="row clearfix">
<p></p><p></p><p></p>
<div class="col-md-12 column">
<form class="form-horizontal" role="form" name="form1" method="post">
<div class="form-group">
<label for="inputEmail31" class="col-sm-2 control-label">学号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputEmail31" name="sid" />
</div>
</div>
<div class="form-group">
<label for="inputPassword32" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword32" name="sname" />
</div>
</div>
<div class="form-group">
<label for="inputPassword33" class="col-sm-2 control-label">班级</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword33" name="scls" />
</div>
</div>
<div class="form-group">
<label for="inputPassword34" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword34" name="age" />
</div>
</div>
<div class="form-group">
<label for="inputPassword35" class="col-sm-2 control-label">专业</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPassword35" name="major" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onclick="submitit1()">添加学生信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增删改查界面</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
//查询
function submitit4() {
document.form2.action = "SelectStudentServlet"
}
//删除
function doDelete(sid) {
var flag = confirm("是否确定删除");
if(flag){
window.location.href="DeleteStudentServlet?sid="+sid;
}
}
//开头全选
function ckAll(){
var status=document.getElementById("p1").checked;
var childs=document.getElementsByName("child");
for(var i=0;i<childs.length;i++){
childs[i].checked=status;
}
}
//选中
function clickChild(){
var childs=document.getElementsByName("child");
var count=childs.length;
var CheckedCount=0;
for(var i=0;i<childs.length;i++){
if(childs[i].checked==true){
CheckedCount++;
}
}
//全选中
if(count==CheckedCount){
document.getElementById("p1").checked=true;
}else{
document.getElementById("p1").checked=false;
}
}
//批量删除
function piLiangDelete(){
var sid=new Array();
var childs=document.getElementsByName("child");
for(var i=0;i<childs.length;i++){
if(childs[i].checked==true){
var id=childs[i].value;
sid.push(id);
}
}
if(sid.length>0){
if(confirm("确定要删除选中的用户吗")){
location.href="PiLiangDeleteServlet?action=deleteusers&sid="+sid;
}
}else{
alert("请先选中要删除的用户数据!");
}
}
</script>
<style type="text/css">
h2{
font-family:宋体;
font-weight:normal;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h2 class="text-center">
学生信息管理系统后台
</h2>
</div>
</div>
<div class="row clearfix">
<div class="col-md-2 column">
<button type="submit" class="btn btn-default1" onclick="piLiangDelete();">批量删除</button>
</div>
<div class="col-md-2 column"></div>
<div class="col-md-6 column">
<form class="form-horizontal" role="form" name="form2">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">查找学生:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="sid" name="sid" />
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-default" onclick="submitit4()">查询</button>
</div>
<div class="col-sm-2">
<a href="/StudentsMessageDemo/add.jsp">添加信息</a>
</div>
</div>
</form>
</div>
<div class="col-md-2 column">
你好!欢迎${username} <a href="/StudentsMessageDemo/LoginOut">退出</a>
</div>
</div>
<p></p><hr style="border: 5px solid green;"/><p></p>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table">
<thead>
<tr>
<th>
<input type="checkbox" onclick="ckAll();" id="p1"/>
</th>
<th>
学号
</th>
<th>
姓名
</th>
<th>
班级
</th>
<th>
年龄
</th>
<th>
专业
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<c:if test="${empty requestScope.studentArray }">
<script>location="/StudentsMessageDemo/SelectServlet1";</script>
</c:if>
<c:forEach items="${requestScope.studentArray}" var="stu">
<tr class="success">
<td>
<input type="checkbox" name="child" value="${stu.sid }" onclick="clickChild();"/>
</td>
<td>
${stu.sid }
</td>
<td>
${stu.sname }
</td>
<td>
${stu.scls }
</td>
<td>
${stu.age }
</td>
<td>
${stu.major }
</td>
<td>
<a href="UpdateStudentServlet?sid=${stu.sid }">修改</a> <a href="#" onclick="doDelete(${stu.sid})">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script>
//根据学号查询
function submitit3() {
document.form3.action = "SelectStudentServlet2"
}
</script>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h2 class="text-center">
学生信息管理系统
</h2>
<div class="row clearfix">
<div class="col-md-5 column">
</div>
<div class="col-md-5 column">
<form class="form-horizontal" role="form" name="form3">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">个人查找:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="sid" name="sid" />
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-default" onclick="submitit3()">查询</button>
</div>
</div>
</form>
</div>
<div class="col-md-2 column">
你好!欢迎${username} <a href="/StudentsMessageDemo/LoginOut">退出</a>
</div>
</div>
<p></p><hr style="border: 5px solid green;"/><p></p>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table">
<thead>
<tr>
<th>
学号
</th>
<th>
姓名
</th>
<th>
班级
</th>
<th>
年龄
</th>
<th>
专业
</th>
</tr>
</thead>
<tbody>
<c:if test="${empty requestScope.studentArray }">
<script>location="/StudentsMessageDemo/SelectServlet";</script>
</c:if>
<c:forEach items="${requestScope.studentArray}" var="stu">
<tr class="warning">
<td>
${stu.sid }
</td>
<td>
${stu.sname }
</td>
<td>
${stu.scls }
</td>
<td>
${stu.age }
</td>
<td>
${stu.major }
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆界面</title>
<link rel="stylesheet" type="text/css" href="static/Login.css"/>
</head>
<body>
<div id="login">
<h1>学生信息管理系统</h1>
<form method="post" action="LoginServlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
用户类型:
<select name="type">
<option value="学生" name="xuesheng">学生</option>
<option value="教师" name="jiaoshi">教师</option>
</select><P></P>
<button class="but" type="submit">登录</button>
<label for="password">
没有账号请
<a href="register.jsp" style="color:blue">注册</a>
</label>
<br>
<label>
<%
String state = (String) session.getAttribute("A2");
session.removeAttribute("A2");
if(state != null){
out.print(state);
}
%>
</label>
</form>
</div>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<link rel="stylesheet" type="text/css" href="static/Register.css"/>
</head>
<body>
<div id="login">
<h1>注册</h1>
<form method="post" action="RegisterServlet">
<input type="text" required="required" placeholder="用户名" name="u"></input>
<input type="password" required="required" placeholder="密码" name="p"></input>
用户类型:
<select name="type">
<option value="学生" name="xuesheng">学生</option>
<option value="教师" name="jiaoshi">教师</option>
</select><P></P>
<button class="but" type="submit">注册</button>
<label for="password">
返回
<a href="login.jsp" style="color:blue">登录</a>界面
</label>
<br>
<label>
<%
String state = (String) session.getAttribute("A1");
session.removeAttribute("A1");
if(state != null){
out.print(state);
}
%>
</label>
</form>
</div>
</body>
</html>
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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改操作</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script>
//修改
function submitit3() {
document.form1.action = "UpdateStudentServlet1"
}
</script>
</head>
<body>
<div class="container">
<p></p><p></p><p></p>
<div class="row clearfix">
<div class="col-md-12 column">
<form class="form-horizontal" role="form" name="form1" method="post">
<c:if test="${empty requestScope.studentArray }">
<script>location="/StudentsMessageDemo/UpdateStudentServlet";</script>
</c:if>
<c:forEach items="${requestScope.studentArray}" var="stu">
<input type="hidden" class="form-control" name="sid" value="${stu.sid }" />
<div class="form-group">
<label for="inputPassword32" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="sname" value="${stu.sname }" />
</div>
</div>
<div class="form-group">
<label for="inputPassword33" class="col-sm-2 control-label">班级</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="scls" value="${stu.scls }" />
</div>
</div>
<div class="form-group">
<label for="inputPassword34" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="age" value="${stu.age }"/>
</div>
</div>
<div class="form-group">
<label for="inputPassword35" class="col-sm-2 control-label">专业</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="major" value="${stu.major }"/>
</div>
</div>
</c:forEach>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" onclick="submitit3()">更改学生信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
最终效果图:
登录界面:
注册界面:
学生界面:
教师界面(前面的复选框可进行批量删除):
修改界面(没有显示学号是因为对学号进行了隐藏):
添加页面:
数据库:
文章来源:https://www.toymoban.com/news/detail-476702.html
文章来源地址https://www.toymoban.com/news/detail-476702.html
到了这里,关于JAVAWEB学生信息管理系统保姆级教程(增删改查+<普通用户和管理员>登录注册+Filter+mysql+批量删除信息+用户退出登录注销)eclipse版(升级版)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!