java jsp實現網路考試系統(mysql)
阿新 • • 發佈:2020-08-13
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();
}
}
}
寫在最後
交流:3459067873