কুকি পড়তে হলে javax.servlet.http.Cookie অবজেক্টের একটি অ্যারে তৈরি করতে হবে getCookies() মেথড কল করে HttpServletRequest-এ। তারপর অ্যারেটি ট্রাভার্স করে getName() এবং getValue() মেথড ব্যবহার করে প্রতিটি কুকি এবং সংশ্লিষ্ট মান অ্যাক্সেস করুন।
উদাহরণ: পূর্বের উদাহরণে সেট করা কুকিগুলো পড়া
<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<center>
<h1>Reading Cookies</h1>
</center>
<%
Cookie cookie = null;
Cookie[] cookies = null;
// এই ডোমেইন-এর সাথে জড়িত কুকিজের অ্যারে পান
cookies = request.getCookies();
if( cookies != null ) {
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
out.print("Name : " + cookie.getName() + ", ");
out.print("Value: " + cookie.getValue() + " <br/>");
}
} else {
out.println("<h2>No cookies founds</h2>");
}
%>
</body>
</html>
আউটপুট
যদি first_name cookie "John" এবং last_name cookie "Player" হিসেবে সেট করা থাকে, তবে https://localhost:8080/main.jsp রান করলে নিচের রেজাল্ট দেখাবে:
Found Cookies Name and Value Name : first_name, Value: John Name : last_name, Value: Player