Pages Navigation Menu

Coding is much easier than you think

How to get cookies value in java servlet?

How to get cookies value in java servlet?

Cookies come as part of the HttpServletRequest object. The Cookie Class provides an easy way to read Cookies. You can use getCookies() method to retrieve all the cookies in your servlet program.You can gain access to any cookies sent by the browser from the javax.servlet.http.HttpServletRequest passed to the servlet’s doGet, doPost, etc methods.

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.

// Check for cookies
Cookie[] cookie_jar = request.getCookies();

// Check to see if any cookies exists
if (cookie_jar != null)
{
	for (int i =0; i< cookies.length; i++)
	{
		Cookie aCookie = cookie_jar[i];
		pout.println ("Name : " + aCookie.getName());
		pout.println ("Value: " + aCookie.getValue());
	}
}

About Gokul


I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.