关于jsp:jspservlet搭建在线投票问卷系统

48次阅读

共计 6690 个字符,预计需要花费 17 分钟才能阅读完成。

点赞再看,养成习惯
程序帮 QQ:1022287044

开发环境筹备:

  1. jdk1.8
  2. tomcat8
  3. mysql5.7
  4. intellij IDEA

零碎采纳技术:

  1. jsp+ servlet mvc 三层架构模式
  2. jdbc
  3. jQuery+ajax
  4. ECharts 4.3.0

我的项目介绍

用户端

  1. 用户端能够查看问卷列表并参加问卷调查
  2. 查看集体参加的调查报告以及整个问卷状况

治理端

  1. 问卷的新增和保护
  2. 用户信息保护

我的项目设计


运行成果

  • 登录

  • 增加问卷

  • 问卷投票列表

  • 投票详情

  • 数据库表


  • 代码构造截图

要害代码:

1. 增加问卷:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException {String questionTitle = request.getParameter("questionTitle"); // 问卷题目
 String qId = request.getParameter("qId"); // 问卷 id
 Integer ops =Integer.valueOf(request.getParameter("ops"));// 具体几组
 String doType=request.getParameter("doType"); // 操作类型
 User user=(User)request.getSession().getAttribute(User.SESSIONNAME); SubjectService subjectService=new SubjectServiceImpl(); List<Subject> subjects=new ArrayList<>(); // 问题列表
 Question question=new Question(); question.setTitle(questionTitle); if("add".equalsIgnoreCase(doType)){ // 如果批改增加 id
 if(null!=user){// 用户对象不为空,保留值
 question.setUserId(user.getId()); } }else{question.setId(Integer.valueOf(qId)); } String title=""; for(int i=0;i<ops;i++){int number = Integer.parseInt(request.getParameter("number"+i)); String[] os = request.getParameterValues("options"+i); String[] osIds = request.getParameterValues("optionsIds"+i);// 选项 id
 title=request.getParameter("title"+i);// 问题题目
 String titleId=request.getParameter("titleId"+i);// 问题 id
 Subject subject = new Subject(); subject.setOrderId(i); subject.setTitle(title); subject.setNumber(number); if(null!=titleId&&!"".equalsIgnoreCase(titleId)){// 设置问题 id
 subject.setId(Integer.valueOf(titleId)); } for(int i1=0;i1<os.length;i1++){Option op = new Option(); op.setContent(os[i1]); if("modify".equalsIgnoreCase(doType)){ // 如果批改增加 id
 op.setId(Integer.valueOf(osIds[i1])); } subject.getOptions().add(op); } try {long startTime=new Date().getTime(); subject.setStartTime(startTime); subject.setEndTime(startTime+1*24*60*60*1000); subject.setMaster(user); subjects.add(subject); } catch (ReTryException e) {HttpSession session = request.getSession(); session.setAttribute("subject", subject); session.setAttribute("message", e.getMessage()); response.sendRedirect(request.getContextPath() + "/m/add"); } catch (Exception ex) {throw new RuntimeException(ex); } } try{if("add".equalsIgnoreCase(doType)){// 增加
 subjectService.add(question,subjects); }else{// 批改
 subjectService.modify(question,subjects); } }catch (Exception e){e.printStackTrace(); }
 if(null!=user&&user.getRole().equalsIgnoreCase("user")){response.sendRedirect(request.getContextPath() + "/list?action=myRelease");// 用户查看本人的公布
 }else{response.sendRedirect(request.getContextPath() + "/list?action=index"); }
 }

2. 批改问卷信息

public void doGet(HttpServletRequest request, HttpServletResponse response) {int id = Integer.parseInt(request.getParameter("id")); try {SubjectService subjectService = new SubjectServiceImpl(); QuestionService questionService = new QuestionServiceImpl(); SubjectQueryModel subjectQueryModel=new SubjectQueryModel(); subjectQueryModel.setQuestionId(id); List<Subject> subject = subjectService.getSubjects(subjectQueryModel);// 问题列表
 subjectQueryModel.setQuestionId(id); Question question=questionService.getQuestion(id); request.setAttribute("subjects", subject); request.setAttribute("ops", subject.size());// 多少组
 request.setAttribute("question", question); request.getRequestDispatcher("/WEB-INF/pages/modify.jsp") .forward(request, response); } catch (Exception e) {throw new RuntimeException(e);
 } }

3. 问卷投票

 public void doGet(HttpServletRequest request, HttpServletResponse response)  {try{ PrintWriter printWriter = response.getWriter(); int subjectId=Integer.parseInt(request.getParameter("subjectId")); String id = request.getParameter("options"); String[] ids = id.split(","); if(null==ids){ // 未选中任何一项答案
 printWriter.println(0); return; }else{User user= (User)request.getSession().getAttribute(User.SESSIONNAME); // 如果登录用户,判断是否曾经投过票
 if(null!=user){RecordQueryModel recordQueryModel=new RecordQueryModel(); recordQueryModel.getUser().setId(user.getId()); recordQueryModel.getSubject().setId(subjectId); List<Record> list=recordService.getVotes(recordQueryModel); if(list.size()>0){       // 以后用户曾经投过票
 printWriter.println(2); return; } } List<Record> records = new ArrayList<>(); for(int i=0;i<ids.length;i++){Record record = new Record(); record.getSubject().setId(subjectId); record.getOption().setId(Integer.parseInt(ids[i])); if(null!=user){record.getUser().setId(user.getId()); } records.add(record); } try {recordService.vote(records); // 保留问卷信息
 printWriter.println(1);// 参加胜利
 return; } catch (ReTryException e) {e.printStackTrace(); printWriter.println(3);// 参加胜利
 return; }catch(Exception ex){ex.printStackTrace(); throw new RuntimeException(ex); } } }  catch (Exception e){e.printStackTrace(); } }

依据 jdbc 直连技术,编写数据库操作工具类,不便存储数据,代码如下:

public class DBUtils {
 String url = null;    // 连贯地址
 String username = null;       // 数据库名
 String password = null;       // 数据库明码
 String driverClass = null; // 连贯驱动
 private static DBUtils db = new DBUtils(); /** 构建数据库连贯参数 */
 private DBUtils() { try { url = "jdbc:mysql://localhost:3306/shopCartDb?useUnicode=true&characterEncoding=utf8"; username = "root"; password = "root123"; driverClass = "com.mysql.jdbc.Driver"; Class.forName(driverClass); } catch (Exception e) {e.printStackTrace(); } } /** 构建数据库连贯对象 */
 public Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (Exception e) {e.printStackTrace(); } return conn; } public static DBUtils getInstance(){ return db;}}

4. 投票可视化代码

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {HttpSession session = request.getSession(); try {int subjectId=Integer.parseInt(request.getParameter("subjectId")); // 题目
 RecordService recordService=new RecordServiceImpl(); List<Record> list=recordService.geyVotes(subjectId); if(list.size()>0){session.setAttribute("list", list); session.setAttribute("stitle", list.get(0).getSubject().getTitle()); List<String> opDatas=new ArrayList<>(); Map<String,Integer> map=new HashMap<>(); for(Record r:list){if(null!=r.getOption()){if(!opDatas.contains(r.getOption().getContent())){opDatas.add(r.getOption().getContent()); map.put(r.getOption().getContent(),0); } map.put(r.getOption().getContent(),map.get(r.getOption().getContent())+1); }
 } String opData=""; for(String o:opDatas){opData=opData+"'"+o+"',";} if(opData.length()>0){opData=opData.substring(0,opData.length()-1); } session.setAttribute("opData", opData);   // 选项
 String datavn=""; for (String key : map.keySet()) {datavn=datavn+"{ value: "+map.get(key)+", name: '"+key+"' },"; } if(datavn.length()>0){datavn=datavn.substring(0,datavn.length()-1); } session.setAttribute("datavn", datavn); request.getRequestDispatcher("/WEB-INF/pages/view.jsp").forward(request, response); return; }else{request.setAttribute("exception"," 暂无问卷后果!");
 request.getRequestDispatcher("/WEB-INF/pages/500.jsp").forward(request, response); return; } } catch (ReTryException e) {request.getSession().setAttribute("message", e.getMessage()); response.sendRedirect(request.getContextPath()+"/m/view"); }catch(Exception ex){ex.printStackTrace(); } }

外围代码截图

  • 我的公布后台程序

  • 管理员保护界面后盾代码

  • 管理员创立界面后盾代码

时序图

  • mvc 流程图

  • 问卷操作时序图

  • 用户注册登录时序图

正文完
 0