Lập trình mạng – Chương
6
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
6.5 Lập trình web với ASP
Lập trình mạng – Chương
6
2
6.1 Giới thiệu Servlet/JSP
•
Servlet là một ứng dụng (class) Java chạy
trên nền web server.
•
Cơ chế hoạt động theo mô hình CGI mở
rộng.
•
Chương trình phải được dịch ra ở 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ó
method main.
Lập trình mạng – Chương
6
3
//dùng đối tượng “request” để đọc dữ liệu từ client
//đối tượng “response” để xuất dữ liệu cho client
PrintWriter out = response.getWriter();
//dùng đối tượng out để ghi (method print) dữ liệu cho
client
}
}
Lập trình mạng – Chương
6
5
6.1 Giới thiệu Servlet/JSP
•
Biên dịch như một class Java.
•
File *.class dịch được phải đặt vào đúng thư
mục quy định sẵn của web server.
–
Tomcat: $/webpages/WEB-INF/classes
–
JWS: $/servlets
•
Cấu hình cho web server đối với mỗi servlet:
–
Tomcat: hiệu chỉnh file web.xml trong thư mục
$/webpages/WEB-INF theo DTD
/>–
JWS: Cấu hình bằng web-based tool được cung cấp.
Lập trình mạng – Chương
6
6
•
getParameterValues(“para-name”)
String username= request.getParameter(“username”);
String[] choice =
request.getParameterValues(“comments”);
–
Dùng đối tượng của class HttpServletRequest để lấy
các thông tin HTTP header
Lập trình mạng – Chương
6
8
6.2 Lập trình web với Servlet
•
Ví dụ lấy tất cả các 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]+ “-”);
}
}
}
Lập trình mạng – Chương
6
•
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”)
•
…
Lập trình mạng – Chương
6
11
6.2 Lập trình web với Servlet
•
Gởi dữ liệu cho web client: dùng đối tượng của
class HttpServletResponse:
–
Tạo đối tượng PrintWriter để ghi dữ liệu gởi
•
•
Lưu trữ username, password
•
Thông tin để customize web site cho user hay dùng
cho cơ chế personalization
•
…
Lập trình mạng – Chương
6
13
6.2 Lập trình web với Servlet
•
Ghi thông tin Cookie lên máy client:
–
Thông tin được truyền đi trong field Set-
Cookie HTTP header
–
Dùng method addCookie(Cookie cookie) của
class HttpServletResponse.
–
Các thuộc tính quan trọng trong class Cookie:
•
Cookie name: setName(String name) – getName()
•
Cookie value: setValue(String value) – getValue()
•
Max Age: setMaxAge(int seconds) – getMaxAge()
Lập trình mạng – Chương
6
14
method getSession() của class HttpServletRequest.
–
Các method thường sử dụng:
•
Object getValue(String name) [2.2: getAttribute]
•
void putValue(String name,Object object) [2.2: putAttribute]
•
void removeValue(String name) [2.2: removeAttribute]
•
String[] getValueNames() [Enumeration getAttributeNames()]
•
String getId()
•
void setMaxInactiveInterval(int seconds)
Lập trình mạng – Chương
6
16
6.2 Lập trình web với Servlet
•
Ví dụ lưu ShoppingCart vào session
HttpSession session = request.getSession(true);
ShoppingCart cart =
(ShoppingCart)session.getValue(“ShoppingCart”);
if(cart==null){
cart = new ShoppingCart();
session.putValue(“ShoppingCart”,cart);
}
//process(cart)
Lập trình mạng – Chương
Connection con = null; Statement stmt = null; ResultSet rs = null;
String driver = “sun.jdbc.odbc.JdbcOdbcDriver”;
String databaseURL = “jdbc:odbc:DataSourceName”;
try{
Class.forName(driver);
con = DriverManager.getConnection(databaseURL);
stmt = con.createStatement()
rs = stmt.executeQuery(strSQL);
while(rs.next()){
out.println(rs.getString[1]+”-” rs.getInt(“quantity”));//…
}
con.close();
}cacth(SQLException se){ con.close(); }
Lập trình mạng – Chương
6
19
6.2 Lập trình web với Servlet
•
Có thể kết nối database server bất kỳ có driver hỗ trợ.
–
Kết nối đến Oracle Database Server:
driver=“oracle.jdbc.driver.OracleDriver”
databaseURL =“jdbc:oracle:thin@localhost:1521:”+dbName
con = DriverManager.getConnection(databaseURL,user,password)
–
Kết nối đến Sysbase:
driver=“com.sysbase.jdbc.SysDriver”
databaseURL = “jdbc:sysbase:Tds:localhost:1521?
SERVICENAME=“+dbName
con = DriverManager.getConnection(databaseURL,user,password)