1 package com.am.utils; 2
3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 //import java.sql.ResultSet;
7 import java.sql.SQLException; 8 import java.sql.Statement; 9
10
11 /**
12 * JDBC類:鏈接數據庫 13 * 一、加載驅動 14 * 二、建立鏈接 15 * 三、建立普通SQL通道和預編譯通道 16 * 四、 17 * @author Honcker 18 * 19 */
20
21 public class DataBase { 22 /**路徑**/
23 String url="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"; 24 /**帳號**/
25 String username="root"; 26 /**密碼**/
27 String password="123456"; 28
29 /**靜態初始化塊:直接加載一次驅動,與類同生共死**/
30 static { 31 try { 32 /**加載驅動**/
33 Class.forName("com.mysql.cj.jdbc.Driver"); 34 } catch (ClassNotFoundException e) { 35 System.out.println("DateBase提示: 加載驅動失敗。。。"); 36 } 37 } 38
39 /**
40 * 定義屬性: 41 * 【鏈接】、 42 * 【預編譯SQL通道】、 43 * 【普通SQL通道】 44 */
45 Connection conn=null; 46 PreparedStatement ps=null; 47 Statement stat = null; 48
49 /**建立鏈接**/
50 public Connection getConn() { 51
52 try { 53 conn=DriverManager.getConnection(url, username, password); 54 } catch (SQLException e) { 55 System.out.println(this.getClass().getSimpleName()+"提示:數據庫鏈接失敗!!!"); 56 } 57 return conn; 58 } 59
60 /**建立普通SQL通道**/
61 public Statement getStat() { 62 try { 63 stat=getConn().createStatement(); 64 } catch (SQLException e) { 65 System.out.println(this.getClass().getSimpleName()+"提示:建立普通SQL通道失敗!!!"); 66 } 67 return stat; 68
69 } 70
71 /**建立預編譯SQL通道**/
72 public PreparedStatement getPs(String sql) { 73
74 try { 75 ps=getConn().prepareStatement(sql); 76 } catch (SQLException e) { 77 System.out.println(this.getClass().getSimpleName()+"提示:建立預編譯SQL通道失敗!!!"); 78 } 79
80 return ps; 81 } 82
83 /**關閉資源**/
84 public void close() { 85 try { 86 /**關閉結果集**/
87 /**
88 * if(rs!=null) { 89 * rs.close(); 90 * } 91 */
92 /**關閉普通SQL通道**/
93 if(null!=ps) { 94 ps.close(); 95 } 96 /**關閉預編譯SQL通道**/
97 if(null!=stat) { 98 stat.close(); 99 } 100 /**關閉數據庫鏈接**/
101 if(null!=conn) { 102 conn.close(); 103 } 104 } catch (SQLException e) { 105 System.out.println(this.getClass().getSimpleName()+"提示:關閉【普通SQL通道】,【預編譯SQL通道】,【數據庫鏈接】失敗!!!"); 106 } 107 } 108 }
1 package com.am.bean; 2
3 import java.io.Serializable; 4
5 public class User implements Serializable{ 6 private static final long serialVersionUID = -4752686484682049959L; 7 private Integer id; //序號
8 private String name; //姓名
9 private String username; //用戶名
10 private String password; //密碼
11 private String birthday; //生日
12 private Integer age; //年齡
13 private String gender; //性別
14 public User() { 15 super(); 16 // TODO Auto-generated constructor stub
17 } 18
19 /**顯示全部信息專用*/
20 public User(Integer id, String name, String username, String password, String birthday, Integer age, 21 String gender) { 22 super(); 23 this.id = id; 24 this.name = name; 25 this.username = username; 26 this.password = password; 27 this.birthday = birthday; 28 this.age = age; 29 this.gender = gender; 30 } 31
32 /**註冊專用*/
33 public User(String name, String username, String password, String birthday, Integer age, String gender) { 34 super(); 35 this.name = name; 36 this.username = username; 37 this.password = password; 38 this.birthday = birthday; 39 this.age = age; 40 this.gender = gender; 41 } 42
43 public Integer getId() { 44 return id; 45 } 46 public void setId(Integer id) { 47 this.id = id; 48 } 49 public String getName() { 50 return name; 51 } 52 public void setName(String name) { 53 this.name = name; 54 } 55 public String getUsername() { 56 return username; 57 } 58 public void setUsername(String username) { 59 this.username = username; 60 } 61 public String getPassword() { 62 return password; 63 } 64 public void setPassword(String password) { 65 this.password = password; 66 } 67 public String getBirthday() { 68 return birthday; 69 } 70 public void setBirthday(String birthday) { 71 this.birthday = birthday; 72 } 73 public Integer getAge() { 74 return age; 75 } 76 public void setAge(Integer age) { 77 this.age = age; 78 } 79 public String getGender() { 80 return gender; 81 } 82 public void setGender(String gender) { 83 this.gender = gender; 84 } 85 public static long getSerialversionuid() { 86 return serialVersionUID; 87 } 88 @Override 89 public String toString() { 90 return "User [id=" + id + ", name=" + name + ", username=" + username + ", password=" + password + ", birthday="
91 + birthday + ", age=" + age + ", gender=" + gender + "]"; 92 } 93 }
1 package com.am.dao; 2
3 import java.util.List; 4
5 import com.am.bean.User; 6
7 /**
8 * 9 * @author Honcker 10 * 11 */
12 public interface UserDao { 13 /**註冊(添加)用戶**/
14 public int addUser(User user); 15 /**刪除用戶 16 * @return **/
17 public int deleteUser(int id); 18 /**用戶登陸驗證**/
19 public User loginUser(String username,String password); 20 /**修改用戶專用**/
21 public User showUser(int id); 22 /**修改用戶**/
23 public int updateUser(User user); 24 /**顯示全部用戶**/
25 public List<User> showAllUser(); 26
27 }
1 package com.am.service; 2
3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 import java.util.ArrayList; 8 import java.util.List; 9
10 import com.am.bean.User; 11 import com.am.dao.UserDao; 12 import com.am.utils.DataBase; 13 /**
14 * 實現類 15 * @author Honcker 16 * 17 */
18 public class UserService implements UserDao{ 19 /**建立JDBC類對象*/
20 DataBase database=new DataBase(); 21
22 /**添加(註冊)用戶*/
23 @Override 24 public int addUser(User user){ 25 /**建立添加用戶的SQL語句*/
26 String sql="insert into account (name,username,password,birthday,age,gender) values (?,?,?,?,?,?)"; 27 /**得到預編譯SQL通道對象*/
28 PreparedStatement ps = database.getPs(sql); 29 /**聲明受影響的行數,並賦予初始值*/
30 int rows=0; 31 try { 32 /**用戶名、帳號、密碼、生日、年齡、性別**/
33 /*爲佔位符賦值*/
34 ps.setString(1, user.getName()); 35 ps.setString(2, user.getUsername()); 36 ps.setString(3, user.getPassword()); 37 ps.setString(4, user.getBirthday()); 38 ps.setInt(5, user.getAge()); 39 ps.setString(6, user.getGender()); 40 rows = ps.executeUpdate(); 41 } catch (SQLException e) { 42 System.out.println(this.getClass().getSimpleName()+": 註冊失敗。。。"); 43 } 44 return rows; 45 } 46
47
48 /**刪除用戶*/
49 @Override 50 public int deleteUser(int id) { 51 /**建立刪除用戶的SQL語句**/
52 String sql="delete from account where id='"+id+"'"; 53 /**獲取(預編譯)SQL通道對象**/
54 Statement stat = database.getStat(); 55 /**聲明受影響的行數*/
56 int rows=0; 57 try { 58 //ps.setInt(1, id);
59 rows = stat.executeUpdate(sql); 60 } catch (SQLException e) { 61 System.out.println(this.getClass().getSimpleName()+": 刪除失敗。。。"); 62 } 63 return rows; 64 } 65
66 /**登陸校驗*/
67 @Override 68 public User loginUser(String username,String password) { 69 /**建立SQL語句**/
70 String sql="select * from account where username='"+username+"' and password ='"+password+"'"; 71 /**獲取(預編譯)SQL通道對象**/
72 Statement stat = database.getStat(); 73 // PreparedStatement ps = database.getPs(sql);
74 /**建立User類接收查詢的結果**/
75 User user = null; 76 try { 77 // ps.setString(1, username); 78 // ps.setString(2, password);
79 /**獲取查詢結果**/
80 ResultSet rs = stat.executeQuery(sql); 81 user = new User(); 82 /**遍歷查詢結果,並賦值給user類**/
83 while(rs.next()) { 84 user.setId(rs.getInt("id")); 85 user.setName(rs.getString("name")); 86 user.setUsername(rs.getString("username")); 87 user.setPassword(rs.getString("password")); 88 user.setBirthday(rs.getString("birthday")); 89 user.setAge(rs.getInt("age")); 90 user.setGender(rs.getString("gender")); 91 } 92 } catch (SQLException e) { 93 System.out.println(this.getClass().getSimpleName()+": 登陸失敗。。。"); 94 } 95 return user; 96 } 97
98 /****/
99 @Override 100 public User showUser(int id) { 101 String sql="select * from account where id=?"; 102 PreparedStatement ps = database.getPs(sql); 103 ResultSet rs; 104 User user=null; 105 try { 106 ps.setInt(1, id); 107 rs= ps.executeQuery(); 108 while(rs.next()) { 109 user=new User(rs.getInt("id"), rs.getString("name"), rs.getString("username"), rs.getString("password"), rs.getString("birthday"), rs.getInt("age"), rs.getString("gender")); 110 } 111 } catch (SQLException e) { 112 System.out.println("SQLException: 修改失敗"); 113 } 114 return user; 115 } 116
117 /**修改用戶**/
118 @Override 119 public int updateUser(User user) { 120 /**建立SQL語句**/
121 String sql="update account set name=?,username=?,password=?,birthday=?,age=?,gender=? where id=?"; 122 /**聲明受影響的行數**/
123 int rows=0; 124 /**建立預編譯SQL通道**/
125 PreparedStatement ps = database.getPs(sql); 126 try { 127 /**爲佔位符賦值**/
128 ps.setString(1, user.getName()); 129 ps.setString(2, user.getUsername()); 130 ps.setString(3, user.getPassword()); 131 ps.setString(4, user.getBirthday()); 132 ps.setInt(5, user.getAge()); 133 ps.setString(6, user.getGender()); 134 ps.setInt(7, user.getId()); 135 rows=ps.executeUpdate(); 136 } catch (SQLException e) { 137 System.out.println("SQLException: 更新失敗。。。"); 138 } 139 /**返回受影響的行數**/
140 return rows; 141 } 142
143 /**查詢全部的用戶**/
144 @Override 145 public List<User> showAllUser() { 146 /**建立SQL語句**/
147 String sql="select * from account"; 148 /**獲取預編譯SQL通道對象**/
149 PreparedStatement ps = database.getPs(sql); 150 /**聲明結果集變量**/
151 ResultSet rs; 152 /**建立List<User>集合存儲全部的用戶信息list{【user】【user】}**/
153 List<User> list = new ArrayList<User>(); 154 /**聲明User類變量**/
155 User user; 156 try { 157 rs = ps.executeQuery(); 158 while(rs.next()) { 159 Integer id = rs.getInt("id"); 160 String name = rs.getString("name"); 161 String username = rs.getString("username"); 162 String password = rs.getString("password"); 163 String birthday = rs.getString("birthday"); 164 Integer age = rs.getInt("age"); 165 String gender = rs.getString("gender"); 166 /**將每一條記錄存入User對象*/
167 user=new User(id, name, username, password, birthday, age, gender); 168 /**將每個User存入List集合**/
169 list.add(user); 170 } 171 } catch (SQLException e) { 172 System.out.println("SQLException: 顯示數據失敗。。。"); 173 } 174 /**返回存儲全部用戶的List集合**/
175 return list; 176 } 177
178
179 }
一、跳轉到註冊頁面的Servlet:ForwardToRegistServlet.javajavascript
1 package com.am.servlet; 2
3 import java.io.IOException; 4
5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10
11 /**
12 * Jump to regist.jsp 13 * 跳轉至註冊頁面 14 * @author Honcker 15 * 16 */
17 @WebServlet("/regist") 18 public class ForwardToRegistServlet extends HttpServlet{ 19 private static final long serialVersionUID = 5913743008020729050L; 20
21 @Override 22 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 req.getRequestDispatcher("WEB-INF/jsp/regist.jsp").forward(req, resp); 24
25 } 26
27 @Override 28 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 29
30 } 31
32 }
二、註冊頁面:regist.jspcss
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>註冊</title>
8 <link rel="stylesheet" href="././css/account.css" type="text/css">
9
10
11 </head>
12 <body>
13 <div class="lr_allbg">
14 <div class="lr_bar">
15 <h2>註冊帳號</h2>
16 <hr size="3" color="brown">
17 </div>
18 <div class="formbox">
19 <form action="registreg" method="get">
20 <div class="label">用戶名:</div>
21 <div class="input_box">
22 <input type="text" name="name" class="input" placeholder="請輸入用戶名">
23 <span></span>
24 </div>
25 <div class="label">帳號:</div>
26 <div class="input_box">
27 <input type="text" name="username" class="input" placeholder="請輸入帳號">
28 <span></span>
29 </div>
30 <div class="label">密碼:</div>
31 <div class="input_box">
32 <input type="password" name="password" class="input" placeholder="請輸入密碼">
33 <span></span>
34 </div>
35 <div class="label">生日:</div>
36 <div class="input_box">
37 <input type="date" name="birthday" class="input">
38 <span></span>
39 </div>
40 <div class="label">年齡:</div>
41 <div class="input_box">
42 <input type="number" name="age" class="input" placeholder="請輸入年齡">
43 <span></span>
44 </div>
45 <div class="label">性別:</div>
46 <div class="input_box">
47 <input type="radio" name="gender" value="男" checked>男 48 <input type="radio" name="gender" value="女">女 49 </div>
50 <div class="input_box submit_box">
51 <button type="submit">註冊</button>
52 </div>
53 </form>
54 </div>
55 <div class="rt_img">
56 <a href="http://localhost/${pageContext.request.contextPath}/ftlogin"><img alt="鼠寶寶" src="././images/mouse.jpg" width="300px" title="已有帳號,去登陸"/></a>
57 </div>
58 </div>
59 </body>
60 </html>
三、處理註冊信息的Servlet:RegistServlet.javahtml
1 package com.am.servlet; 2
3 import java.io.IOException; 4
5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11
12 import com.am.bean.User; 13 import com.am.service.UserService; 14
15 /**
16 * 處理註冊的信息 17 * 將帳號和密碼存入數據庫 18 * Servlet implementation class RegistServlet 19 */
20 @WebServlet("/registreg") 21 public class RegistServlet extends HttpServlet { 22 private static final long serialVersionUID = -7336946611411448207L; 23 UserService us = new UserService(); 24 @Override 25 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 26 /**經過request獲取姓名、帳號、密碼、生日、年齡、性別**/
27 String name=req.getParameter("name"); 28 String username=req.getParameter("username"); 29 String password=req.getParameter("password"); 30 String birthday=req.getParameter("birthday"); 31 Integer age=Integer.parseInt(req.getParameter("age")); 32 String gender=req.getParameter("gender"); 33
34 /**將註冊的帳號密碼存入Session中,以便於登陸**/
35 HttpSession session = req.getSession(); 36 session.setAttribute("username",username); 37 session.setAttribute("password", password); 38
39 /**用user類儲存註冊的信息**/
40 User user=new User(name, username, password, birthday, age, gender); 41 /**經過service層將user添加到數據庫**/
42 int rows = us.addUser(user); 43 if(rows>0) { 44 System.out.println(this.getClass().getSimpleName()+"提示: 註冊了"+rows+"個帳號"); 45 /**跳轉至登陸頁面**/
46 req.getRequestDispatcher("ftlogin").forward(req, resp); 47
48 }else { 49 System.out.println(this.getClass().getSimpleName()+"提示: 註冊失敗"); 50 } 51 } 52 @Override 53 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 54
55 } 56 }
四、跳轉到登陸頁面的Servlet:ForwardToLoginServlet.javajava
1 package com.am.servlet; 2
3 import java.io.IOException; 4
5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10
11 /**
12 * 跳轉到登陸 13 * @author Honcker 14 */
15 @WebServlet("/ftlogin") 16 public class ForwardToLoginServlet extends HttpServlet{ 17 private static final long serialVersionUID = 4465872810151666786L; 18
19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 req.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(req, resp); 22 } 23
24 @Override 25 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 26
27 } 28
29 }
五、登陸頁面:login.jspmysql
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>登陸</title>
8 <link rel="stylesheet" href="././css/account.css" type="text/css">
9 </head>
10 <body>
11 <div class="lr_allbg">
12 <div class="lr_bar">
13 <h2>帳號登陸</h2>
14 <hr size="3" color="brown">
15 </div>
16 <div class="formbox">
17 <form action="loginreg" method="get">
18 <div class="label">
19 帳號: 20 </div>
21 <div class="input_box">
22 <input type="text" name="username" class="input" value="${sessionScope.username}" placeholder="請輸入帳號">
23 <span></span>
24 </div>
25 <div class="label">
26 密碼: 27 </div>
28 <div class="input_box">
29 <input type="password" name="password" class="input" value="${sessionScope.password }" placeholder="請輸入密碼">
30 <span></span>
31 </div>
32 <div class="input_box submit_box">
33 <button type="submit">登陸</button>
34 </div>
35 </form>
36 </div>
37 <div class="lt_img">
38 <a href="http://localhost/${pageContext.request.contextPath}/regist"><img alt="小老鼠" src="././images/mouse2.jpg" title="沒有帳號?點我" width="200px"></a>
39 </div>
40 </div>
41 </body>
42 </html>
六、處理登陸信息的Servlet:LoginServlet.javajquery
1 package com.am.servlet; 2
3 import java.io.IOException; 4 import java.io.PrintWriter; 5
6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12
13 import com.am.bean.User; 14 import com.am.dao.UserDao; 15 import com.am.service.UserService; 16
17 /**
18 * LoginServlet: 19 * 登陸驗證 20 * @author Honcker 21 * 22 */
23 @WebServlet("/loginreg") 24 public class LoginServlet extends HttpServlet{ 25 private static final long serialVersionUID = -6376546296575630791L; 26 UserDao ud = new UserService(); 27
28 @Override 29 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 30 String username = req.getParameter("username"); 31 String password = req.getParameter("password"); 32 System.out.println("帳號:"+username+"密碼:"+password); 33
34 /**獲取到當前登陸的帳號**/
35 User user = ud.loginUser(username, password); 36 /**將登陸的帳戶的信息存入Session**/
37 /**
38 * HttpSession session = req.getSession(); 39 * session.setAttribute("user",user); 40 */
41 /**校驗帳號密碼**/
42 if(user!=null) { 43 if(user.getUsername().equals(username)&&user.getPassword().equals(password)) { 44 System.out.println(this.getClass().getSimpleName()+"提示:\n"+user.getName()+"登陸成功!!!"); 45 req.getRequestDispatcher("ftindex").forward(req, resp); 46 } 47 }else { 48 PrintWriter pw = resp.getWriter(); 49 pw.print("帳戶不存在"); 50 pw.flush(); 51 pw.close(); 52 } 53 } 54
55 @Override 56 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 57
58 } 59 }
七、跳轉到首頁的Servlet:ForwardToIndexServlet.javasql
1 package com.am.servlet; 2
3 import java.io.IOException; 4 import java.util.List; 5
6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12
13 import com.am.bean.User; 14 import com.am.dao.UserDao; 15 import com.am.service.UserService; 16
17 @WebServlet("/ftindex") 18 public class ForwardToIndexServlet extends HttpServlet{ 19 private static final long serialVersionUID = 4588762042246354545L; 20 UserDao ud = new UserService(); 21
22 @Override 23 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 24 /**獲取全部的用戶信息**/
25 List<User> list = ud.showAllUser(); 26
27 /**將存儲全部用戶信息的List<User>存儲在Session中**/
28 HttpSession session = req.getSession(); 29 session.setAttribute("list", list); 30 /**
31 * User user = (User) session.getAttribute("user"); 32 * session.setAttribute("user",user); 33 */
34 // resp.sendRedirect("");
35 req.getRequestDispatcher("WEB-INF/jsp/index.jsp").forward(req, resp); 36 } 37
38 @Override 39 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 40
41 } 42 }
八、首頁:index.jsp數據庫
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta charset="UTF-8">
9 <title>帳戶管理首頁</title>
10 <!-- 引入CSS樣式表 -->
11 <link rel="stylesheet" href="././css/index.css" type="text/css">
12 <!-- 引入Jquery包 -->
13 <script type="text/javascript" src="././js/jquery-3.4.1.min.js"></script>
14 <script type="text/javascript" src="././js/index.js"></script>
15 </head>
16 <body>
17 <div id="index_allbg">
18 <!-- 當前用戶 -->
19 <div class="login_user">
20 <span>當前用戶: <%-- ${sessionScope.user.name} --%></span>
21 </div>
22 <!-- 退出系統 -->
23 <div class="exit_box">
24 <a href="http://localhost/${pageContext.request.contextPath}/ftlogin"><button class="exit_btn">退出系統</button></a>
25 </div>
26 <div class="index_bar">
27 <h2>帳戶管理</h2>
28 <hr size="3" color="brown">
29 </div>
30 <!-- 全部用戶的表格 -->
31 <div class="amtb_box">
32 <table border="1">
33 <tr>
34 <td>序號</td>
35 <td>姓名</td>
36 <td>用戶名</td>
37 <td>生日</td>
38 <td>年齡</td>
39 <td>性別</td>
40 <td>操做</td>
41 </tr>
42 <c:forEach items="${sessionScope.list}" var="user" varStatus="type">
43 <tr>
44 <td>${type.index+1}</td>
45 <td>${user.name}</td>
46 <td>${user.username}</td>
47 <td>${user.birthday}</td>
48 <td>${user.age}</td>
49 <td>${user.gender}</td>
50 <td colspan="2">
51 <a href="ftupdate?id=${user.id}"><button name="upt" class="btn">修改</button></a>
52 <button name="del" value="${user.id}" class="btn">刪除</button>
53 </td>
54 </tr>
55 </c:forEach>
56 </table>
57 </div>
58 </div>
59 </body>
60 </html>
九、刪除的Servlet:DeleteServlet.javasession
1 package com.am.servlet; 2
3 import java.io.IOException; 4 import java.util.List; 5
6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12
13 import com.am.bean.User; 14 import com.am.dao.UserDao; 15 import com.am.service.UserService; 16
17 /**
18 * 刪除用戶Servlet 19 * @author Honcker 20 * 21 */
22 @WebServlet("/delete") 23 public class DeleteServlet extends HttpServlet{ 24 private static final long serialVersionUID = -2553575351064163507L; 25 UserDao ud = new UserService(); 26
27 @Override 28 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 29 Integer id = Integer.parseInt(req.getParameter("id")); 30 int rows = ud.deleteUser(id); 31 HttpSession session = req.getSession(); 32 User user= (User)session.getAttribute("user"); 33 session.setAttribute("user", user); 34 if(rows>0) { 35 System.out.println(this.getClass().getSimpleName()+"提示: 刪除"+rows+"個用戶成功。。。"); 36 req.getRequestDispatcher("ftindex").forward(req, resp); 37 }else { 38 System.out.println("受影響行數:"+rows+"行,刪除失敗。。。"); 39 } 40 } 41 }
十、跳轉到更新頁面的Servlet:ForwardToUpdateServlet.javajsp
1 package com.am.servlet; 2
3 import java.io.IOException; 4
5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11
12 import com.am.bean.User; 13 import com.am.dao.UserDao; 14 import com.am.service.UserService; 15
16 @WebServlet("/ftupdate") 17 public class ForwardToUpdateServlet extends HttpServlet{ 18 private static final long serialVersionUID = -1511111251877011866L; 19 UserDao ud = new UserService(); 20 @Override 21 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 Integer id = Integer.parseInt(req.getParameter("id")); 23 User user = ud.showUser(id); 24
25 HttpSession session = req.getSession(); 26 session.setAttribute("user", user); 27 req.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(req, resp); 28
29
30 } 31 }
十一、更新頁面:update.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>修改</title>
8 <link rel="stylesheet" href="././css/account.css" type="text/css">
9 </head>
10 <body>
11 <div class="lr_allbg">
12 <div class="lr_bar">
13 <h2>修改帳戶</h2>
14 <hr size="3" color="brown">
15 </div>
16 <div class="formbox">
17 <form action="update" method="get">
18 <div class="label">用戶名:</div>
19 <div class="input_box">
20 <input type="text" name="name" class="input" value="${sessionScope.user.name }" placeholder="請輸入新的用戶名">
21 <span></span>
22 </div>
23 <div class="label">帳號:</div>
24 <div class="input_box">
25 <input type="text" name="username" value="${sessionScope.user.username}" class="input">
26 <span></span>
27 </div>
28 <div class="label">密碼:</div>
29 <div class="input_box">
30 <input type="text" name="password" class="input" value="${sessionScope.user.password }" placeholder="請輸入新密碼">
31 <span></span>
32 </div>
33 <div class="label">生日:</div>
34 <div class="input_box">
35 <input type="date" name="birthday" class="input" value="${sessionScope.user.birthday}">
36 <span></span>
37 </div>
38 <div class="label">年齡:</div>
39 <div class="input_box">
40 <input type="number" name="age" class="input" value="${sessionScope.user.age }" placeholder="請輸入年齡">
41 <span></span>
42 </div>
43 <div class="label">性別:</div>
44 <div class="input_box">
45 <input type="radio" name="gender" value="男" checked>男 46 <input type="radio" name="gender" value="女">女 47 </div>
48 <div class="input_box submit_box">
49 <button type="submit" value="${sessionScope.user.id }" name="id">保存</button>
50 </div>
51 </form>
52 </div>
53 <div class="rt_img">
54 <a href="http://localhost/${pageContext.request.contextPath}/ftlogin"><img alt="鼠寶寶" src="././images/mouse.jpg" width="300px" title="已有帳號,去登陸"/></a>
55 </div>
56 </div>
57 </body>
58 </html>
十二、處理更新的Servlet:UpdateServlet.java
1 package com.am.servlet; 2
3 import java.io.IOException; 4
5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10
11 import com.am.bean.User; 12 import com.am.dao.UserDao; 13 import com.am.service.UserService; 14
15 /** 16 * 17 * 修改用戶信息 18 * @author Honcker 19 * 20 */ 21 @WebServlet("/update") 22 public class UpdateServlet extends HttpServlet{ 23 private static final long serialVersionUID = 2663414008874647347L; 24 UserDao ud = new UserService(); 25
26 @Override 27 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 Integer id = Integer.parseInt(req.getParameter("id")); 29 String name = req.getParameter("name"); 30 String username = req.getParameter("username"); 31 String password = req.getParameter("password"); 32 String birthday = req.getParameter("birthday"); 33 Integer age = Integer.parseInt(req.getParameter("age")); 34 String gender = req.getParameter("gender"); 35 User user = new User(id,name, username, password, birthday, age, gender); 36
37 int rows = ud.updateUser(user); 38 if(rows>0) { 39 System.out.println("修改爲功。。。"); 40 req.getRequestDispatcher("ftindex").forward(req, resp); 41
42 }else { 43 System.out.println(this.getClass().getSimpleName()+"提示: 修改失敗。。。"); 44 } 45
46
47 } 48
49
50
51 }