使用jsp+ servlet mvc三层架构模式编写服务端逻辑,jdbc直连mysql操作数据库,完成整体业务逻辑开发;系统定义了管理端(管理员)和用户端(普通用户和游客)管理端主要实现对问卷的增删改查操作,以及用户信息管理用户端可以查看问卷并参与问卷调查以及整个问卷调查后的占比情况
开发环境准备:
- jdk1.8
- tomcat8
- mysql5.7
- intellij IDEA
系统采用技术:
- jsp+ servlet mvc三层架构模式
- jdbc
- jQuery+ajax
- ECharts 4.3.0
项目介绍
用户端
用户端可以查看问卷列表并参与问卷调查
查看个人参与的调查报告以及整个问卷情况
管理端
问卷的新增和维护
用户信息维护
项目设计
运行效果
- 登录
- 添加问卷
- 问卷投票列表
- 投票详情
- 数据库表
- 代码结构截图
关键代码:
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 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
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 = 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 list=recordService.getVotes(recordQueryModel);
if(list.size()>0){ //当前用户已经投过票
printWriter.println(2);
return;
}
}
List records = new ArrayList<>();
for(int i=0;i
根据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 list=recordService.geyVotes(subjectId);
if(list.size()>0){
session.setAttribute("list", list);
session.setAttribute("stitle", list.get(0).getSubject().getTitle());
List opDatas=new ArrayList<>();
Map 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流程图
- 问卷操作时序图
- 用户注册登录时序图