คุกกี้ Java: วิธีจัดการเซสชัน Java Servlet โดยใช้คุกกี้
เผยแพร่แล้ว: 2020-08-19
หน้าเว็บไม่มีความทรงจำ ผู้ใช้ที่เปลี่ยนจากหน้าหนึ่งไปยังอีกหน้าหนึ่งจะได้รับการปฏิบัติโดยเว็บไซต์ในฐานะผู้เยี่ยมชมใหม่ทั้งหมด คุกกี้ของเซสชันช่วยให้เว็บไซต์ที่คุณกำลังเยี่ยมชมสามารถติดตามการเคลื่อนไหวของคุณจากหน้าหนึ่งไปอีกหน้า ดังนั้นคุณจะไม่ถูกถามถึงข้อมูลเดียวกันกับที่คุณให้ไว้กับเว็บไซต์
คุกกี้ช่วยให้คุณดำเนินการผ่านหน้าต่างๆ ของเว็บไซต์ได้อย่างรวดเร็วและง่ายดาย โดยไม่ต้องตรวจสอบสิทธิ์หรือประมวลผลพื้นที่ใหม่แต่ละส่วนที่คุณเยี่ยมชมใหม่
แต่บางครั้งในเว็บแอปพลิเคชัน Java เราควรรู้ว่าใครคือลูกค้าและดำเนินการตามคำขอตามลำดับ
ตัวอย่างเช่น แอปพลิเคชันตะกร้าสินค้าควรรู้ว่าใครเป็นผู้ส่งคำขอเพื่อเพิ่มสินค้า และต้องเพิ่มสินค้าในตะกร้าสินค้าใด หรือใครเป็นผู้ส่งคำขอชำระเงิน เพื่อให้สามารถเรียกเก็บเงินตามจำนวนเพื่อแก้ไขลูกค้าได้
เซสชันคือสถานะการสนทนาระหว่างไคลเอ็นต์และเซิร์ฟเวอร์ และอาจประกอบด้วยคำขอและการตอบสนองหลายรายการระหว่างไคลเอ็นต์และเซิร์ฟเวอร์ เนื่องจาก HTTP และ Web Server ทั้งคู่ไม่มีสถานะ วิธีเดียวที่จะรักษาเซสชันคือเมื่อข้อมูลเฉพาะบางอย่างเกี่ยวกับเซสชัน (id เซสชัน) ถูกส่งผ่านระหว่างเซิร์ฟเวอร์และไคลเอ็นต์ในทุกคำขอและการตอบกลับ

ตอนนี้ มาสร้าง Simple Dynamic Web Project ใน Eclipse ซึ่งจะอธิบายการจัดการเซสชัน Java Servlet โดยใช้คุกกี้
นี่คือขั้นตอน:
- สร้างโครงการเว็บแบบไดนามิก:
CrunchifySessionManagementByCookie
-
crunchify-login.html
: สร้างหน้าต้อนรับของแอปพลิเคชัน -
CrunchifyLoginServlet.java
– ดูแลคำขอเข้าสู่ระบบ -
CrunchifyLogoutServlet.java
– ดูแลคำขอออกจากระบบ -
web.xml
– ไฟล์ Deployment Descriptor (ไม่เห็น web.xml ทำตามบทช่วยสอนนี้) -
CrunchifyLoginSuccessful.jsp
– คำขอความสำเร็จจะถูกส่งต่อไปยังLoginSuccess.jsp
คุกกี้นี้จะถูกใช้ที่นั่นเพื่อติดตามเซสชัน นอกจากนี้ ให้สังเกตว่าระยะหมดเวลาของคุกกี้ถูกตั้งค่าเป็น 60 นาที

ต้องอ่านอีก:
- ตัวอย่าง/บทช่วยสอน Spring MVC: สวัสดีชาวโลก – Spring MVC 3.2.1
- วิธีอัปเดตกราฟเส้นแบบประกายไฟทุก 3 วินาทีใน Spring MVC (อัปเดตตามเวลาจริง)
1. crunchify-login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
< ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http - equiv = "Content-Type" content = "text/html; charset=US-ASCII" > < title > Crunchify Login Form - Session Management by Cookies < / title > <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/bg.png' ) ; } </style> < / head > < body > < div align = "center" > < br > < br > < form action = "CrunchifyLoginServlet" method = "post" > Enter Your Username : < input type = "text" name = "crunchifyUser" > < br > Enter Your Password : < input type = "password" name = "crunchifyPassword" > < br > < br > < br > < input type = "submit" value = "Login" > < / form > < / div > < / body > < / html > |
2. CrunchifyLoginServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com . crunchify . tutorials ; import java . io . IOException ; import java . io . PrintWriter ; import javax . servlet . RequestDispatcher ; import javax . servlet . ServletException ; import javax . servlet . annotation . WebServlet ; import javax . servlet . http . Cookie ; import javax . servlet . http . HttpServlet ; import javax . servlet . http . HttpServletRequest ; import javax . servlet . http . HttpServletResponse ; /* * Author: Crunchify.com * */ /** * Servlet implementation class LoginServlet */ @WebServlet ( "/CrunchifyLoginServlet" ) public class CrunchifyLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L ; private final String userID = "CrunchifyUser" ; private final String password = "CrunchifyPassword" ; protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { // get request parameters for userID and password String crunchifyUser = request . getParameter ( "crunchifyUser" ) ; String pwd = request . getParameter ( "crunchifyPassword" ) ; if ( userID . equals ( crunchifyUser ) && password.equals(pwd)) { Cookie crunchifyCookie = new Cookie("crunchifyUser", crunchifyUser); // setting cookie to expiry in 60 mins crunchifyCookie . setMaxAge ( 60 * 60 ) ; response . addCookie ( crunchifyCookie ) ; response . sendRedirect ( "CrunchifyLoginSuccess.jsp" ) ; } else { RequestDispatcher rd = getServletContext ( ) . getRequestDispatcher ( "/crunchify-login.html" ) ; PrintWriter out = response . getWriter ( ) ; out . println ( "<font color=red>Please make sure you enter UserID/Pass as \"CrunchifyUser : CrunchifyPassword\".</font>\n" ) ; rd . include ( request , response ) ; } } } |
3. CrunchifyLogoutServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com . crunchify . tutorials ; import java . io . IOException ; import javax . servlet . ServletException ; import javax . servlet . annotation . WebServlet ; import javax . servlet . http . Cookie ; import javax . servlet . http . HttpServlet ; import javax . servlet . http . HttpServletRequest ; import javax . servlet . http . HttpServletResponse ; /* * Author: Crunchify.com * */ /** * Servlet implementation class LogoutServlet */ @WebServlet ( "/CrunchifyLogoutServlet" ) public class CrunchifyLogoutServlet extends HttpServlet { private static final long serialVersionUID = 1L ; protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { response . setContentType ( "text/html" ) ; Cookie loginCookie = null ; Cookie [ ] cookies = request . getCookies ( ) ; if ( cookies ! = null ) { for ( Cookie cookie : cookies ) { if ( cookie . getName ( ) . equals ( "crunchifyUser" ) ) { loginCookie = cookie ; break ; } } } if ( loginCookie ! = null ) { loginCookie . setMaxAge ( 0 ) ; response . addCookie ( loginCookie ) ; } response . sendRedirect ( "crunchify-login.html" ) ; } } |
4. web.xml
1 2 3 4 5 6 7 8 9 10 |
<? xml version = "1.0" encoding = "UTF-8" ?> < web - app xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi : schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0" > < display - name > CrunchifySessionManagementByCookie < / display - name > < welcome - file - list > < welcome - file > crunchify - login . html < / welcome - file > < / welcome - file - list > < / web - app > |
5. CrunchifyLoginSuccessful.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<% @ page language = "java" contentType = "text/html; charset=US-ASCII" pageEncoding = "US-ASCII" %> < ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http - equiv = "Content-Type" content = "text/html; charset=US-ASCII" > < title > Crunchify - Login Successful - Session management by Cookies < / title > <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/bg.png' ) ; } </style> < / head > < body > < div align = "center" > < br > < br > <% String userName = null ; Cookie [ ] cookies = request . getCookies ( ) ; if ( cookies ! = null ) { for ( Cookie cookie : cookies ) { if ( cookie . getName ( ) . equals ( "crunchifyUser" ) ) userName = cookie . getValue ( ) ; } } if ( userName == null ) response . sendRedirect ( "crunchify-login.html" ) ; %> < h3 > Hi <%= userName %> , Login successful . < / h3 > < br > < form action = "CrunchifyLogoutServlet" method = "post" > < input type = "submit" value = "Logout" > < / form > < / div > < / body > < / html > |
ทีนี้มาลองรันตัวอย่างนี้กัน:
- ปรับใช้โครงการ
CrunchifySessionManagementByCookie
กับ Tomcat และเรียกใช้ Tomcat

- ชี้ URL เบราว์เซอร์ของคุณไปที่ http://localhost:8080/CrunchifySessionManagementByCookie/crunchify-login.html

- หน้าความสำเร็จ

- หน้าเข้าสู่ระบบล้มเหลว

ตอนนี้จะตรวจสอบได้อย่างไรว่าคุกกี้ของคุณตั้งค่าถูกต้องหรือไม่

ลองดาวน์โหลด Cookie Manager Extension
และคุณจะเห็น Cookie
ในเบราว์เซอร์ของคุณดังที่แสดงด้านล่าง

แจ้งให้เราทราบหากคุณประสบปัญหาใด ๆ ที่ทำงานอยู่เหนือรหัสการจัดการคุกกี้ของ Servlet