index.html -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到stulist.jsp)
stulist.jsp 实现对学生的增删改查及分页.(stulist.jsp检测是否有session,以及student列表中是否有学生信息,没有session返回index页面,student列表中没有信息调用GetStudentServlet获取学生信息,获取过程中已分页)
stulist.jsp界面如下:
查询:可以实现组合查询,输入条件,点击‘查询’即可
增加:在上方输入完整学生信息后,选择‘添加’选项按钮,添加可用,点击‘添加’,即将刚才填写的学生信息,增加的数据库中
删除:在要删除的学生后点击‘删除’,弹出‘询问框’,是否要删除该学生信息,选择‘是’从数据库中删除该学生,选择‘否’取消对该学生的删除操作。
修改:在要修改的学生后点击‘修改’,表格上方,显示一排信息框,除学号外,填写好更改的信息,点击更新,即可,或者点击‘取消’,取消本次的更新。
(如果删除了该学生,则该页面会刷新,上方的更新框会隐藏,即不会存在逻辑性错误)。
对于分页:
这里没有让用户输入每页显示多少条信息,而是笔者自己再代码中写好的,每页显示10条,如果没有条件则显示如下:
如果加上条件,则显示为:
在stulist.jsp 页面中共有两个表单,一个表单传入到 GetStudentServlet执行stulist.jsp传入的数据进行分页跳转,插入输入,查询数据,flag=3 分页跳转 flag=2插入 flag=1查询,一个表单传入到GetStudentServlet_extend执行stulist.jsp传入的数据进行删除和修改数据,flag=1 删除 flag=2修改
以上两个Servlet操作完后都会返回student数组列表对象在stulist显示。(使用StudentDao对象得到student数组列表)
StudentDao:
1 packagecom.mis.dao;2
3 importjava.sql.ResultSet;4 importjava.sql.SQLException;5 importjava.util.ArrayList;6 importcom.mis.bean.Student;7
8 public class StudentDao extendsDBOper{9 public ArrayList getStudent(int flag,String parameter,int crrentPage){//分页查询
10 String sql="select id,stuno,name,sex,age,major from student order by id desc";11 if (flag>0) {12 sql= "select * from student where "+parameter;13 }14 crrentPage=(crrentPage==0?0:crrentPage-1);15 sql+=" limit "+crrentPage*10+",10";//默认每页显示
16 ArrayList students = new ArrayList();17 ResultSet rs = executeQuery(sql,null);18 try{19 while(rs.next()){20 Student s = newStudent();21 s.setId(rs.getInt("id"));22 s.setStuno(rs.getString("stuno"));23 s.setName(rs.getString("name"));24 s.setSex(rs.getString("sex"));25 s.setAge(rs.getInt("age"));26 s.setMajor(rs.getString("major"));27 students.add(s);28 }29 } catch(SQLException e) {30 e.printStackTrace();31 }32 returnstudents;33 }34 public int getStudentCount(int flag,String parameter){//得到查询的总条数
35 String sql="select id,stuno,name,sex,age,major from student";36 if (flag>0) {37 sql= "select * from student where "+parameter;38 }39 ResultSet rs = executeQuery(sql,null);40 try{41 rs.last();42 returnrs.getRow();43 } catch(SQLException e) {44 return 0;45 }46 }47 public ArrayList getStudent(int flag,String parameter){//未分页查询
48 String sql="select id,stuno,name,sex,age,major from student order by id";49 if (flag>0) {50 sql= "select * from student where "+parameter;51 }52 ArrayList students = new ArrayList();53 ResultSet rs = executeQuery(sql,null);54 try{55 while(rs.next()){56 Student s = newStudent();57 s.setId(rs.getInt("id"));58 s.setStuno(rs.getString("stuno"));59 s.setName(rs.getString("name"));60 s.setSex(rs.getString("sex"));61 s.setAge(rs.getInt("age"));62 s.setMajor(rs.getString("major"));63 students.add(s);64 }65 } catch(SQLException e) {66 e.printStackTrace();67 }68 returnstudents;69 }70 public intaddDelUp(String sql) {71 int num=0;72 num=executeUpdate(sql,null);73 if(num!=0)returnnum;74 return 0;75 }76 }
GetStudentServlet_extend
1 packagecom.mis.servlet;2
3 importjava.io.IOException;4 importjava.util.ArrayList;5
6 importjavax.servlet.ServletContext;7 importjavax.servlet.ServletException;8 importjavax.servlet.http.HttpServlet;9 importjavax.servlet.http.HttpServletRequest;10 importjavax.servlet.http.HttpServletResponse;11
12 importcom.mis.bean.Student;13 importcom.mis.dao.StudentDao;14
15 public class GetStudentServlet_extend extendsHttpServlet {16 private static final long serialVersionUID = 1L;17
18 publicGetStudentServlet_extend() {19 super();20 }21
22 //执行stulist.jsp传入的数据进行删除和修改数据,flag=1 删除 flag=2修改
23 protected voiddoGet(HttpServletRequest request, HttpServletResponse response)24 throwsServletException, IOException {25 request.setCharacterEncoding("utf-8");26 response.setContentType("text/html;charset=utf-8");27 ServletContext ctx = this.getServletContext();28 String server = ctx.getInitParameter("server");29 String dbname = ctx.getInitParameter("dbname");30 String user = ctx.getInitParameter("user");31 String pwd = ctx.getInitParameter("pwd");32 StudentDao dao = newStudentDao();33 String sql = null;34 int flag = 0;35 try{36 flag = Integer.parseInt(request.getParameter("flag_delUp"));37 } catch(Exception e) {38 flag = 0;39 }40 String stuid = request.getParameter("ud");41 String stuname = request.getParameter("ud_name");42 String stusex = request.getParameter("ud_sex");43 String stuage = request.getParameter("ud_age");44 String stumajor = request.getParameter("ud_major");45 String del_stuid = request.getParameter("parameter_del");46 if (flag == 1) {//执行删除
47 try{48 flag = 0;49 sql = "Delete from student where stuno='" + del_stuid + "';";50 dao.getConn(server, dbname, user, pwd);51 dao.addDelUp(sql);52 } catch(Exception e) {53 e.printStackTrace();54 }55 } else if (flag == 2) {//执行修改
56 flag = 0;57 sql = "update student set name='" + stuname + "',sex='" + stusex + "',age='" + stuage + "',major='"
58 + stumajor + "' where stuno='" + stuid + "';";59 try{60 dao.getConn(server, dbname, user, pwd);61 dao.addDelUp(sql);62 } catch(Exception e) {63 e.printStackTrace();64 }65 }66 try{67 dao.getConn(server, dbname, user, pwd);68 ArrayList students = dao.getStudent(0, null, 0);69 request.setAttribute("stuCount", dao.getStudentCount(0, null));70 request.setAttribute("students", students);71 request.setAttribute("currentPage", 1);//默认用户打开的分页第一页
72 request.getRequestDispatcher("stulist.jsp").forward(request, response);73 return;74 } catch(Exception e) {75 e.printStackTrace();76 }77
78 }79
80 protected voiddoPost(HttpServletRequest request, HttpServletResponse response)81 throwsServletException, IOException {82 doGet(request, response);83 }84 }
stulist.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
9
10
11
12
13
学生信息列表
14
15
16
17
18
19
20
21
22
23
24
25
26
27 id="senfe"style='width:90%'>
28
29
30
31
32
33
35 οnclick="if(this.value == '学号')this.value ='';"
36 onBlur="if(this.value == ' '||this.value == '')this.value ='学号';">
37
38 οnclick="if(this.value == '姓名')this.value ='';"
39 onBlur="if(this.value == ' '||this.value == '')this.value ='姓名';">
40
41 οnclick="if(this.value == '性别')this.value ='';"
42 onBlur="if(this.value == ' '||this.value == '')this.value ='性别';">
43
44 οnclick="if(this.value == '年龄')this.value ='';"
45 onBlur="if(this.value == ' '||this.value == '')this.value ='年龄';">
46
47 οnclick="if(this.value == '专业')this.value ='';"
48 onBlur="if(this.value == ' '||this.value == '')this.value ='专业';">
49
50
53
54
55
56
57
学号
58
姓名
59
性别
60
年龄
61
专业
62
删除
63
修改
64
65
66
67
68
69
70
${student.stuno }
71
${student.name }
72
${student.sex }
73
${student.age }
74 ${student.major }
75
76 οnclick="del(this)">
77
78 οnclick="updata(this)">
79
80
81
82
83 学号:
84 姓名:
85 性别:
86 年龄:
87 专业:
88
89
90 取消
91
92
93
94 name="parameter_del">
95
96
97
98
99
100 查询条件为
101 条记录,
102 共页103 ,当前为第页104
105
106
107
108 '>109
110
111
112