java jsp实现网络考试系统(mysql)

java网络考试系统

功能:可进行学生、管理员登录,学生考试、管理员出卷、列表分页
@

实现效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

主要代码实现

package cn.itbaizhan.tyut.exam.sys.servlets;


import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.itbaizhan.tyut.exam.common.PageControl;
import cn.itbaizhan.tyut.exam.common.Pager;
import cn.itbaizhan.tyut.exam.common.Tools;
import cn.itbaizhan.tyut.exam.model.SysFunction;
import cn.itbaizhan.tyut.exam.sys.services.impl.FunService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.IFunService;

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();
		}
	}

	/**
	 * 初始化修改页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void toedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		SysFunction fun = new SysFunction();
		fun.setFunid(Integer.parseInt(request.getParameter("id")));
		fun = service.detail(fun);
		if(fun!=null){
			request.setAttribute("item",fun);
			request.getRequestDispatcher("/sys/function/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 funlist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String sname = request.getParameter("sname");
		SysFunction fun = new SysFunction();
		if(sname!=null && !sname.equals("")){
			fun.setFunname(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<SysFunction> pager = service.list(fun, pc);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/sys/function/list.jsp").forward(request, response);
	}

	/**
	 * 增加系统功能
	 * @param request
	 * @param response
	 */
	private void addfun(HttpServletRequest request, HttpServletResponse response) {
		
		SysFunction fun = new SysFunction();
		try {
			BeanUtils.populate(fun,request.getParameterMap());
			Integer rtn = service.addfun(fun);
			if(rtn>0){			
				response.sendRedirect(Tools.Basepath(request, response)+"sys/fun?cmd=list");
			}else{
				request.setAttribute("msg", "保存系统功能失败!");
				request.getRequestDispatcher("/sys/function/add.jsp").forward(request, response);
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}	
	}

	
	
}

package cn.itbaizhan.tyut.exam.sys.servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.itbaizhan.tyut.exam.common.PageControl;
import cn.itbaizhan.tyut.exam.common.Pager;
import cn.itbaizhan.tyut.exam.common.Tools;
import cn.itbaizhan.tyut.exam.model.Paper;
import cn.itbaizhan.tyut.exam.model.Subject;
import cn.itbaizhan.tyut.exam.sys.services.impl.PaperService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.IPaperService;

public class PaperServlet extends HttpServlet {
	IPaperService service = new PaperService();
	
	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("slist")){
			slist(request,response);
		}
	}	
	
	/**
	 * 生成试题功能
	 * @param request
	 * @param response
	 */
	private void addsubject(HttpServletRequest request, HttpServletResponse response) {
		
		Paper paper = new Paper();
		try {
			BeanUtils.populate(paper,request.getParameterMap());
			Integer rtn = service.addpaper(paper) ;
			if(rtn>0){			
				response.sendRedirect(Tools.Basepath(request, response)+"sys/paper?cmd=list");
			}else{
				request.setAttribute("msg", "增加试题功能失败!");
				request.getRequestDispatcher("/sys/paper/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 pname = request.getParameter("pname");
		Paper paper = new Paper();
		if(pname!=null && !pname.equals("")){
			paper.setPname(pname);
		}
		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<Paper> pager = service.list(paper, pc);
//		System.out.println(pager.getList());;
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/sys/paper/list.jsp").forward(request, response);
	}
	private void slist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Paper paper = new Paper();
		String pname = new String(request.getParameter("pname").getBytes("iso-8859-1"), "utf-8");
		paper.setPname(pname);
		List<Subject> subjects = service.subjectlist(paper);
		request.setAttribute("subjects", subjects);
		request.getRequestDispatcher("/sys/paper/subjects.jsp").forward(request, response);
	}
}

package cn.itbaizhan.tyut.exam.sys.servlets;


import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.jspsmart.upload.SmartUpload;

import cn.itbaizhan.tyut.exam.common.DBUnitHelper;
import cn.itbaizhan.tyut.exam.common.PageControl;
import cn.itbaizhan.tyut.exam.common.Pager;
import cn.itbaizhan.tyut.exam.common.Tools;
import cn.itbaizhan.tyut.exam.model.Paper;
import cn.itbaizhan.tyut.exam.model.Studentpaper;
import cn.itbaizhan.tyut.exam.model.Subject;

import cn.itbaizhan.tyut.exam.sys.services.impl.StudentpaperService;
import cn.itbaizhan.tyut.exam.sys.services.impl.SubjectService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.IStudentpaperService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.ISubjectService;


public class StudentpaperServlet extends HttpServlet {

	IStudentpaperService service = new StudentpaperService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String cmd = request.getParameter("cmd");
//		if(cmd.equals("add")){
//			add(request,response);
//		}
//		else if(cmd.equals("list")){
//			list(request,response);
//		}
//		else if(cmd.equals("listByRightcount")){
//			listByRightcount(request,response);
//		}
		if(cmd.equals("list")){
			list(request,response);
		}else if(cmd.equals("score")){
			score(request,response);
		}else if(cmd.equals("stupaper")){
			StudentPaperList(request,response);
		}
	}
	
	/**
	 * 查询试卷得分
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void score(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Studentpaper stupaper = new Studentpaper();
		try {
			BeanUtils.populate(stupaper,request.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(stupaper.getUserid()+stupaper.getSpid());
		List<Studentpaper> studentpaper = service.listByRightcount(stupaper);
		Integer score = studentpaper.get(0).getRightcount(); 
		request.setAttribute("score", score);
		PrintWriter out = response.getWriter();  
		  
        out.println("您本次得分" + score * 2 + "分!");  
        out.flush();  
        out.close(); 
		//request.getRequestDispatcher("/sys/paper/subjects.jsp").forward(request, response);
	}
	
	/**
	 * 查询详细错题
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Studentpaper studentpaper = new Studentpaper();
		PageControl pc = new PageControl();
		Integer currindex = 1;
		if(request.getParameter("index")!=null){
			currindex = Integer.parseInt(request.getParameter("index"));
		}
		pc.setCurrentindex(currindex);
		//pc.setPagesize(5);
		studentpaper.setUserid((Integer) request.getSession().getAttribute("userid"));
		studentpaper.setSpid(request.getParameter("spid"));
		Pager<Subject> pager = service.list(studentpaper, pc);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/user/paper/studenterror.jsp").forward(request, response);
	}

	/**
	 * 增加试题功能
	 * @param request
	 * @param response
	 *//*
	private void addsubject(HttpServletRequest request, HttpServletResponse response) {
		
		Subject subject = new Subject();
		try {
			BeanUtils.populate(subject,request.getParameterMap());
			Integer rtn = service.addsubject(subject);
			if(rtn>0){			
				response.sendRedirect(Tools.Basepath(request, response)+"sys/subject?cmd=list");
			}else{
				request.setAttribute("msg", "增加试题功能失败或请不要添加相同试题!");
				request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}	
	}
	*/
	private void StudentPaperList(HttpServletRequest request, HttpServletResponse response) {
		
		Studentpaper studentpaper = new Studentpaper();
		Integer userid = (Integer) request.getSession().getAttribute("userid");
		studentpaper.setUserid(userid);
		List<Studentpaper> papers = service.StudentPaperList(studentpaper);
		request.setAttribute("papers", papers);
		try {
			request.getRequestDispatcher("/user/paper/studentpaper.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package cn.itbaizhan.tyut.exam.sys.servlets;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.jspsmart.upload.SmartUpload;

import cn.itbaizhan.tyut.exam.common.DBUnitHelper;
import cn.itbaizhan.tyut.exam.common.PageControl;
import cn.itbaizhan.tyut.exam.common.Pager;
import cn.itbaizhan.tyut.exam.common.Tools;
import cn.itbaizhan.tyut.exam.model.Subject;

import cn.itbaizhan.tyut.exam.sys.services.impl.SubjectService;
import cn.itbaizhan.tyut.exam.sys.services.interfaces.ISubjectService;


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) {
		
		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);
		}
	}

	/**
	 * 查询试题列表
	 * @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"));
		}
		pc.setCurrentindex(currindex);
		//pc.setPagesize(5);
		
		Pager<Subject> pager = service.list(subject, pc);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/sys/subject/list.jsp").forward(request, response);
	}

	/**
	 * 增加试题功能
	 * @param request
	 * @param response
	 */
	private void addsubject(HttpServletRequest request, HttpServletResponse response) {
		
		Subject subject = new Subject();
		try {
			BeanUtils.populate(subject,request.getParameterMap());
			Integer rtn = service.addsubject(subject);
			if(rtn>0){			
				response.sendRedirect(Tools.Basepath(request, response)+"sys/subject?cmd=list");
			}else{
				request.setAttribute("msg", "增加试题功能失败或请不要添加相同试题!");
				request.getRequestDispatcher("/sys/subject/add.jsp").forward(request, response);
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}	
	}

	
	
}

写在最后

有问题可加博主V交流:Code2Life2

posted @ 2020-08-13 21:39  武红明  阅读(1305)  评论(0编辑  收藏  举报