Cookie
- Lấy thông tin cookie từ yêu cầu
- Máy chủ gửi cookie về cho client
Cookie[] cookies = request.getCookies(); // Lấy cookie
cookie.getName(); // Lấy tên của cookie
cookie.getValue(); // Lấy giá trị của cookie
Cookie cookie = new Cookie("lastVisit", System.currentTimeMillis() + ""); // Tạo một cookie mới
cookie.setMaxAge(24 * 60 * 60); // Đặt thời gian sống cho cookie
response.addCookie(cookie); // Gửi cookie về client
Cookie thường được lưu tại thư mục AppData trên máy tính người dùng.
Mã hóa và giải mã
Cookie cookie = new Cookie("data", URLEncoder.encode("Xia Xia Xia", "utf-8")); // Mã hóa
URLDecoder.decode(cookie.getValue(), "utf-8"); // Giải mã
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
Cookie[] cookies = request.getCookies();
PrintWriter out = response.getWriter();
if (cookies != null) {
out.write("Thời gian truy cập lần trước của bạn là: ");
for (int i = 0; i < cookies.length; i++) {
Cookie currentCookie = cookies[i];
if ("lastVisit".equals(currentCookie.getName())) {
String value = currentCookie.getValue();
long timestamp = Long.parseLong(value);
Date date = new Date(timestamp);
// out.write(date.toLocaleString());
// URLDecoder.decode(currentCookie.getValue(), "utf-8");
}
}
} else {
out.write("Đây là lần đầu tiên bạn truy cập!");
}
Cookie cookie = new Cookie("lastVisit", System.currentTimeMillis() + "");
// Cookie cookie = new Cookie("data", URLEncoder.encode("Xia Xia Xia", "utf-8"));
cookie.setMaxAge(24 * 60 * 60); // Đặt thời gian sống cho cookie
response.addCookie(cookie);
Session
- Máy chủ tạo ra một đối tượng session cho mỗi người dùng (trình duyệt)
- Mỗi session chỉ tồn tại trong một trình duyệt cụ thể, miễn là trình duyệt chưa đóng thì session sẽ vẫn hoạt động
- Sau khi người dùng đăng nhập, toàn bộ website có thể truy cập!
// Thiết lập mã hóa
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// Lấy session
HttpSession session = request.getSession();
// Lấy ID của session
String sessionId = session.getId();
System.out.println(sessionId);
// Kiểm tra xem session có phải mới tạo không
if (session.isNew()) {
response.getWriter().write("Tạo session thành công; SessionId:" + sessionId);
} else {
response.getWriter().write("Session đã tồn tại; SessionId:" + sessionId);
}
// Khi session được tạo, điều gì xảy ra?
Cookie cookie = new Cookie("JSESSIONID", sessionId);
response.addCookie(cookie);
Cookie và Session khác nhau như thế nào?
- Khu vực lưu trữ khác nhau: Cookie được lưu ở phía client (trình duyệt), còn Session được lưu ở phía server.
- Cách lưu trữ dữ liệu khác nhau: Cookie chỉ có thể lưu dữ liệu dạng ASCII, trong khi Session có thể lưu bất kỳ kiểu dữ liệu nào. Thông thường chúng ta lưu các biến thường dùng như UserId trong Session.
- Thời gian sống khác nhau: Cookie có thể được cấu hình để duy trì lâu dài, ví dụ như chức năng đăng nhập tự động. Session thường có thời gian sống ngắn hơn, khi trình duyệt bị đóng hoặc hết hạn sẽ bị hủy.