1、 学校代码: 10128 学 号: 201320905047 JSP开发实训报告书题 目:在线考试系统的制作学生姓名:张志勇学 院:理学院班 级:信计13-1指导教师:李晓瑜、宋健、赖俊峰 二一六年一月一、项目名称在线考试系统-题库子系统二、功能要求 a、用户管理 b、课程管理 c、题库管理 三、需求分析 在线考试系统-题库子系统的用户包括用户管理员,试题管理员和题库使用人员,旨在建立一个独立的题库系统,为在线考试生成试卷提供支持,包含用户管理,科目管理,试题管理,生成试卷,试卷分析等内容,为用户提供了一个快速、全面、准确的试题管理平台。四、设计思想 a、使用Java Web技术实现 b、使用
2、Mysql存储数据 c、基于MVC方式实现用例设计思路:(如下图)分步详解:第一步:创建项目名(zhangzhiyong);第二步:创建项目所需要的包;第三步:导入需要的工具;第四步:创建数据库的连接;第五步:需要的准备工作做好之后,明确先做用户管理系统(登录,查看,删除,修改,增加,退出;);第六步:做好用户管理之后进行科目管理(增加,删除,修改,查看;);第七步:接下来进行知识点管理(增加,删除,修改,查看;);五、具体实现一用户管理:需要的servlet:(user servlet)package cn.zhangzhiyong.service;import java.io.IOExce
3、ption;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.zhangzhiyong.bean.User;im
4、port cn.zhangzhiyong.dao.UserDAO;import cn.zhangzhiyong.util.RequestUtil;WebServlet(/UserServlet)public class UserServlet extends HttpServlet private static final long serialVersionUID = 1L; public UserServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response)
5、throws ServletException, IOException request.setCharacterEncoding(utf-8);response.setContentType(text/html;charset=utf-8);PrintWriter out=response.getWriter();String type=request.getParameter(type);String userLogname=request.getParameter(userLogname);String userPwd=request.getParameter(userPwd);User
6、DAO dao=new UserDAO();User user=dao.login(userLogname, userPwd);if(login.equals(type)if(user!=null)request.getSession().setAttribute(SESSION_USER,user);response.sendRedirect(res/index.html);elseout.print();out.print(alert(用户名或密码错误,请重新输入!););out.print(window.location=login.jsp;);out.print();else if(l
7、ogout.equals(type)request.getSession().invalidate();out.print();out.print(window.location=login.jsp;);out.print();else if(list.equals(type)List list=dao.selectAll();request.setAttribute(list,list);request.getRequestDispatcher(res/user.jsp).forward(request, response);protected void doPost(HttpServlet
8、Request request, HttpServletResponse response) throws ServletException, IOException doGet(request, response);(user add servlet)package cn.zhangzhiyong.service;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import
9、 javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.dao.UserDAO;import cn.zhangzhiyong.util.RequestUtil;WebServlet(/UserAddServlet)public class UserAddServlet extends HttpSe
10、rvlet private static final long serialVersionUID = 1L; public UserAddServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException request.setCharacterEncoding(utf-8);String login=request.getParameter(login);String name=request
11、.getParameter(name);int type=(request.getParameter(type)=null)?3:Integer.parseInt(request.getParameter(type);int status=(request.getParameter(status)=null)?1:Integer.parseInt(request.getParameter(status);User user=new User(login,name,type,status);UserDAO dao=new UserDAO();int n=dao.save(user);if(n=1
12、)response.sendRedirect(UserServlet?type=list);elseresponse.sendRedirect(index.jsp);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request, response);(UserDeleteServlet)package cn.zhangzhiyong.service;import java.io.IOExcepti
13、on;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.dao.UserDAO;import cn.zhangzhiy
14、ong.util.RequestUtil;import cn.zhangzhiyong.util.WebUtil;WebServlet(/UserDeleteServlet)public class UserDeleteServlet extends HttpServlet private static final long serialVersionUID = 1L; public UserDeleteServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response
15、) throws ServletException, IOException request.setCharacterEncoding(UTF-8);/ System.out.println(request.getParameter(Id); int id=RequestUtil.getInt(request,Id); UserDAO dao=new UserDAO(); dao.delete(id); WebUtil.forward(request, response, UserServlet?type=list); protected void doPost(HttpServletRequ
16、est request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoGet(request, response);(UserUpdate1Servlet)package cn.zhangzhiyong.service;import java.io.File;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.anno
17、tation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.dao.UserDAO;import cn.zhangzhiyong.util.RequestUtil;import cn.zhan
18、gzhiyong.util.WebUtil;WebServlet(/UserUpdate1Servlet)public class UserUpdate1Servlet extends HttpServlet private static final long serialVersionUID = 1L; public UserUpdate1Servlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExc
19、eption request.setCharacterEncoding(utf-8);response.setContentType(text/html;charset=utf-8);int id=RequestUtil.getInt(request,Id); request.setAttribute(id, id); request.getRequestDispatcher(res/user-update.jsp).forward(request, response); protected void doPost(HttpServletRequest request, HttpServlet
20、Response response) throws ServletException, IOException / TODO Auto-generated method stubdoGet(request, response);(UserUpdateServlet)package cn.zhangzhiyong.service;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.H
21、ttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.dao.UserDAO;import cn.zhangzhiyong.util.RequestUtil;WebServlet(/UserUpdateServlet)public class UserUpdateServlet extends HttpServlet private s
22、tatic final long serialVersionUID = 1L; public UserUpdateServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException request.setCharacterEncoding(utf-8);String login=request.getParameter(login);String name=request.getParamete
23、r(name);String passwd=request.getParameter(passwd);int type=Integer.parseInt(request.getParameter(type);int status=Integer.parseInt(request.getParameter(status);int id=RequestUtil.getInt(request,id);User users=new User(login,name,type,status);UserDAO dao=new UserDAO();int n=dao.update(users);if(n=1)
24、response.sendRedirect(UserServlet?type=list);elseresponse.sendRedirect(index.jsp);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request, response);(UserViewServlet)package cn.zhangzhiyong.service;import java.io.IOException;
25、import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.dao.Use
26、rDAO;import cn.zhangzhiyong.util.RequestUtil;WebServlet(/UserViewServlet)public class UserViewServlet extends HttpServlet private static final long serialVersionUID = 1L; public UserViewServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletEx
27、ception, IOException request.setCharacterEncoding(utf-8);response.setContentType(text/html;charset=utf-8);PrintWriter out=response.getWriter();String type=request.getParameter(type);String userLogname=request.getParameter(userLogname);String userPwd=request.getParameter(userPwd);UserDAO dao=new User
28、DAO();int id=RequestUtil.getInt(request,Id);User user1=dao.selectById(id);request.setAttribute(user,user1);request.getRequestDispatcher(res/user-show.jsp).forward(request, response);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException d
29、oGet(request, response);需要的类:package cn.zhangzhiyong.bean;import java.sql.Timestamp;public class User public static final String passwd=123456;private int id;private String login;private String name;private String password;private int type;private int status;private Timestamp last_login; public User
30、(String login2, String name2, int type2, int status2) super();this.login=login2;this.name=name2;this.type=type2;this.status=status2;public User() public User(int id2) this.id=id2;public int getId() return id;public void setId(int id) this.id = id;public String getLogin() return login;public void set
31、Login(String login) this.login = login;public String getName() return name;public void setName(String name) this.name = name;public String getPassword() return password;public void setPassword(String password) this.password = password;public int getType() return type;public void setType(int type) th
32、is.type = type;public int getStatus() return status;public void setStatus(int status) this.status = status;public Timestamp getLast_login() return last_login;public void setLast_login(Timestamp last_login) this.last_login = last_login;需要的dao:package cn.zhangzhiyong.dao;import java.sql.Connection;imp
33、ort java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import cn.zhangzhiyong.bean.User;import cn.zhangzhiyong.util.DBUtil;public class UserDAO /userdao中的save方法public int save(User user)int n=-1;Connection conn=DBUtil.ge
34、tConnection();/连接对象PreparedStatement pstmt=null;/语句对象/下边是s语句String sql=insert into userss+ values(DL_USERSS.NEXTVAL,?,?,123456,?,?,sysdate);trypstmt=conn.prepareStatement(sql);pstmt.setString(1, user.getLogin();pstmt.setString(2, user.getName();pstmt.setInt(3, user.getType();pstmt.setInt(4, user.get
35、Status();n=pstmt.executeUpdate();/n=更新的行数catch(SQLException e)e.printStackTrace();finallyDBUtil.closeJDBC(null, pstmt, conn);return n;public User login(String userLogname, String userPwd) Connection conn=DBUtil.getConnection();PreparedStatement pstmt=null;ResultSet rs=null;User u=null;String sql=sel
36、ect id,login,name,passwd,type,status,last_login+ from users where login=? and passwd=?;trypstmt=conn.prepareStatement(sql);pstmt.setString(1, userLogname);pstmt.setString(2, userPwd);rs=pstmt.executeQuery();if(rs.next()u=new User();u.setId(rs.getInt(1);u.setLogin(rs.getString(2);u.setName(rs.getStri
37、ng(3);u.setPassword(rs.getString(4);u.setType(rs.getInt(5);u.setStatus(rs.getInt(6);u.setLast_login(rs.getTimestamp(7);catch(SQLException e)e.printStackTrace();finallyDBUtil.closeJDBC(rs, pstmt, conn);return u;public User selectById(int Id) User user=new User();Connection conn=DBUtil.getConnection()
38、;PreparedStatement pstmt=null;ResultSet rs=null;String sql=select * from userss where id=?;trypstmt=conn.prepareStatement(sql);pstmt.setInt(1, Id);rs=pstmt.executeQuery();if(rs.next()user.setId(rs.getInt(1);user.setLogin(rs.getString(2);user.setName(rs.getString(3);user.setPassword(rs.getString(4);u
39、ser.setType(rs.getInt(5);user.setStatus(rs.getInt(6);catch(SQLException e)e.printStackTrace();finallyDBUtil.closeJDBC(rs, pstmt, conn);return user;public List selectAll()List list=new ArrayList();Connection conn=DBUtil.getConnection();PreparedStatement pstmt=null;ResultSet rs=null;String sql=select
40、* from userss order by id desc;trypstmt=conn.prepareStatement(sql);rs=pstmt.executeQuery();while(rs.next()User user=new User();user.setId(rs.getInt(1);user.setLogin(rs.getString(2);user.setName(rs.getString(3);user.setPassword(rs.getString(4);user.setType(rs.getInt(5);user.setStatus(rs.getInt(6);lis
41、t.add(user);catch(SQLException e)e.printStackTrace();finallyDBUtil.closeJDBC(rs, pstmt, conn);return list;/userdao中的update方法public int update(User users)int n=-1;Connection conn=DBUtil.getConnection();/连接对象PreparedStatement pstmt=null;/语句对象String sql=update userss + set login=?,name=?,passwd=123,typ
42、e=?,status=? where id=?;trypstmt=conn.prepareStatement(sql);pstmt.setString(1, users.getLogin();pstmt.setString(2, users.getName();pstmt.setInt(3, users.getType();pstmt.setInt(4, users.getStatus();pstmt.setInt(5,users.getId();n=pstmt.executeUpdate();/n=更新的行数catch(SQLException e)e.printStackTrace();finallyDBUtil.closeJDBC(null, pstmt, conn);re