jsp+servlet搭建線上投票問卷系統
阿新 • • 發佈:2020-12-22
jsp+servlet搭建線上投票問卷系統
點贊再看,養成習慣
程式幫QQ:1022287044
開發環境準備:
- jdk1.8
- tomcat8
- mysql5.7
- intellij IDEA
系統採用技術:
- jsp+ servlet mvc三層架構模式
- jdbc
- jQuery+ajax
- ECharts 4.3.0
專案介紹
使用者端
-
使用者端可以檢視問卷列表並參與問卷調查
-
檢視個人參與的調查報告以及整個問卷情況
管理端
-
問卷的新增和維護
-
使用者資訊維護
專案設計
執行效果
-
註冊
-
登入
-
新增問卷
-
問卷投票列表
-
投票詳情
-
使用者列表
-
資料庫表
-
程式碼結構截圖,
關鍵程式碼:
- 新增問卷:
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"); } }
- 修改問卷資訊
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); } }
- 問卷投票
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;
}
}
- 投票視覺化程式碼
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流程圖
- 問卷操作時序圖
- 使用者註冊登入時序圖