This section illustrates you how to send cookie in servlets.
Cookies are small bits of information that a Web server sends to a browser and that the browser returns unchanged after visiting the same page. A Class javax.servlet.http.Cookie, represents the cookie. HttpservletRequest and HttpServletResponse interfaces have methods for getting and setting the cookies. You can create cookie, read cookie and send cookie to the client browser. You can create cookie by calling the cookie Constructor.
To read cookies, call request.getCookies() method which returns an array
of cookie objects. To send cookie, a servlet create cookie and add the cookie to the response
header with method response.addCookie(cookie).
Here is the code of SendCookie.java
Now in the above example which shows the process of adding and removing cookies. With the method req.getCookies(), you will get all the cookies on request.
When you run the program you will get the following output.
Here is the output
After refreshing the browser , value will change. Here is the output after refreshing the page:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: July 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles