05. Servlet Form Data
Transcript of 05. Servlet Form Data
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Nội dung trình bày
Form Data
Servlet đọc dữ liệu gửi từ Form Data
Serlvet lọc các ký tự đặc biệt
Xây dựng một số chức năng cho BookOnline với Servlet
– Đăng nhập
– Đăng ký
– Tìm kiếm sách theo tiêu chí sau tên sách, giá từ, giá
đến
– Xem chi tiết thông tin sách
2
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Form Data
Được sử dụng để nhận dữ liệu input từ client và gởi
dữ liệu đến trang web khác như servlet, php,
aspx,cgi,… xử lý.
Thẻ form được sử dụng để chứa các thành phần khác
của form nhưng quan trọng nhất là các thẻ input như
– Text field, Password field
– Drop down list (combo box)
– Radio button, Checkbox
– Submit button
– …
3
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Form Data
Hai cơ chế Request thông dụng nhất từ Client
– HTTP GET và HTTP POST
HTTP GET Request
– Thông tin gửi đính kèm vào cuối URL
– Dữ liệu gửi bị giới hạn
• ../XemSanPham?id=S001&Trang=1
HTTP POST Request
– Thông tin gửi xem như dữ liệu (không đính kèm vào
URL)
– Không giới hạn dung lượng gửi
5
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Form Data – GET/POST
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!—HTTP GET-->
http://localhost/Web1/ServletSample?name1=value1& . . .
<form id="form1" name="form1"
method="get" action="ServletSample">
<!--Các thành thành của form-->
</form>
<!—HTTP POST-->
http://localhost/Web1/ServletSample
<form id="form1" name="form1"
method="post" action="ServletSample">
<!--Các thành thành của form-->
</form>
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Form Data - GET
9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<form id="form1" name="form1" method="get"
action="XuLyTimKiem">
<table width="300" border="1" align="center">
<tr><td colspan="2" align="center"><strong>TÌM
KIẾM</strong></td>tr>
<tr><td width="40%">Tên sách</td>
<td><input name="tbTenSach" type="text"
id="tbTenSach" size="30" /></td></tr>
<tr><td>Giá từ</td>
<td><input name="tbGiaTu" type="text"
id="tbGiaTu" size="30" /></td></tr>
<tr><td>Giá đến</td>
<td><input name="tbGiaDen" type="text"
id="tbGiaDen" size="30" /></td>
</tr><tr><td> </td>
<td><input type="submit" name="btTimKiem"
id="btTimKiem" value="Tìm kiếm" /></td>
</tr></table>
</form>
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Form Data - POST
12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<form id="form1" name="form1" method="post"
action="XuLyTimKiem">
<table width="300" border="1" align="center">
<tr><td colspan="2" align="center"><strong>TÌM
KIẾM</strong></td>tr>
<tr><td width="40%">Tên sách</td>
<td><input name="tbTenSach" type="text"
id="tbTenSach" size="30" /></td></tr>
<tr><td>Giá từ</td>
<td><input name="tbGiaTu" type="text"
id="tbGiaTu" size="30" /></td></tr>
<tr><td>Giá đến</td>
<td><input name="tbGiaDen" type="text"
id="tbGiaDen" size="30" /></td>
</tr><tr><td> </td>
<td><input type="submit" name="btTimKiem"
id="btTimKiem" value="Tìm kiếm" /></td>
</tr></table>
</form>
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet đọc dữ liệu từ Form Data
String str=request.getParameter(“nameInput”)
– Lấy giá trị đầu tiên của nameInput trong query string
– Sử dụng cho cả Form GET và Form POST
– Return null nếu nameInput trong tồn tại trong query string
String []s=request.getParameterValues(“NameInput”)
– Lấy tất cả giá trị của nameInput trong query string
– Sử dụng cho cả Form GET và Form POST
– Return null nếu nameInput trong tồn tại trong query string
Map<String, String[]> m = request.getParameterMap(),
Enumeration<String> e = request.getParameterNames()
– Return Map hoặc Enumeration các tham số request
– Thường sử dụng để debug
13
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet đọc dữ liệu từ Form Data
15
tbTenSach :textField
tbGiaTu :textField
tbGiaDen :textField
btTimKiem :submit button
form1 : Form GET hoặc POST
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet đọc dữ liệu từ Form Data
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {out.println("<html><head>");
out.println("<title>Tìm kiếm sách</title>");
out.println("</head><body>");
String tenSach=request.getParameter("tbTenSach");
out.println("<p>Tên sách:"+tenSach+"</p>");
String giaTu=request.getParameter("tbGiaTu");
out.println("<p>Giá từ:"+giaTu+"</p>");
String giaDen=request.getParameter("tbGiaDen");
out.println("<p>Giá đến:"+giaDen+"</p>");
out.println("</body></html>");
} finally {
out.close();
}
}
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet đọc dữ liệu từ Form Data – GET/POST
17
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet lọc các ký tự đặc biệt
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ServletHelper {
public static String filter(String input) {
StringBuilder filtered =
new StringBuilder(input.length());
char c;
for (int i = 0; i < input.length(); i++) {
c = input.charAt(i);
switch (c) {
case '<': filtered.append("<");break;
case '>': filtered.append(">");break;
case '"': filtered.append(""");break;
case '&': filtered.append("&"); break;
default: filtered.append(c);
}
}
return (filtered.toString());
}
}
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Servlet lọc các ký tự đặc biệt
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html><head>");
out.println("<title>Tìm kiếm sách</title>");
out.println("</head><body>");
String tenSach=request.getParameter("tbTenSach");
out.println("<p>Tên sách:"+ServletHelper.filter(tenSach)+"</p>");
String giaTu=request.getParameter("tbGiaTu");
out.println("<p>Giá từ:"+ServletHelper.filter(giaTu)+"</p>");
String giaDen=request.getParameter("tbGiaDen");
out.println("<p>Giá đến:"+ServletHelper.filter(giaDen)+"</p>");
out.println("</body></html>");
} finally {
out.close();
}
}
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
BookOnline – Các chức năng
Khách hàng có thể chọn chức năng đăng ký từ trang
chủ
Khách hàng có thể chọn chức năng tìm kiếm sách
theo tên sách và khoảng giá. Trên trang tìm kiếm có
thể chọn xem chi tiết sách
Biết rằng thông tin khách hàng bao gồm tên đăng
nhập, mật khẩu, email, địa chỉ, điện thoại
26
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
BookOnline – Sơ đồ use case
27
DangKy
TimKiemSach XemChiTietSach
<<extend>>
KhachHang
XemTrangChu
<<extend>>
<<extend>>
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
BookOnline – Mô hình xây dựng (2 Layer)
29
DATABASE
Data Layer
Presentation Layer
Java Beans
(POJO)
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
BookOnline – Mô hình xây dựng (2 Layer)
30
DATABASE
Data Layer
(JDBC)
Presentation Layer
(Servlet) Java Beans
(POJO)
Nguyễn Hoàng Anh – [email protected] – ĐH KHTN - 2011
Tài liệu tham khảo
Nguyễn Hoàng Anh, Tập bài giảng và video môn
chuyên đề Java, 2010
Marty Hall, Tập bài giảng về Servlet, 2010
– http://courses.coreservlets.com/Course-Materials/csajsp2.html
31