基于javaweb+mysql的jsp+servlet在线考试系统(java+jsp+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
网络考试系统主要用于实现高校在线考试,基本功能包括:自动组卷、试卷发布、试卷批阅、试卷成绩统计等。本系统结构如下:
后台管理员:系统功能、用户、角色、题目管理、试卷管理、
后台教师:题目管理、试卷管理
前台学生:查看试题、答题、查看错题
后台管理员
后台教师
前台学生
技术框架
JSP Servlet MySQL JDBC Tomcat jQuery Bootstrap
基于javaweb+mysql的JSP+Servlet在线考试系统(java+jsp+servlet+mysql)
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse rsp = (HttpServletResponse)response;
HttpSession session = req.getSession();
if(session==null || session.getAttribute("user")==null){
//处理未登陆问题。重定向到登陆页面
//String path = req.getContextPath();
//String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
rsp.sendRedirect("/projdemo/login.jsp");
return;
}
else{
System.out.println("成功执行操作");
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
public class Tools {
/**
/**
* 初始化修改页面
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Subject subject = new Subject();
subject.setSid(Integer.parseInt(request.getParameter("id")));
subject = service.detail(subject);
if(subject!=null){
request.setAttribute("item",subject);
request.getRequestDispatcher("/sys/subject/edit.jsp").forward(request, response);
}else{
request.setAttribute("msg", "需要修改的试题功能不存在。");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
/**
* 查询试题列表
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void list(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String scontent = request.getParameter("scontent");
Subject subject = new Subject();
if(scontent!=null && !scontent.equals("")){
subject.setScontent(scontent);
}
PageControl pc = new PageControl();
Integer currindex = 1;
if(request.getParameter("index")!=null){
currindex = Integer.parseInt(request.getParameter("index"));
}
IUserService service = new UserService();
IPaperService paperService = new PaperService();
IStudentpaperService spServece = new StudentpaperService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd= request.getParameter("cmd");
if(cmd.equals("paperlist")){
index(request,response);
}else if(cmd.equals("login")){
login(request,response);
}else if(cmd.equals("init")){
initpage(request,response);
}else if(cmd.equals("logout")){
logout(request,response);
}else if(cmd.equals("list")){
list(request,response);
}else if(cmd.equals("add")){
add(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}else if(cmd.equals("toeditpwd")){
toeditpwd(request,response);
}else if(cmd.equals("editpwd")){
editpwd(request,response);
}else if(cmd.equals("paper")){
paper(request,response);
}else if(cmd.equals("stulogin")){
stulogin(request,response);
}else if(cmd.equals("answer")){
answer(request,response);
}
}
/**
* 跳转首页
* @param request
* @param response
*/
private void index(HttpServletRequest request, HttpServletResponse response) {
Paper paper = new Paper();
//String pname = request.getParameter("pname");
//paper.setPname(pname);
List<Paper> papers = paperService.list(paper);
request.setAttribute("papers", papers);
try {
request.getRequestDispatcher("/user/index.jsp").forward(request, response);
} catch (ServletException e) {
public class RoleServlet extends HttpServlet {
IRoleService service = new RoleService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd = request.getParameter("cmd");
if(cmd.equals("list")){
list(request,response);
}else if(cmd.equals("add")){
add(request,response);
}else if(cmd.equals("initrole")){
initrole(request,response);
}else if(cmd.equals("saveright")){
saveright(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}
}
/**
* 保存角色权限
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void saveright(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String[] funids = request.getParameterValues("ckrr");
String roleid = request.getParameter("roleid");
Integer rtn = service.saveright(roleid, funids);
if(rtn>0){
response.sendRedirect(Tools.Basepath(request, response)+"sys/role?cmd=list");
}else{
request.setAttribute("msg", "保存角色权限失败");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
request.getRequestDispatcher("/sys/user/edit.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用户初始化修改页面
* @param request
* @param response
* @throws IOException
* @throws ServletException
* @throws IOException
* @throws ServletException
*/
private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Sysuser user = new Sysuser();
user.setUserid(Integer.parseInt(request.getParameter("id")));
user = service.detail(user);
if(user!=null){
request.setAttribute("item",user);
request.getRequestDispatcher("/sys/user/edit.jsp").forward(request, response);
}else{
request.setAttribute("msg", "需要修改的用户不存在。");
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
}
/**
* 修改用户密码功能
* @param request
* @param response
*/
private void editpwd(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
Sysuser user = new Sysuser();
try {
BeanUtils.populate(user, request.getParameterMap());
Integer rtn = service.editpwd(user);
}else if(cmd.equals("edit")){
edit(request,response);
}
}
/**
* 修改试题功能
* @param request
* @param response
*/
private void edit(HttpServletRequest request, HttpServletResponse response) {
Subject subject = new Subject();
try {
BeanUtils.populate(subject, request.getParameterMap());
Integer rtn = service.edit(subject);
if(rtn>0){
response.sendRedirect(Tools.Basepath(request, response)+"sys/subject?cmd=list");
}else{
request.setAttribute("msg", "编辑试题功能失败!");
request.getRequestDispatcher("/sys/subject/edit.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化修改页面
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Subject subject = new Subject();
subject.setSid(Integer.parseInt(request.getParameter("id")));
subject = service.detail(subject);
if(subject!=null){
request.setAttribute("item",subject);
request.getRequestDispatcher("/sys/subject/edit.jsp").forward(request, response);
}else{
request.setAttribute("msg", "需要修改的试题功能不存在。");
request.getRequestDispatcher("/error.jsp").forward(request, response);
*/
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Sysuser user = new Sysuser();
user.setUsername(request.getParameter("username"));
user.setUserpwd(request.getParameter("userpwd"));
user = service.login(user);
if(user==null){
request.setAttribute("msg", "用户名密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}else{
HttpSession session = request.getSession(true);
session.setAttribute("user", user);
initpage(request, response);
//request.getRequestDispatcher("/sys/user?cmd=init").forward(request, response);
}
}
/**
* 学生获取试题内容
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void paper(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Paper paper = new Paper();
String pname = new String(request.getParameter("pname").getBytes("utf-8"), "utf-8");
paper.setPname(pname);
List<Subject> subjects = paperService.subjectlist(paper);
request.setAttribute("subjects", subjects);
request.setAttribute("pname", pname);
request.getRequestDispatcher("/user/paper/paper.jsp").forward(request, response);
}
/**
* 提交回答问题
* @param request
* @param response
* @throws UnsupportedEncodingException
public class FunServlet extends HttpServlet {
IFunService service = new FunService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd = request.getParameter("cmd");
if(cmd.equals("add")){
addfun(request,response);
}else if(cmd.equals("list")){
funlist(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}
}
/**
* 修改系统功能
* @param request
* @param response
*/
private void edit(HttpServletRequest request, HttpServletResponse response) {
SysFunction fun = new SysFunction();
try {
BeanUtils.populate(fun, request.getParameterMap());
Integer rtn = service.edit(fun);
if(rtn>0){
response.sendRedirect(Tools.Basepath(request, response)+"sys/fun?cmd=list");
}else{
request.setAttribute("msg", "保存系统功能失败!");
request.getRequestDispatcher("/sys/function/edit.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void slist(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Paper paper = new Paper();
String pname = new String(request.getParameter("pname").getBytes("utf-8"), "utf-8");
paper.setPname(pname);
List<Subject> subjects = service.subjectlist(paper);
request.setAttribute("subjects", subjects);
request.getRequestDispatcher("/sys/paper/subjects.jsp").forward(request, response);
}
}
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse rsp = (HttpServletResponse)response;
HttpSession session = req.getSession();
if(session==null || session.getAttribute("user")==null){
user.setUserpwd(request.getParameter("userpwd"));
user = service.stulogin(user);
if(user==null){
request.setAttribute("msg", "用户名密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}else{
HttpSession session = request.getSession(true);
session.setAttribute("user", user);
session.setAttribute("userid", user.getUserid());
index(request, response);
// request.getRequestDispatcher("/sys/user?cmd=init").forward(request, response);
}
}
}
public class RoleServlet extends HttpServlet {
IRoleService service = new RoleService();
e.printStackTrace();
}
}
/**
* 获取用户列表
* @param request
* @param response
* @throws IOException
* @throws ServletException
* @throws IOException
* @throws ServletException
*/
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sname = request.getParameter("sname");
Sysuser user = new Sysuser();
if(sname!=null && !sname.equals("")){
user.setUsername(sname);
}
PageControl pc = new PageControl();
Integer currindex = 1;
if(request.getParameter("index")!=null){
currindex = Integer.parseInt(request.getParameter("index"));
}
pc.setCurrentindex(currindex);
//pc.setPagesize(5);
Pager<Sysuser> pager = service.list(user, pc);
request.setAttribute("pager", pager);
request.getRequestDispatcher("/sys/user/list.jsp").forward(request, response);
}
/**
* 注销
* @param request
* @param response
* @throws IOException
*/
private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
session.removeAttribute("user");
response.sendRedirect(Tools.Basepath(request, response)+"login.jsp");
}
* @param response
*/
private void initpage(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
Sysuser user = (Sysuser)session.getAttribute("user");
List<SysFunction> list = service.initpage(user);
try {
request.setAttribute("list", list);
RequestDispatcher rds=request.getRequestDispatcher("/index.jsp");
rds.forward(request, response);
return;
//request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用户登陆
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Sysuser user = new Sysuser();
user.setUsername(request.getParameter("username"));
user.setUserpwd(request.getParameter("userpwd"));
user = service.login(user);
if(user==null){
request.setAttribute("msg", "用户名密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}else{
HttpSession session = request.getSession(true);
session.setAttribute("user", user);
initpage(request, response);
//request.getRequestDispatcher("/sys/user?cmd=init").forward(request, response);
}
}
/**
* 学生获取试题内容
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void paper(HttpServletRequest request,
List<Subject> subjects = paperService.subjectlist(paper);
request.setAttribute("subjects", subjects);
request.setAttribute("pname", pname);
request.getRequestDispatcher("/user/paper/paper.jsp").forward(request, response);
}
/**
* 提交回答问题
* @param request
* @param response
* @throws UnsupportedEncodingException
*/
private void answer(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
//JSONArray json = JSONArray.fromObject(personstr);
// TODO Auto-generated method stub
// System.out.println(request.getParameterMap());
String pname = new String(request.getParameter("pname").getBytes("utf-8"), "utf-8");
Studentpaper studentpaper = new Studentpaper();
try {
BeanUtils.populate(studentpaper, request.getParameterMap());
studentpaper.setPname(pname);
Integer rtn = spServece.addPaper(studentpaper);
if(rtn>0){
//response.sendRedirect(Tools.Basepath(request, response)+"sys/user?cmd=list");
}else{
// request.setAttribute("msg", "保存用户失败");
// request.getRequestDispatcher("/sys/user/add.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 学生登陆
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void stulogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Sysuser user = new Sysuser();
user.setUsername(request.getParameter("username"));
user.setUserpwd(request.getParameter("userpwd"));
user = service.stulogin(user);
if(user==null){
request.setAttribute("msg", "用户名密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
String cmd= request.getParameter("cmd");
if(cmd.equals("paperlist")){
index(request,response);
}else if(cmd.equals("login")){
login(request,response);
}else if(cmd.equals("init")){
initpage(request,response);
}else if(cmd.equals("logout")){
logout(request,response);
}else if(cmd.equals("list")){
list(request,response);
}else if(cmd.equals("add")){
add(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}else if(cmd.equals("toeditpwd")){
toeditpwd(request,response);
}else if(cmd.equals("editpwd")){
editpwd(request,response);
}else if(cmd.equals("paper")){
paper(request,response);
}else if(cmd.equals("stulogin")){
stulogin(request,response);
}else if(cmd.equals("answer")){
answer(request,response);
}
}
/**
* 跳转首页
* @param request
* @param response
*/
private void index(HttpServletRequest request, HttpServletResponse response) {
Paper paper = new Paper();
//String pname = request.getParameter("pname");
//paper.setPname(pname);
List<Paper> papers = paperService.list(paper);
request.setAttribute("papers", papers);
try {
request.getRequestDispatcher("/user/index.jsp").forward(request, response);
* 注销
* @param request
* @param response
* @throws IOException
*/
private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
session.removeAttribute("user");
response.sendRedirect(Tools.Basepath(request, response)+"login.jsp");
}
/**
* 初始化主页
* @param request
* @param response
*/
private void initpage(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
Sysuser user = (Sysuser)session.getAttribute("user");
List<SysFunction> list = service.initpage(user);
try {
request.setAttribute("list", list);
RequestDispatcher rds=request.getRequestDispatcher("/index.jsp");
rds.forward(request, response);
return;
//request.getRequestDispatcher("/index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用户登陆
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("list",list);
role = service.detail(role);
request.setAttribute("role", role);
request.getRequestDispatcher("/sys/role/right.jsp").forward(request, response);
}
/**
* 新增角色
* @param request
* @param response
*/
private void add(HttpServletRequest request, HttpServletResponse response) {
Sysrole role = new Sysrole();
try {
BeanUtils.populate(role, request.getParameterMap());
Integer rtn = service.add(role);
if(rtn>0){
response.sendRedirect(Tools.Basepath(request, response)+"sys/role?cmd=list");
}else{
request.setAttribute("msg", "保存角色失败");
request.getRequestDispatcher("/sys/role/add.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取角色列表
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sname = request.getParameter("sname");
Sysrole role = new Sysrole();
if(sname!=null && !sname.equals("")){
role.setRolename(sname);
}else if(cmd.equals("logout")){
logout(request,response);
}else if(cmd.equals("list")){
list(request,response);
}else if(cmd.equals("add")){
add(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}else if(cmd.equals("toeditpwd")){
toeditpwd(request,response);
}else if(cmd.equals("editpwd")){
editpwd(request,response);
}else if(cmd.equals("paper")){
paper(request,response);
}else if(cmd.equals("stulogin")){
stulogin(request,response);
}else if(cmd.equals("answer")){
answer(request,response);
}
}
/**
* 跳转首页
* @param request
* @param response
*/
private void index(HttpServletRequest request, HttpServletResponse response) {
Paper paper = new Paper();
//String pname = request.getParameter("pname");
//paper.setPname(pname);
List<Paper> papers = paperService.list(paper);
request.setAttribute("papers", papers);
try {
request.getRequestDispatcher("/user/index.jsp").forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 修改用户功能
* @param request
* @param response
*/
private void edit(HttpServletRequest request, HttpServletResponse response) {
}
}
public class SubjectServlet extends HttpServlet {
ISubjectService service = new SubjectService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cmd = request.getParameter("cmd");
if(cmd.equals("add")){
addsubject(request,response);
}else if(cmd.equals("list")){
list(request,response);
}else if(cmd.equals("toedit")){
toedit(request,response);
}else if(cmd.equals("edit")){
edit(request,response);
}
}
/**
* 修改试题功能
* @param request
* @param response
*/
private void edit(HttpServletRequest request, HttpServletResponse response) {