专栏首页java系统java jsp实现网络考试系统(mysql)
原创

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

java网络考试系统

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

@TOC

实现效果

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

主要代码实现

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

	
	
}

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Java基于SSM的个人博客系统(源码 包含前后台)

    用户6334815
  • python智能图片识别系统(图片切割、图片识别、区别标识)

    你好! python flask图片识别系统使用到的技术有:图片背景切割、图片格式转换(pdf转png)、图片模板匹配、图片区别标识。

    用户6334815
  • Flask之请求和响应

    人生不如戏
  • Servlet第四篇【request对象常用方法、应用】

    什么是HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所...

    Java3y
  • Jtro的技术分享:UNITY请求JAVA服务器接口

    现在有许多的服务器是用java写的,所以就需要我们请求java服务器的接口,java提供的服务器长这样:

    LittleU
  • flask中请求勾子

    在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如:     *在请求开始时,建立数据库连接;     *在请求开始时,根据需求进行权限校验;...

    py3study
  • Ajax设置请求和接收响应、自己封装简易jQuery.Ajax、回调函数

    这篇文章是承接前几篇博客的,是前几篇继续学习 包括Ajax学习与理解和简化版自己实现jQuery等 这篇文章只算是我的个人学习笔记,内容没有精心排版,一些错误请...

    代码之风
  • [048]Block Layer层架构演变(个人YY)

    Block Layer层在整个I/O中负责承上启下,上接文件系统,下接块驱动。 我不想直接讨论代码,希望从一个架构的演变来初探一下Block Layer层。

    王小二
  • Java网络03 Servlet沙拉

    Servlet开胃酒中介绍了Servlet的基本流程和设置方式。这里,我们将看一些Servlet实例。这些实例都是HTTP通信(参考HTTP协议)中的常见问题。...

    Vamei
  • python爬虫从入门到放弃(三)之 Urllib库的基本使用

     官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的...

    coders

扫码关注云+社区

领取腾讯云代金券