jsp+servlet搭建在線投票問卷系統

點贊再看,養成習慣
程序幫QQ:1022287044mysql

開發環境準備:

  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

項目介紹

用戶端
ajax

  1. 用戶端能夠查看問卷列表並參與問卷調查
  2. 查看我的參與的調查報告以及整個問卷狀況

管理端
sql

  1. 問卷的新增和維護
  2. 用戶信息維護

項目設計

image
image

運行效果

  • 登陸

登陸

  • 添加問卷

添加問卷

  • 問卷投票列表

問卷投票列表

  • 投票詳情

投票詳情

  • 數據庫表

image
image

  • 代碼結構截圖

代碼結構截圖

關鍵代碼:

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.修改問卷信息tomcat

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.問卷投票session

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.投票可視化代碼mvc

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流程圖

mvc流程圖

  • 問卷操做時序圖

問卷操做時序圖

  • 用戶註冊登陸時序圖

用戶註冊登陸時序圖

相關文章
相關標籤/搜索