This section illustrates you how to read cookies from Servlets.
The Cookie Class provides an easy way to read Cookies. You can use getCookies() method to retrieve all the cookies in your servlet program.
This getCookies() method returns an array of cookie objects. In this example we will show you how you can retrieve all the cookies and display using servlets.
Here is the code of ReadCookies.java
import javax.servlet.ServletException;.intValue(); |
web.xml for ReadCookie.java
| <context-param> <param-name>maxAge</param-name> <param-value>100</param-value> </context-param> <servlet> <servlet-name>ReadCookies</servlet-name> <servlet-class>ReadCookies</servlet-class> </servlet> <servlet-mapping> <servlet-name>ReadCookies</servlet-name> <url-pattern>/ReadCookies</url-pattern> </servlet-mapping> |
After Calling the request, cookie value, cookie maxAge, cookie path along with cookie name, will be displayed on the browser. And after reloading Cookie value has been changed with getNextCookieValue() method.
Output will be displayed as:

After reloading the application cookie value will be changed, output will be:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Read Cookies from Servlet View All Comments
Post your Comment