- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read cookies with JSP?
To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest. Then cycle through the array, and use getName() and getValue() methods to access each cookie and associated value.
Let us now read cookies that were set in the previous example −
Example
<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with the this domain 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>
Let us now put the above code in main.jsp file and try to access it. If you set the first_name cookie as "John" and the last_name cookie as "Player" then running http://localhost:8080/main.jsp will display the following result −
Output
Found Cookies Name and Value Name : first_name, Value: John Name : last_name, Value: Player
- Related Articles
- How to delete cookies with JSP?
- How cookies work in JSP?
- What are cookies in JSP?
- How to read form data using JSP?
- How do you set cookies in the JSP?
- How to read a HTTP header using JSP?
- How to read all form parameters in JSP?
- How to read request parameters passed in URL using JSP?
- How to read form data using JSP via GET Method?
- How to read form data using JSP via POST Method?
- How to clear all cookies with JavaScript?
- How to send cookies with selenium webdriver?
- How to work with cookies in Selenium with python?
- How can you read a request header information in JSP?
- How to create a session only cookies with JavaScript?

Advertisements