基于javaweb+mysql的jsp+servlet学生选课管理系统(java+servlet+jsp+javascript+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet学生选课管理系统(java+servlet+jsp+javascript+mysql)
项目介绍
学生选课系统,有学生、老师、管理员三种角色。 学生端权限
学生登录系统 选择课程并提交确认 各科成绩查询 总成绩查询 成绩排名、各科成绩排名
老师端权限
登录系统 课程管理(实现对课程的CURD) 考试成绩录入 各种查询(学生的单科成绩、总成绩、排名) 各种统计(统计及格率、统计80分以上的同学等)
管理端权限
登录系统 实现对学生的CURD 实现对老师的CURD 实现各种查询、统计
学完这个项目后的收获:
掌握项目开发的流程 掌握开发中的简单权限控制 掌握前端页面如何与后端整合 宏观把握一个项目 掌握echarts图表技术
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
技术栈
前端基础(Html、css、JavaScript、Jquery) mysql数据库 Web容器(Tomcat) Jsp/Servlet
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,然后运行; 3. 将项目中news.properties配置文件中的数据库配置改为自己的配置; 4. 运行项目,在浏览器中输入localhost:8080/jsp_zxmszssys 管理员账号/密码:admin/123456 老师账号/密码:zhangping/123456 学生账号/密码:0001/123456
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getIntParameter(HttpServletRequest request,String name) {
if(StringUtils.isNoneBlank(request.getParameter(name))) {
return Integer.parseInt(request.getParameter(name));
}else {
return null;
}
}
}
@WebServlet("/student2")
public class StudentServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("======StudentServlet========");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("list".equals(method)) {
this.list(request, response);
}else if("add".equals(method)) {
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@WebFilter(urlPatterns= {"/*"})
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
httpServletRequest.setCharacterEncoding("utf-8");
chain.doFilter(httpServletRequest, httpServletResponse);
httpServletResponse.setContentType("text/html;charset=utf-8");
}
@Override
public void destroy() {
}
}
}
private void findById(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
Student student = DaoFactory.getInstance().getStudentDao().findById(Integer.parseInt(id));
request.setAttribute("student", student);
request.getRequestDispatcher("page/student/update.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
String stuPwd = request.getParameter("stuPwd");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuPwd(MD5.encrypByMd5(MD5.encrypByMd5(stuPwd)));
try {
DaoFactory.getInstance().getStudentDao().add(student);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
//当前页码
Integer pageNo = getIntParameter(request, "pageNo");
int pageSize = 10;
//当pageNo参数为空的时候,则默认为第1页
if(pageNo == null) {
pageNo = 1;
}
//总记录条数
Long totalCount = 0L;
try {
totalCount = DaoFactory.getInstance().getStudentDao().count(null);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//总页数
Long totalPage = totalCount/pageSize;
if(totalCount%pageSize!=0 || totalPage == 0) {
totalPage++;
}
//下一页
@WebServlet("/pwd")
public class PwdServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer id = Integer.parseInt(req.getParameter("id"));
String type = req.getParameter("type");
String pwd = req.getParameter("pwd");
String newPwd = req.getParameter("newPwd");
String newPwd2 = req.getParameter("newPwd2");
if(!newPwd.equals(newPwd2)) {
req.setAttribute("msg", "两次密码不一致");
req.getRequestDispatcher("pwd.jsp").forward(req, resp);
}else {
try {
if("0".equals(type)) {
DaoFactory.getInstance().getStudentDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
if("1".equals(type)) {
DaoFactory.getInstance().getTeacherDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
if("2".equals(type)) {
DaoFactory.getInstance().getAdminDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
req.setAttribute("msg", "修改成功");
req.getRequestDispatcher("pwd.jsp").forward(req, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
try {
DaoFactory.getInstance().getCourseDao().update(course);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void findById(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
PageInfo<Teacher> pageInfo = new PageInfo<>(1);
pageInfo.setPageSize(1000);
try {
Course course = DaoFactory.getInstance().getCourseDao().findById(Integer.parseInt(id));
pageInfo = DaoFactory.getInstance().getTeacherDao().list(null, pageInfo);
request.setAttribute("course", course);
request.setAttribute("teachers", pageInfo.getList());
request.getRequestDispatcher("page/course/update.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void v_add(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
PageInfo<Teacher> pageInfo = new PageInfo<>(1);
pageInfo.setPageSize(1000);
try {
pageInfo = DaoFactory.getInstance().getTeacherDao().list(null, pageInfo);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("teachers", pageInfo.getList());
request.getRequestDispatcher("page/course/add.jsp").forward(request, response);
}
private void add(HttpServletRequest request, HttpServletResponse response) {
String cName = request.getParameter("cName");
//老师ID
Integer tId = Integer.parseInt(request.getParameter("tId"));
Course course = new Course();
course.setcName(cName);
//new一个Teacher对象,然后设置ID,然后再设置course对象的setTeacher关联 一下即可
Teacher teacher = new Teacher();
teacher.settId(tId);
course.setTeacher(teacher);
try {
DaoFactory.getInstance().getCourseDao().add(course);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
} catch (SQLException e) {
throws ServletException, IOException {
String method = request.getParameter("method");
if ("query_range".equals(method)) {
this.query_range(request, response);
} else if ("query_jgl".equals(method)) {
this.query_jgl(request, response);
} else if ("query_teacher".equals(method)) {
this.query_teacher(request, response);
}
}
public void query_teacher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Teacher teacher = (Teacher) request.getSession().getAttribute("user");
try {
List<Map<String, Object>> list = DaoFactory.getInstance().getScDao().query_rangeByTid(teacher.gettId());
request.setAttribute("list1", list);
List<Map<String, Object>> list2 = DaoFactory.getInstance().getScDao().query_jglByTid(teacher.gettId());
request.setAttribute("list2", list2);
request.getRequestDispatcher("page/sc/query_teacher.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void query_range(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<Map<String, Object>> list = DaoFactory.getInstance().getScDao().query_range();
request.setAttribute("list", list);
request.getRequestDispatcher("page/sc/query_range.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void query_jgl(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<Map<String, Object>> list = DaoFactory.getInstance().getScDao().query_jgl();
request.setAttribute("list", list);
request.getRequestDispatcher("page/sc/query_jgl.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("list".equals(method)) {
this.list(request, response);
}else if("add".equals(method)) {
this.add(request, response);
}else if("edit".equals(method)) {
this.findById(request, response);
}else if("editsubmit".equals(method)) {
this.editsubmit(request, response);
}else if("delete".equals(method)) {
this.delete(request, response);
}else if("detail".equals(method)) {
this.detail(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
DaoFactory.getInstance().getStudentDao().delete(Integer.parseInt(id));
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (Exception e) {
e.printStackTrace();
}
}
private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
Integer stuId = Integer.parseInt(request.getParameter("stuId"));
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuId(stuId);
try {
DaoFactory.getInstance().getStudentDao().update(student);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
}
}
}
@WebFilter(urlPatterns= {"/*"},initParams= {
@WebInitParam(name="exclude",value="/login.jsp,/login,/noprivilige.jsp,.css,.png,.jpg,.js")
})
public class PermissionFilter implements Filter {
public static String excludeString ;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
excludeString = filterConfig.getInitParameter("exclude");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
Object user = httpServletRequest.getSession().getAttribute("user");
String uri = httpServletRequest.getRequestURI();
if(isExist(uri) || uri.equals(httpServletRequest.getContextPath()+"/")) {
chain.doFilter(httpServletRequest, httpServletResponse);
}else {
if(user != null) {
chain.doFilter(httpServletRequest, httpServletResponse);
}else {
httpServletResponse.sendRedirect("noprivilige.jsp");
}
PageInfo<Student> pageInfo = new PageInfo<>(pageNo);
try {
pageInfo = DaoFactory.getInstance().getStudentDao().list(student,pageInfo);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
request.setAttribute("pageInfo", pageInfo);
//回写到页面
request.setAttribute("student", student);
request.getRequestDispatcher("page/student/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getIntParameter(HttpServletRequest request,String name) {
if(StringUtils.isNoneBlank(request.getParameter(name))) {
return Integer.parseInt(request.getParameter(name));
}else {
return null;
}
}
}
@WebServlet("/sc")
public class SCServlet extends HttpServlet {
Integer stuId = getIntParameter(request, "stuId");
String stuName = request.getParameter("stuName");
String stuNo = request.getParameter("stuNo");
Student student = new Student();
student.setStuId(stuId);
student.setStuName(stuName);
student.setStuNo(stuNo);
//构造了一个pageInfo对象
PageInfo<Student> pageInfo = new PageInfo<>(pageNo);
try {
pageInfo = DaoFactory.getInstance().getStudentDao().list(student,pageInfo);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
request.setAttribute("pageInfo", pageInfo);
//回写到页面
request.setAttribute("student", student);
request.getRequestDispatcher("page/student/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getIntParameter(HttpServletRequest request,String name) {
if(StringUtils.isNoneBlank(request.getParameter(name))) {
return Integer.parseInt(request.getParameter(name));
}else {
return null;
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("======StudentServlet========");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("list".equals(method)) {
this.list(request, response);
}else if("add".equals(method)) {
this.add(request, response);
}else if("edit".equals(method)) {
this.findById(request, response);
}else if("editsubmit".equals(method)) {
this.editsubmit(request, response);
}else if("delete".equals(method)) {
this.delete(request, response);
}else if("detail".equals(method)) {
this.detail(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
DaoFactory.getInstance().getStudentDao().delete(Integer.parseInt(id));
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (Exception e) {
e.printStackTrace();
}
}
private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
Integer stuId = Integer.parseInt(request.getParameter("stuId"));
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuId(stuId);
try {
DaoFactory.getInstance().getStudentDao().update(student);
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void findById(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
Student student = DaoFactory.getInstance().getStudentDao().findById(Integer.parseInt(id));
request.setAttribute("student", student);
request.getRequestDispatcher("page/student/update.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void detail(HttpServletRequest request, HttpServletResponse response) {
Student entity = (Student)request.getSession().getAttribute("user");
try {
Student student = DaoFactory.getInstance().getStudentDao().findById(entity.getStuId());
List<Sc> list = DaoFactory.getInstance().getScDao().listByStuId(entity.getStuId());
request.setAttribute("student", student);
for(Sc sc:list) {
Course c = DaoFactory.getInstance().getCourseDao().findById(sc.getcId());
sc.setcName(c.getcName());
}
request.setAttribute("list", list);
request.getRequestDispatcher("page/student/detail.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
String stuPwd = request.getParameter("stuPwd");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuPwd(MD5.encrypByMd5(MD5.encrypByMd5(stuPwd)));
try {
DaoFactory.getInstance().getStudentDao().add(student);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
e.printStackTrace();
public class PwdServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer id = Integer.parseInt(req.getParameter("id"));
String type = req.getParameter("type");
String pwd = req.getParameter("pwd");
String newPwd = req.getParameter("newPwd");
String newPwd2 = req.getParameter("newPwd2");
if(!newPwd.equals(newPwd2)) {
req.setAttribute("msg", "两次密码不一致");
req.getRequestDispatcher("pwd.jsp").forward(req, resp);
}else {
try {
if("0".equals(type)) {
DaoFactory.getInstance().getStudentDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
if("1".equals(type)) {
DaoFactory.getInstance().getTeacherDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
if("2".equals(type)) {
DaoFactory.getInstance().getAdminDao().update(MD5.encrypByMd5(MD5.encrypByMd5(newPwd)),id);
}
req.setAttribute("msg", "修改成功");
req.getRequestDispatcher("pwd.jsp").forward(req, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
this.list(request, response);
}else if("add".equals(method)) {
this.add(request, response);
}else if("v_add".equals(method)) {
this.v_add(request, response);
}else if("edit".equals(method)) {
this.findById(request, response);
}else if("editsubmit".equals(method)) {
this.editsubmit(request, response);
}else if("delete".equals(method)) {
this.delete(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
DaoFactory.getInstance().getCourseDao().delete(Integer.parseInt(id));
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
} catch (Exception e) {
e.printStackTrace();
}
}
private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
String cName = request.getParameter("cName");
Integer cId = getIntParameter(request, "cId");
//老师ID
Integer tId = Integer.parseInt(request.getParameter("tId"));
Course course = new Course();
course.setcId(cId);
course.setcName(cName);
//new一个Teacher对象,然后设置ID,然后再设置course对象的setTeacher关联 一下即可
Teacher teacher = new Teacher();
teacher.settId(tId);
course.setTeacher(teacher);
try {
DaoFactory.getInstance().getCourseDao().update(course);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
private void list(HttpServletRequest request, HttpServletResponse response) {
//当前页码
Integer pageNo = getIntParameter(request, "pageNo");
//查询条件
Integer cId = getIntParameter(request, "cId");
String cName = request.getParameter("cName");
String tName = request.getParameter("tName");
String userName = request.getParameter("userName");
Course course = new Course();
course.setcId(cId);
course.setcName(cName);
Teacher teacher = new Teacher();
teacher.settName(tName);
teacher.setUserName(userName);
course.setTeacher(teacher);
//构造了一个pageInfo对象
PageInfo<Course> pageInfo = new PageInfo<>(pageNo);
try {
pageInfo = DaoFactory.getInstance().getCourseDao().list(course,pageInfo);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
request.setAttribute("pageInfo", pageInfo);
//回写到页面
request.setAttribute("course", course);
request.getRequestDispatcher("page/course/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public Integer getIntParameter(HttpServletRequest request,String name) {
if(StringUtils.isNoneBlank(request.getParameter(name))) {
return Integer.parseInt(request.getParameter(name));
}else {
return null;
}
}
}
}else if("delete".equals(method)) {
this.delete(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
DaoFactory.getInstance().getStudentDao().delete(Integer.parseInt(id));
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (Exception e) {
e.printStackTrace();
}
}
private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
Integer stuId = Integer.parseInt(request.getParameter("stuId"));
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuId(stuId);
try {
DaoFactory.getInstance().getStudentDao().update(student);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void findById(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
Student student = DaoFactory.getInstance().getStudentDao().findById(Integer.parseInt(id));
request.setAttribute("student", student);
request.getRequestDispatcher("page/student/update.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
}
}
private void add(HttpServletRequest request, HttpServletResponse response) {
String stuNo = request.getParameter("stuNo");
String stuName = request.getParameter("stuName");
String stuPwd = request.getParameter("stuPwd");
Student student = new Student();
student.setStuName(stuName);
student.setStuNo(stuNo);
student.setStuPwd(MD5.encrypByMd5(MD5.encrypByMd5(stuPwd)));
try {
DaoFactory.getInstance().getStudentDao().add(student);
//直接重定向到列表页面
response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
//当前页码
Integer pageNo = getIntParameter(request, "pageNo");
int pageSize = 10;
//当pageNo参数为空的时候,则默认为第1页
if(pageNo == null) {
pageNo = 1;
}
//总记录条数
Long totalCount = 0L;
try {
totalCount = DaoFactory.getInstance().getStudentDao().count(null);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//总页数
Long totalPage = totalCount/pageSize;
if(totalCount%pageSize!=0 || totalPage == 0) {
totalPage++;
}
//下一页
Integer nextPage = 0;
if(nextPage<totalPage) {
nextPage = pageNo+1;
}else {
nextPage = pageNo;
}
//上一页
Integer prePage = 0;