jsp+servlet搭建在线投票问卷系统

点赞再看,养成习惯
程序帮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"); //问卷idInteger 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)){ //如果修改添加idif(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);//选项idtitle=request.getParameter("title"+i);//问题标题String titleId=request.getParameter("titleId"+i);//问题idSubject subject = new Subject();subject.setOrderId(i);subject.setTitle(title);subject.setNumber(number);if(null!=titleId&&!"".equalsIgnoreCase(titleId)){//设置问题idsubject.setId(Integer.valueOf(titleId));}for(int i1=0;i1<os.length;i1++){Option op = new Option();op.setContent(os[i1]);if("modify".equalsIgnoreCase(doType)){ //如果修改添加idop.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");}}
  1. 修改问卷信息

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);}}
  1. 问卷投票

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;}}
  1. 投票可视化代码

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)

相关推荐