index.html -登錄->stulist.jsp (index.html傳遞到LoginServlet,進行登錄檢測及寫入session,NO返回index.html界面,OK 跳轉到stulist.jsp)
stulist.jsp 實現對學生的增刪改查及分頁.(stulist.jsp檢測是否有session,以及student列表中是否有學生信息,沒有session返回index頁面,student列表中沒有信息調用GetStudentServlet獲取學生信息,獲取過程中已分頁)
數據庫操作類詳見:http://www.cnblogs.com/A--Q/p/6137525.html
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 package com.mis.dao; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import com.mis.bean.Student; 7 8 public class StudentDao extends DBOper{ 9 public ArrayList<Student> 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<Student> students = new ArrayList<Student>(); 17 ResultSet rs = executeQuery(sql,null); 18 try { 19 while(rs.next()){ 20 Student s = new Student(); 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 return students; 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 return rs.getRow(); 43 } catch (SQLException e) { 44 return 0; 45 } 46 } 47 public ArrayList<Student> 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<Student> students = new ArrayList<Student>(); 53 ResultSet rs = executeQuery(sql,null); 54 try { 55 while(rs.next()){ 56 Student s = new Student(); 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 return students; 69 } 70 public int addDelUp(String sql) { 71 int num=0; 72 num=executeUpdate(sql,null); 73 if(num!=0)return num; 74 return 0; 75 } 76 }
GetStudentServlet_extend
1 package com.mis.servlet; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.mis.bean.Student; 13 import com.mis.dao.StudentDao; 14 15 public class GetStudentServlet_extend extends HttpServlet { 16 private static final long serialVersionUID = 1L; 17 18 public GetStudentServlet_extend() { 19 super(); 20 } 21 22 // 執行stulist.jsp傳入的數據進行刪除和修改數據,flag=1 刪除 flag=2修改 23 protected void doGet(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, 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 = new StudentDao(); 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<Student> 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 void doPost(HttpServletRequest request, HttpServletResponse response) 81 throws ServletException, IOException { 82 doGet(request, response); 83 } 84 }
stulist.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.util.*"%> 4 <%@ page import="java.lang.Math"%> 5 <%@ page import="com.mis.bean.Student"%> 6 <%@ page import="com.mis.dao.DBOper"%> 7 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 8 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10 <html> 11 <head> 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13 <title>學生信息列表</title> 14 15 <script src="js_jquery/stulist.js" charset="UTF-8"></script> 16 <link rel="stylesheet" href="css/stulist.css"> 17 </head> 18 <body> 19 <c:if test="${username==null }"> 20 <jsp:forward page="index.html"></jsp:forward> 21 </c:if> 22 <c:if test="${students==null}"> 23 <jsp:forward page="getStudent"></jsp:forward> 24 </c:if> 25 26 <table border="0" align="center" cellspacing="0" class="list_table" 27 id="senfe" style='width: 90%'> 28 <thead> 29 <tr> 30 <td colspan='7'> 31 <form method="POST" name="form" action="getStudent"> 32 <!-- 開發階段 沒有對用戶的非法輸入進行過濾--> 33 <input type="hidden" name="flag" value="1"> <input 34 type="text" name="stuid" value="學號" 35 onclick="if(this.value == '學號')this.value ='';" 36 onBlur="if(this.value == ' '||this.value == '')this.value ='學號';"> 37 <input type="text" name="stuname" value="姓名" 38 onclick="if(this.value == '姓名')this.value ='';" 39 onBlur="if(this.value == ' '||this.value == '')this.value ='姓名';"> 40 <input type="text" name="stusex" value="性別" 41 onclick="if(this.value == '性別')this.value ='';" 42 onBlur="if(this.value == ' '||this.value == '')this.value ='性別';"> 43 <input type="text" name="stuage" value="年齡" 44 onclick="if(this.value == '年齡')this.value ='';" 45 onBlur="if(this.value == ' '||this.value == '')this.value ='年齡';"> 46 <input type="text" name="stumajor" value="專業" 47 onclick="if(this.value == '專業')this.value ='';" 48 onBlur="if(this.value == ' '||this.value == '')this.value ='專業';"> 49 <input type="radio" name="selAdd" checked onclick="selop()"> 50 <input type="submit" value="查詢" id="selcon"> <input 51 type="radio" name="selAdd" onclick="selop()"><input 52 type="submit" disabled value="增加" id="addcon" onclick="setPar()"> 53 </form> 54 </td> 55 </tr> 56 <tr> 57 <th>學號</th> 58 <th>姓名</th> 59 <th>性別</th> 60 <th>年齡</th> 61 <th>專業</th> 62 <th>刪除</th> 63 <th>修改</th> 64 </tr> 65 </thead> 66 <tbody> 67 <form method="post" name="DelUp" action="getStudent_extend"> 68 <c:forEach var="student" items="${students }"> 69 <tr align="center"> 70 <td>${student.stuno }</td> 71 <td>${student.name }</td> 72 <td>${student.sex }</td> 73 <td>${student.age }</td> 74 <td align="left">${student.major }</td> 75 <td><input type="submit" name=${student.stuno } value="刪除" 76 onclick="del(this)"></td> 77 <td><input type="button" name=${student.stuno } value="修改" 78 onclick="updata(this)"></td> 79 </tr> 80 </c:forEach> 81 82 <div id="updateDiv"> 83 學號:<input type="text" name="ud" readOnly> 84 姓名:<input type="text" name="ud_name"> 85 性別:<input type="text" name="ud_sex"> 86 年齡:<input type="text" name="ud_age"> 87 專業:<input type="text" name="ud_major"> 88 89 <input type="submit" value="更新"> 90 <span id="cancelUpdate" onclick="cancelupdate()">取消</span> 91 </div> 92 93 <input type="hidden" name="flag_delUp"> <input type="hidden" 94 name="parameter_del"> 95 </form> 96 </tbody> 97 </table> 98 <div id="pageCount" style="width:600px;margin-left:auto;margin-right:auto;"> 99 <c:if test="${stuCount!=null}"> 100 <br /><c:if test="${condition!=null&&condition!=''}">查詢條件為<c:out value="${condition}"></c:out></c:if> 101 <c:out value="${stuCount}"></c:out>條記錄,<!-- Math.ceil() 向上取整 -- (stuCount-((stuCount-1)%10))/10+1 ** --> 102 共<fmt:formatNumber type="number" value="${(stuCount-((stuCount-1)%10))/10+1}" maxFractionDigits="0"/>頁 103 ,當前為第<c:out value="${currentPage}"></c:out>頁 104 105 <form method="post" name="paging" action="getStudent"> 106 <input type="text" value="" name="page" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"> 107 <input type="hidden" name="flag" value="3"> 108 <input type="hidden" name="parame" value='<c:out value="${condition}"></c:out>'> 109 <input type="submit" value="跳轉" onclick="checkPage()"> 110 </form> 111 </c:if> 112 </div> 113 </body> 114 </html>
源代碼:https://pan.baidu.com/s/1e_mTr1Rf4BoehNBwDEg76g 密碼:8qg3