This section illustrates you how cookie is used in Servlet.
The cookie class provides an easy way for servlet to read, create, and manipulate HTTP-style cookies, which allows servlets to store small amount of data. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting the same Web site.
A servlet uses the getCookies() method of HTTPServletRequest to retrieve cookies as request. The addCookie() method of HTTPServletResponse sends a new cookie to the browser. You can set the age of cookie by setMaxAge() method.
Here is the code which defines cookie and shows how to set the maximum age of cookie.
In the above example, a servlet class UseCookies defines the cookie class.
Here the age of cookie has been set as
setMaxAge(100). If its value is set to 0, the cookie will delete immediately. After the time provided been expired, cookie will automatically deleted.
Here is the output
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: July 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles