現在開始上截圖:
下面粘貼代碼:
首先是目錄結構:
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>add.jsp</title> <script type= "text/javascript" src= "<c:url value='/jquery/contactor.js'/>" ></script> <style type= "text/css" > body{ text-align: center; font-size:38px; } *{ font-family:華文楷體; font-size:18px; color:#f0f"; } h1{ text-align: center; font-size:38px; } </style> </head> <body > <h1 style= "font-size:38px;" >添加新的聯系人</h1> <form action= "<c:url value='/contactorservlet?userid=${userid } '/>" method= "post" onsubmit= "return checkform()" > <input type= "hidden" name= "method" value= "4" > <table border= "1" width= "50%" align= "center" > <tr> <td>姓名</td> <td> <input type= "text" name= "name" id= "name" onfocus= "namefocus()" onblur= "nameblur()" /> <div id= "nameid" ></div> </td> </tr> <tr> <td>電話</td> <td> <input type= "text" name= "tel" id= "tel" onfocus= "telfocus()" onblur= "telblur()" /> <div id= "telid" ></div> </td> </tr> <tr> <td>地址</td> <td> <input type= "text" name= "address" id= "address" /> </td> </tr> <tr> <td>郵編</td> <td> <input type= "text" name= "zipcode" /> </td> </tr> </table> <input style= "margin-right:200px;" type= "submit" value= "添加" /> </form> </body> </html> |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>edit.jsp</title> <script type= "text/javascript" src= "<c:url value='/jquery/contactor.js'/>" ></script> <style type= "text/css" > body{ text-align: center; font-size:38px; } *{ font-family:華文楷體; font-size:18px; color:#f0f"; } h1{ text-align: center; font-size:38px; } </style> </head> <body > <h1 style= "font-size:38px;" >修改聯系人信息</h1> <form action= "<c:url value='/contactorservlet'/>" method= "post" onsubmit= "return checkform()" > <input type= "hidden" name= "method" value= "6" > <input type= "hidden" name= "id" value= "${contactor.id }" > <table border= "1" width= "50%" align= "center" > <tr> <td>姓名</td> <td> <input type= "text" name= "name" value= "${contactor.name }" id= "name" onfocus= "namefocus()" onblur= "nameblur()" /> <div id= "nameid" ></div> </td> </tr> <tr> <td>電話</td> <td> <input type= "text" name= "tel" value= "${contactor.tel }" id= "tel" onfocus= "telfocus()" onblur= "telblur()" /> <div id= "telid" ></div> </td> </tr> <tr> <td>地址</td> <td> <input type= "text" id= "address" name= "address" value= "${contactor.address }" /> </td> </tr> <tr> <td>郵編</td> <td> <input type= "text" name= "zipcode" value= "${contactor.zipcode }" /> </td> </tr> </table> <input style= "margin-right:200px;" type= "submit" value= "更改" /> </form> </body> </html> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>home.jsp</title> </head> <frameset rows= "100%,*" > <frame name= "body" src= "<c:url value='/jsp/top.jsp'/>" /> </frameset> </html> |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>list.jsp</title> <style type= "text/css" > body{ text-align: center; font-size:38px; } *{ font-family:華文楷體; font-size:18px; color:#f0f"; } h1{ text-align: center; font-size:38px; } a:link{ color: # 999 ; text-decoration: none; } a:visited { text-decoration: none; color: #c60; } a:hover { text-decoration: none; color: #00f; } </style> </head> <body > <h1 style= "font-size:38px;" >歡迎登錄到通信錄系統</h1> <a style= "font-size:26px;" href= "<c:url value='/contactorservlet?method=3&userid=${userid }'/>" >查看聯系人</a> <a style= "font-size:26px;" href= "<c:url value='/jsp/add.jsp'/>" >添加聯系人</a> <a style= "font-size:26px;" href= "<c:url value='/jsp/top.jsp'/>" >退出系統</a> <table border= "1" align= "center" width= "60%" > <tr> <th>姓名</th> <th>電話</th> <th>地址</th> <th>郵編</th> <th>操作 1 </th> <th>操作 2 </th> </tr> <c:foreach items= "${contactorlist }" var= "c" > <tr> <td>${c.name }</td> <td>${c.tel }</td> <td>${c.address }</td> <td>${c.zipcode }</td> <td><a href= "<c:url value='/contactorservlet?method=5&id=${c.id }'/>" >編輯</a> </td> <td> <a href= "<c:url value='/contactorservlet?method=7&id=${c.id }'/>" >刪除</a> </td> </tr> </c:foreach> </table> </body> </html> |
1
2
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
28
29
30
31
32
33
34
35
36
37
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>top.jsp</title> <style type= "text/css" > body{ text-align: center; font-size:38px; } *{ font-family:華文楷體; font-size:18px; color:#f0f"; } h1{ text-align: center; font-size:38px; } </style> </head> <body > <h1 style= "font-size:38px;" >我的通信錄</h1> <form action= "<c:url value='/contactorservlet'/>" method= "post" > <input type= "hidden" name= "method" value= "1" > 用戶名:<input type= "text" name= "username" /><br/> 密 碼:<input type= "password" name= "password" /><br/> <input type= "submit" value= "注冊" > </form> </body> </html> |
1
2
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
28
29
30
31
32
33
34
35
36
37
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <title>top.jsp</title> <style type= "text/css" > body{ text-align: center; font-size:38px; } *{ font-family:華文楷體; font-size:18px; color:#f0f"; } h1{ text-align: center; font-size:38px; } </style> </head> <body > <h1 style= "font-size:38px;" >我的通信錄</h1> <form action= "<c:url value='/contactorservlet'/>" method= "post" > <input type= "hidden" name= "method" value= "2" > 用戶名:<input type= "text" name= "username" /><br/> 密 碼:<input type= "password" name= "password" /><br/> <input type= "submit" value= "登錄" > <a href= "<c:url value='/jsp/register.jsp'/>" target= "body" >注冊</a> </form> </body> </html> |
index.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<jsp:forward page="/jsp/home.jsp" />
contactor.js
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
//當鼠標放在客戶名文本框時,給出一個提示文本 function namefocus() { var nameid = document.getelementbyid( "nameid" ); nameid.innerhtml = "請輸入客戶真實姓名" ; } //當鼠標離開客戶名文本框時,給出一個文本提示 function nameblur() { var name = document.getelementbyid( "name" ); var nameid = document.getelementbyid( "nameid" ); var reg= /^[\u4e00-\u9fa5]+$/; if (name.value == "" ){ nameid.innerhtml = "<font color='red'>聯系人名不能為空</font>" ; return false ; } if (reg.test(name.value) == false ){ nameid.innerhtml = "<font color='red'>聯系人名只能輸入中文簡體漢字</font>" ; return false ; } nameid.innerhtml = "<font color='green'>聯系人名輸入正確</font>" ; return true ; } //當鼠標放在客戶名文本框時,給出一個提示文本 function telfocus() { var telid = document.getelementbyid( "telid" ); telid.innerhtml = "請輸入手機號碼" ; } //當鼠標離開客戶名文本框時,給出一個文本提示 function telblur() { var tel = document.getelementbyid( "tel" ); var telid = document.getelementbyid( "telid" ); var reg= /^( 13 | 15 | 17 | 18 )\d{ 9 }$/; if (tel.value == "" ){ telid.innerhtml = "<font color='red'>手機號碼不能為空</font>" ; return false ; } if (reg.test(tel.value) == false ){ telid.innerhtml = "<font color='red'>手機號碼輸入不正確</font>" ; return false ; } telid.innerhtml = "<font color='green'>手機號碼輸入正確</font>" ; return true ; } //表單提交時驗證輸入內容的有效性 function checkform() { var flagname = nameblur(); var flagtel = telblur(); if (flagname == true && flagtel == true ){ return true ; } else { alert( "失敗,請按提示輸入信息 !" ) return false ; } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package cn.edu.aynu.rjxy.bean; /** * 聯系人表屬性 * @author administrator * */ public class contactor { private int id; private int userid; private string name; private string tel; private string address; private string zipcode; public int getid() { return id; } public void setid( int id) { this .id = id; } public int getuserid() { return userid; } public void setuserid( int userid) { this .userid = userid; } public string getname() { return name; } public void setname(string name) { this .name = name; } public string gettel() { return tel; } public void settel(string tel) { this .tel = tel; } public string getaddress() { return address; } public void setaddress(string address) { this .address = address; } public string getzipcode() { return zipcode; } public void setzipcode(string zipcode) { this .zipcode = zipcode; } } |
1
2
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
28
29
30
31
|
package cn.edu.aynu.rjxy.bean; /** * 用戶表屬性 * @author administrator * */ public class user { private int id; private string username; private string password; public int getid() { return id; } public void setid( int id) { this .id = id; } public string getusername() { return username; } public void setusername(string username) { this .username = username; } public string getpassword() { return password; } public void setpassword(string password) { this .password = password; } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package cn.edu.aynu.rjxy.dao; import java.sql.sqlexception; import java.util.list; import org.apache.commons.dbutils.queryrunner; import org.apache.commons.dbutils.handlers.beanhandler; import org.apache.commons.dbutils.handlers.beanlisthandler; import cn.edu.aynu.rjxy.bean.contactor; import cn.edu.aynu.rjxy.utils.jdbcutils; /** * 對linkman表的增刪改查 * @author administrator * */ public class contactordao { private queryrunner qr = new queryrunner(jdbcutils.getdatasource()); /** * 添加聯系人 * @param c * @throws sqlexception */ public void insert(contactor c, int userid) throws sqlexception{ string sql = "insert into linkman(name,tel,address,zipcode,userid) values(?,?,?,?,?)" ; qr.update(sql, c.getname(),c.gettel(),c.getaddress(),c.getzipcode(),userid); } /** * 刪除聯系人 * @param cid * @throws sqlexception */ public void delete( int id) throws sqlexception{ string sql = "delete from linkman where id=?" ; qr.update(sql, id); } /** * 修改聯系人 * @param c * @throws sqlexception */ public void update(contactor c, int userid) throws sqlexception{ string sql = "update linkman set name=?,tel=?,address=?,zipcode=?,userid=? where id=?" ; qr.update(sql, c.getname(),c.gettel(),c.getaddress(),c.getzipcode(),userid,c.getid()); } /** * 根據userid查詢聯系人 * @throws sqlexception * */ public list<contactor> findall( int userid) throws sqlexception{ string sql = "select * from linkman where userid = ?" ; list<contactor> list = qr.query(sql, new beanlisthandler<contactor>(contactor. class ),userid); return list; } /** * 根據id查詢某個聯系人 * @throws sqlexception * */ public contactor findbycid( int id) throws sqlexception{ string sql = "select * from linkman where id=?" ; contactor c = qr.query(sql, new beanhandler<contactor>(contactor. class ), id); return c; } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
|
package cn.edu.aynu.rjxy.dao; import java.sql.sqlexception; import org.apache.commons.dbutils.queryrunner; import org.apache.commons.dbutils.handlers.beanhandler; import cn.edu.aynu.rjxy.bean.user; import cn.edu.aynu.rjxy.utils.jdbcutils; /** * 對user表的查詢 * @author administrator * */ public class userdao { private queryrunner qr = new queryrunner(jdbcutils.getdatasource()); /** * 根據用戶名和用戶密碼查詢 * @param cid * @return * @throws sqlexception */ public user findbycid(string username,string password) throws sqlexception{ string sql = "select * from user where username=?&&password=?" ; user u = qr.query(sql, new beanhandler<user>(user. class ), username,password); return u; } /** * 注冊 * @param c * @throws sqlexception */ public void register(user u) throws sqlexception{ string sql = "insert into user(username,password) values(?,?)" ; qr.update(sql, u.getusername(),u.getpassword()); } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package cn.edu.aynu.rjxy.service; import java.sql.sqlexception; import java.util.list; import cn.edu.aynu.rjxy.bean.contactor; import cn.edu.aynu.rjxy.dao.contactordao; /** * 聯系人功能處理 * @author administrator * */ public class contactorservice { //創建contactordao對象 contactordao dao = new contactordao(); /** * 添加聯系人 * @param c */ public void add(contactor c, int userid){ try { dao.insert(c,userid); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 刪除聯系人 * @param cid */ public void delete( int id){ try { dao.delete(id); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 加載(查詢)聯系人 * @param cid */ public list<contactor> load( int userid){ try { return dao.findall(userid); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 加載(查詢)聯系人(為修改做準備) * @param cid */ public contactor loadbyid( int id){ try { return dao.findbycid(id); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 編輯(修改)聯系人信息 * @param c */ public void edit(contactor c, int userid){ try { dao.update(c,userid); } catch (sqlexception e) { throw new runtimeexception(e); } } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
|
package cn.edu.aynu.rjxy.service; import java.sql.sqlexception; import cn.edu.aynu.rjxy.bean.user; import cn.edu.aynu.rjxy.dao.userdao; /** * * @author administrator * */ public class userservice { //創建contactordao對象 userdao dao = new userdao(); /** * 根據用戶名和密碼查詢用戶 * @param cid */ public user login(string username,string password){ try { return dao.findbycid(username,password); } catch (sqlexception e) { throw new runtimeexception(e); } } /** * 注冊 * @param c */ public void register(user u){ try { dao.register(u); } catch (sqlexception e) { throw new runtimeexception(e); } } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package cn.edu.aynu.rjxy.servlet; import java.io.ioexception; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import cn.edu.aynu.rjxy.bean.contactor; import cn.edu.aynu.rjxy.bean.user; import cn.edu.aynu.rjxy.service.contactorservice; import cn.edu.aynu.rjxy.service.userservice; import cn.edu.aynu.rjxy.utils.commonsutils; /** * 處理聯系人請求 * @author administrator * */ public class contactorservlet extends httpservlet { private contactorservice cs = new contactorservice(); private userservice us = new userservice(); private int userid; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this .dopost(request, response); } /** * 通過獲取表單中的隱藏字段method或者超鏈接中的method參數的值來區分調用執行那個方法來處理這個請求 */ public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //處理中文亂碼 request.setcharacterencoding( "utf-8" ); response.setcontenttype( "text/html;charset=utf-8" ); //獲取隱藏字段method的值,并把它轉換為int型 int method = integer.parseint(request.getparameter( "method" )); switch (method){ case 1 : this .register(request, response); break ; case 2 : this .login(request, response); break ; case 3 : this .query(request, response); break ; case 4 : this .add(request, response); break ; case 5 : this .loadforupdate(request, response); break ; case 6 : this .edit(request, response); break ; case 7 : this .delete(request, response); break ; } } /** * 處理注冊的請求 * @param request * @param response * @throws servletexception * @throws ioexception */ public void register(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //使用commonutils工具類將請求數據封裝到bean中 user u = commonsutils.tobean(request.getparametermap(), user. class ); //執行添加客戶業務 us.register(u); //輸出添加成功提示 response.getwriter().print( "注冊成功,三秒鐘自動跳轉到登錄界面" ); response.setheader( "refresh" , "3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp" ); } /** * 處理登錄的請求 method=2 * @param request * @param response * @throws servletexception * @throws ioexception */ public void login(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //從請求中解析用戶名和密碼 string username = request.getparameter( "username" ); string password = request.getparameter( "password" ); //執行加載業務 user u = us.login(username, password); if (u != null ) { httpsession session = request.getsession(); session.setattribute( "userid" , u.getid()); response.getwriter().print( "登錄成功" +u.getid()); //通過請求轉發將pb帶到list.jsp上去 request.getrequestdispatcher( "/jsp/list.jsp" ).forward(request, response); } else { response.getwriter().print( "登錄失敗,即將跳轉到登錄頁面,請重新登錄......" ); response.setheader( "refresh" , "3;url=http://127.0.0.1:8099/addresslist/jsp/top.jsp" ); } } /** * 處理查看聯系人的請求 method=3 * @param request * @param response * @throws servletexception * @throws ioexception */ public void query(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); userid = (integer) session.getattribute( "userid" ); //執行查詢業務 list<contactor> list = cs.load(userid); //將list存放到request域中 request.setattribute( "contactorlist" , list); system.out.println(list.size()); //通過請求轉發將查詢結果帶到list.jsp頁面上顯示 request.getrequestdispatcher( "/jsp/list.jsp" ).forward(request, response); } /** * 處理添加客戶的請求 * @param request * @param response * @throws servletexception * @throws ioexception */ public void add(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); userid = (integer) session.getattribute( "userid" ); //使用commonutils工具類將請求數據封裝到bean中 contactor c = commonsutils.tobean(request.getparametermap(), contactor. class ); //執行添加客戶業務 cs.add(c,userid); //輸出添加成功提示 response.getwriter().print( "添加成功,三秒后跳轉到功能界面......" ); response.setheader( "refresh" , "3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp" ); } /** * 處理加載某一個客戶的請求 * @param request * @param response * @throws servletexception * @throws ioexception */ public void loadforupdate(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //從請求中獲取客戶的cid string ids = request.getparameter( "id" ); int id = integer.valueof(ids); //執行加載業務 contactor c = cs.loadbyid(id); //將customer對象c保存在request域中 request.setattribute( "contactor" , c); //通過請求轉發將customer對象顯示在list.jsp頁面上 request.getrequestdispatcher( "/jsp/edit.jsp" ).forward(request, response); } /** * 處理更改某一聯系人的請求 * @param request * @param response * @throws servletexception * @throws ioexception */ public void edit(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); userid = (integer) session.getattribute( "userid" ); //使用commonutils工具類將請求數據封裝到bean中 contactor c = commonsutils.tobean(request.getparametermap(), contactor. class ); //執行更改業務 cs.edit(c,userid); //給客戶端發送更改成功提示 response.getwriter().print( "更改成功,三秒后跳轉到功能界面......" ); response.setheader( "refresh" , "3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp" ); } /** * 處理刪除某一聯系人的請求 * @param request * @param response * @throws servletexception * @throws ioexception */ public void delete(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //從請求中獲取cid string cid = request.getparameter( "id" ); int id = integer.valueof(cid); //執行刪除客戶業務 cs.delete(id); //給客戶端發送刪除成功提示 response.getwriter().print( "刪除成功,三秒后跳轉到功能界面......" ); response.setheader( "refresh" , "3;url=http://127.0.0.1:8099/addresslist/jsp/list.jsp" ); } } |
1
2
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
28
29
30
31
32
33
34
35
36
37
38
|
package cn.edu.aynu.rjxy.utils; import java.util.map; import java.util.uuid; import org.apache.commons.beanutils.beanutils; import org.apache.commons.beanutils.convertutils; /** * 提供uuid,實現表單數據封裝到bean中 * @author administrator * */ public class commonsutils { /** * 返回一個uuid * @return */ public static string uuid(){ return uuid.randomuuid().tostring().replace( "-" , "" ).touppercase(); } /** * 把表單數據封裝到bean中 */ public static <t> t tobean(map data, class <t> clazz){ try { t bean = clazz.newinstance(); beanutils.populate(bean, data); return bean; } catch (exception e) { throw new runtimeexception(e); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package cn.edu.aynu.rjxy.utils; import java.sql.connection; import java.sql.sqlexception; import javax.sql.datasource; import com.mchange.v2.c3p0.combopooleddatasource; /** * 創建數據庫連接池 * @author administrator * */ public class jdbcutils { //讀取的是c3p0-config默認配置創建數據庫連接池對象 private static datasource ds = new combopooleddatasource(); //獲取數據庫連接池對象 public static datasource getdatasource(){ return ds; } //從池中獲取連接 public static connection getconnection() throws sqlexception{ return ds.getconnection(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "utf-8" ?> <c3p0-config> <!-- 默認配置,當使用combopooleddatasource無參構造器時,使用的就是這個配置 --> < default -config> <!-- 基本配置 --> <property name= "jdbcurl" >jdbc:mysql: //localhost:3306/linkman?characterencoding=utf-8</property> <property name= "driverclass" >com.mysql.jdbc.driver</property> <property name= "user" >root</property> <property name= "password" > 123456 </property> <!-- 每次增量,當需要創建connection對象時,一次創建幾個 --> <property name= "acquireincrement" > 3 </property> <!-- 當創建池對象后,池中應該有幾個connection對象 --> <property name= "initialpoolsize" > 10 </property> <!-- 池中最少connection個數,如果少于這個值,就會創建connection --> <property name= "minpoolsize" > 2 </property> <!-- 池中最大連接個數 --> <property name= "maxpoolsize" > 10 </property> </ default -config> </c3p0-config> |
該工程需要的jar包:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。