bài 3: servlet&cookie&session - lập trình mạng nâng cao
DESCRIPTION
Lập Trình Mạng Nâng Cao - Servlet, Cookie, SessionTRANSCRIPT
Quản lý phiên của ứng dụng
ThS Văn Thiên Hoàng
Mục đích
Trình bày cách xử lý thông tin trong một phiên giao tiếp trong servlet và giữa các trang web.
Thông tin cần duy trì trong phiên
Server cần lưu hai loại thông tin trong một phiên giao tiếp. Thông tin về phiên giao tiếpThông tin của người sử dụng
Http thì không duy trì trạng thái.
Giải pháp
Gửi qua lại client – serverSử dụng các thành phần ẩn của form
Lưu trữ thông tin phía client.Sử dụng Cookie
Lưu trữ thông tin phiên phía serverSession
Thần phần ẩn của form
<input type="hidden" name="sessionID" value="...">
Lấy giá trị biến ẩn trong form và gởi trả lại để duy trì thông tin phiên trong trang.
• String sessionID = getParameter("sessionID");• out.println("<input
type=\"hidden\" name=\"sessionID\" value=\" + sessionID + "\">");
Cookie là gì?
Cookie là một chuỗi văn bản dạng name=value.
Cookie được lưu trên máy client.URL được lưu trong mỗi cookie để
browse xác định địa chỉ server để gởi trả cookie về phía server.
Thao tác với Cookies
Java Servlet API cung cấp bộ công cụ để thao tác với cookie.
Lớp javax.servlet.http.Cookie biểu diễn cookie. Phương thức lấy giá trị
• getName(), getValue(), getPath(), getDomain(), getMaxAge(), getSecure()…
Phương thức thiết lập• setValue(), setPath(), setDomain(), setMaxAge()…
Lấy tất cả các cookie từ request
• Cookie[] HttpServletRequest.getCookies()
Gởi cookie về client
• HttpServletResponse.addCookie(Cookie cookie)
Các bước tạo cookie
Ba bước để tạo cookie1) Tạo đối tượng cookie
Cookie cookie = new Cookie (name, value);
2) Thiết lập thuộc tính cookie Cookie.setMaxAge (60);
3) gởi cookie về client Response.addCookie (cookie)
Gởi cookie về phía client
Tạo đối tượng CookieCookie c = new Cookie("userID", "a1234");
Thiết lập thời gian sống của cookie trên đĩa cứng của client (đơn vị giây). c.setMaxAge(60*60*24*7); // một tuần
Đặt cookie vào đối tượng HTTP response để gởi về client.response.addCookie(c);
Ví dụ:
<html>
<head><title>Insert your Name</title></head>
<body> <h1>What is your name?</h1>
<form action="welcomeback" method="get">
<p>
<input type="text" name="username" />
<input type="submit" />
</p>
</form>
</body>
</html>
getname.html
Ví dụ
public class WelcomeBack extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String user = req.getParameter("username");
if (user == null) { // Find the "username" cookie
Cookie[] cookies = req.getCookies();
for (int i = 0; cookies != null && i < cookies.length; ++i) {
if (cookies[i].getName().equals("username"))
user = cookies[i].getValue();
}
} else res.addCookie(new Cookie("username", user));
WelcomeBack.java
Ví dụ (tiếp theo)
if (user == null) // No parameter and no cookie
res.sendRedirect("getname.html");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><h1>Welcome Back " + user
+ "</h1></body></html>");
}
} WelcomeBack.java
Session là gì?
Giải pháp để duy trì dữ liệu gởi qua nhiều trang ở Server.
Để lưu thông tin, Session tạo ra cơ chế để quản lý cookie cho từng phiên với một id để nhận dạng hoặc sử dụng URL.
Session Cookies
Web browser 1
Web server
request request
ServletServlet
id1
response
put cookie id1
response
Create Session
id1
Session Cookies
Web browser 2
Web server
request request
ServletServlet
id1
response
put cookie id2
response
Create Session
id2id2
Session Cookies
Web server
request
ServletServlet
id1
response response
request
Cookie: id1
id2
Session read/write
Web browser 1
id1
Session Cookies
Web server
request
ServletServlet
id1
response response
request
Cookie: id2
id2
Session read/write
Web browser 2
id2
sessionId list
Truy cập dữ liệu Session
Đối tượng session là thể hiện của lớp HttpSession.
Sử dụng phương thức getSesssion() hoặc getSession(true) để lấy đối tượng HttpSession
hiện tại hoặc tạo ra nó nếu không có.
Sử dụng getSession(false) nếu không muốn tạo ra session mới khi session không tồn tại.
Các phương thức của HttpSession
Dữ liệu Session được truy cập bằng bảng băm. - setAttribute(String name,Object value)
- Object getAttribute(String name)
Các phương thức khác:- removeAttribute, getAttributeNames
- isNew, invalidate, getId
- getCreationTime, getLastAccessedTime
- getMaxInactiveInterval, setMaxInactiveInterval
Ví dụ: Shopping Cart cơ bản
Ví dụ về một shopping cart trực tuyến cơ bản gồm Servlet- Store.java: trang chính - ShoppingCart.java: xử lý sự kiện
Ví dụ: Online-Store
public class Store extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head>"
+ "<link rel=\"stylesheet\" type=\"text/css\""
+ " href=\"cartstyle.css\"/></head><body>");
HttpSession session = req.getSession();
if (session.getAttribute("item-list") == null) {
out.println("<h1>Hello new visitor!</h1>");
session.setAttribute("item-list", new LinkedList());
}
List itemList = (List) session.getAttribute("item-list");
Store.java
Ví dụ: Online-Store out.println("<h2>Your Shopping Cart:</h2><ol>");
for (Iterator it = itemList.iterator(); it.hasNext();)
out.println("<li>" + it.next() + "</li>");
out.println("</ol>");
out.println("<form method=\"post\" action=\"cart\">");
out.println("<p>Add item:<input name=\"item\" type=\"text\"/>"
+ "<input type=\"submit\" value=\"send\"/></p>"
+ "<p><input type=\"submit\" value=\"empty cart\" "
+ "name=\"clear\"/></p></form>");
out.println("</body></html>");
}
} Store.java
public class ShoppingCart extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
List items = (List) req.getSession().getAttribute("item-list");
out.println("<html><head><link rel=\"stylesheet\""
+ " type=\"text/css\" href=\"cartstyle.css\"/>"
+ "</head><body>");
ShoppingCart.java
Ví dụ: Online-Store
if (req.getParameter("clear") != null) {
items.clear();
out.println("<h2>Your Shopping Cart is Empty!</h2>");
} else {
String item = req.getParameter("item");
items.add(item);
out.println("<h2>The item <i>" + item +
"</i> was added to your cart.</h2>");
}
out.println("<h2><a href=\"store\">Return to the store</a></h2>");
out.println("</body></html>");
}} ShoppingCart.java
Ví dụ: Online-Store
Viết vào URL
Web browser
Web server
request request
ServletServlet
id1
response response
Create Session
<HTML…>
< A HREF=“servletURL;sessID=id1>”
/<…HTML>
URL Rewriting
Web server
request
ServletServlet
id1
response response
request
(no cookie)
id2
Session read/write
Web browser 1
GET servletURL;sessID=id1 HTTP/1.0
<HTML…>
<A HREF=“servletURL;sessID=id1>”
/<…HTML>
Viết lại Servlet URL
Sử dụng các phương thức để viết vào URL- String encodeURL(String url)
• Sử dụng cho HTML hyperlink
- String encodeRedirectURL(String url)• Sử dụng cho HTTP redirection
Các phương thức này xác định session ID cần được mã hóa trong URL.
Nếu truy vấn có cookie thì url được gởi về bình thường.
Chương trình Store
Giả sử chương trình store chạy trên client không hỗ trợ cookie.
Để giải quyết, việc viết vào URL nên được sử dụng. Store.java
"<form method=\"post\" action=\"" + res.encodeURL("cart") + "\">“
ShoppingCart.java
“<a href=\"" + res.encodeURL("store") + "\">"
Session Listener
session listener được sử dụng để phản ứng các sự kiện liên quan đến đến session. session mới được tạo. Một session bị hủy.
Để sử dụng được session listener, cài đặt giao diện javax.servlet.http.HttpSessionListener.
Ví dụ: Session-Listener
public class CartInitializer implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
List itemList = new LinkedList();
se.getSession().setAttribute("item-list",itemList);
itemList.add("A Free Apple");
}
public void sessionDestroyed(HttpSessionEvent se) {}
} CartInitializer.java
<listener>
<listener-class>CartInitializer</listener-class>
</listener> web.xml
Forwarding và Redirection
Mặc định, SendRedirect không đảm bảo tham số của request được gới lên từ client.
SendRedirect kết thúc khi một URL khác được nạp ở client.
Forwarding cho phép gởi toàn bộ dữ liệu của request tới trang khác. Để forward sử dụng request Dispatcher.
The Request Dispatcher
Request Dispather
Đối tượng RequestDispatcher được sử dụng để gởi một client request tới một trang bất kỳ (servlet, JSP hoặc HTML) ở server.
Để lấy đối tượng gởi request tới nguồn x, sử dụng:getServletContext().getRequestDispatcher("x")
Các phương thức của đối tượng Request Dispatcher
void forward(ServletRequest request,
ServletResponse response)
Chuyển request từ servlet tới nguồn khác.
void include(ServletRequest request,
ServletResponse response)
Bao gồm cả nội dụng của response.
Chuyển dữ liệu
3 cách để chuyển tham số cho Servlet hoặc JSPDữ liệu được dùng chỉ cho request này:
request.setAttribute("key", value);Dữ liệu được sử dụng cho client (và cả cho các
request về sau)session.setAttribute("key", value);
Dữ liệu sẽ được dùng trong tương lai của mỗi client. context.setAttribute("key", value);
Ví dụ
Servlet JokesAndImages cho phep người sử dụng chọn joke ngẫu nhiên hoặc ảnh ngẫu nhiên.
Server có 5 ảnh trong thư mục images/ và 5 joke (txt file) trong thư mục jokes/
Các yêu cầu rỗng sẽ được gởi tới tập tin HTML nó cho phép người dụng chọn ảnh hoặc joke.
Request với joke được gởi tới trang servlet Jokes
Requests với image được gởi tới một ảnh ngẫu nhiên trong thư mục images/
Joke và Image
<html>
<head><title>Images and Jokes</title></head>
<body>
<h1>Please Select:</h1>
<form method="post" action="JokesAndImages">
<h2>
<input type="submit" name="joke"
value="A Joke" />
<input type="submit" name="image"
value="An Image" />
</h2>
</form>
</body></html> imagesJokesOptions.html
Joke và Imagepublic class JokesAndImages extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int randomNum = 1 + Math.abs((new Random()).nextInt() % 5);
if (req.getParameter("joke") != null) {
req.setAttribute("jokeNumber", new Integer(randomNum));
getServletContext().getRequestDispatcher("/Jokes").forward(req,res);
} else if (req.getParameter("image") != null) {
getServletContext().getRequestDispatcher("/images/image" +
randomNum + ".gif").forward(req, res);
} else getServletContext().getRequestDispatcher
("/imagesJokesOptions.html"). forward(req,res);
}
public void doGet ... }} JokesAndImages.java
Joke và Image
public class Jokes extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><h1>A Joke</h1><pre>");
int jokeNum = ((Integer) req.getAttribute("jokeNumber")).intValue();
getServletContext().getRequestDispatcher
("/jokes/joke" + jokeNum + ".txt").include(req, res);
out.println("\n</pre>");
out.println("<a href=\"" + req.getRequestURL() + "\">Back</a>");
out.println("</body></html>");
}} Jokes.java
Câu hỏi