• Lấy dữ liệu từ web client gởi đến bằng servlet: – Dùng đối tượng của class HttpServletRequest. – Các methods để lấy thông số:[r]
(1)Lập trình mạng – Chương
1
CHƯƠNG 6:
LẬP TRÌNH WEB VỚI CÁC CÔNG NGHỆ PHỔ BIẾN
6.1 Giới thiệu Servlet/JSP
6.2 Lập trình web với Servlet 6.3 Lập trình web với JSP
6.4 Giới thiệu ASP
(2)Lập trình mạng – Chương
2
• Servlet ứng dụng (class) Java chạy trên web server.
• Cơ chế hoạt động theo mơ hình CGI mở rộng.
• Chương trình phải dịch dạng byte-code(.class), khai báo với web
server Web server phải hỗ trợ Java.
• Phải extends class HttpServlet Khơng có
(3)Lập trình mạng – Chương
3
• Cần có package servlet.jar để biên dịch (http://java.sun.com/products/servlet/)
• Các server hỗ trợ Servlet:
– Apache Tomcat (http://jakarta.apache.org)
– Sun’s Java Web Server, free, khơng cho download (http://wwws.sun.com/software/jwebserver/)
– New Atlanta’s ServletExec, tích hợp ServletEngine vào web server(http://newatlanta.com)
– http://www.macromedia.com/software/jrun/trial/
– …
• Tham khảo tài liệu Servlet:
(4)Lập trình mạng – Chương
4
• Cấu trúc đơn giản Servlet: import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
public class Sample extends HttpServlet{ public doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException{
//dùng đối tượng “request” để đọc liệu từ client //đối tượng “response” để xuất liệu cho client PrintWriter out = response.getWriter();
//dùng đối tượng out để ghi (method print) liệu cho client
(5)Lập trình mạng – Chương
5
• Biên dịch class Java.
• File *.class dịch phải đặt vào thư mục quy định sẵn web server.
– Tomcat: $/webpages/WEB-INF/classes – JWS: $/servlets
• Cấu hình cho web server servlet: – Tomcat: hiệu chỉnh file web.xml thư mục
$/webpages/WEB-INF theo DTD
http://java.sun.com/j2ee/dtds/web-app_2_2.dtd
(6)Lập trình mạng – Chương
6
• Cơ chế hoạt động servlet:
– Web server nhận yêu cầu triệu gọi servlet từ client.
• Nếu servlet chạy lần đầu, web server load file
servlet tương ứng, khởi tạo thơng số qua method init()
• Nếu servlet khởi tạo, tạo thread để xử lý yêu cầu
– Gọi methods doXxx() để xử lý request tương ứng theo giao thức HTTP.
(7)Lập trình mạng – Chương
7
• Lấy liệu từ web client gởi đến servlet: – Dùng đối tượng class HttpServletRequest
– Các methods để lấy thông số:
• getParameter(“para-name”)
• getParameterValues(“para-name”)
String username= request.getParameter(“username”); String[] choice =
request.getParameterValues(“comments”);
(8)Lập trình mạng – Chương
8
• Ví dụ lấy tất thơng số từ client
Enumeration parameter_names = request.getParameterNames();
while(parameter_names.hasMoreElements()){
String para = parameter_names.nextElement(); out.print(para + “ = ”);
String[] paraValues = getParameterValues(para); if(paraValues.lenght()==1){
out.println(paraValues[0]); }else{
for(int i = 0, i< paraValues.lenght(),i++){ out.print(paraValues[i]+ “-”);
} }
(9)Lập trình mạng – Chương
9
• Lấy thơng số HTTP request header: class
HttpServletRequest cung cấp method để lấy các thông số request header.
– String getHeader(header-name): lấy nội dung
header-name
– Enumeration getHeaderNames(): lấy tất
header-name
– Một số method điển hình:
• Cookie[] getCookies(): dãy Cookie từ client
• int getContentLength(): trả giá trị Content-Length • int getContentType(): trả giá trị Content-Type
(10)Lập trình mạng – Chương
10
• Lấy thơng số HTTP request header: – Lấy giá trị biến môi trường CGI:
• QUERY_STRING: getQueryString()
• REMOTE_ADDR: getRemoteAddr()
• REMOTE_HOST: getRemoteHost()
• REQUEST_METHOD: getMethod()
• PATH_INFO: getPathInfo()
• SCRIPT_NAME: getServletPath()
• SERVER_NAME: getServerName()
• SERVER_PORT: getServerPort()
• HTTP_XXX_YYY: getHeader(“Xxx-Yyy”)