This section illustrates you how to send cookie in servlets.This section illustrates you how to send cookie in servlets.
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: