In this tutorial you will learn about the Servlet Cookies and its application with practical example.
Servlet Cookies
Cookies are the small amount of information which are sent by the servlet to a web Browser . Browser saves this information and later on it sends to server. Cookies are commanly used for session management because value of cookies can uniquely identify a client. Cookies can be name, a single value and optional attributes like a comment, path , domain qualifiers etc. The servlet method which is used to add cookies to a web browser is HttpServletResponse.addCookie(javax.servlet.http.Cookie) and Cookies can be retrieved by request, the method for getting this thing is HttpServletRequest.getCookies().
Types of Cookies
There are two types of cookies in servlet.
Non-persistant cookie :- This type of cookie removed when user closes the browser and valid only for single session.
Persistent cookie :- This type of cookie is not removed each time on closing of browser. This is removed only when user logout. This is valid for multiple session.
Constructor Summary:
Cookie(java.lang.String name, java.lang.String value): This constructor is used to construct a cookie with specified name and value.
Cookie class Methods
There are some important methods which are used to handle cookies, let’s understand them.
java.lang.object clone(): It overrides the standard java.lang.object.clone method which is used return a copy of this cookie.
int getMaxAge(): This method is used to return maximum age of cookie, specified in seconds, By default. When age is -1 it means the cookie will persist until browser shutdown.
java.lang.String getName(): This method is used to return name of cookie.
java.lang.String getPath() : This method is used to return the path on the server to which the browser returns this cookie.
boolean getSecure(): This method is used to returns true if the browser is sending cookies only over a secure protocol, or false If the browser can send cookies using any protocol.
java.lang.String getValue(): This method is used to returns the value of cookie.
void setMaxAge(int exp): This method is used to sets the maximum age of cookie in seconds.
void setPath(java.lang.String uri): This method is used to specifies the path for the cookie.
void setSecure(boolean flag): It indicates to the browser whether the cookie should only be sent using secure protocol, such as HTTPS or SSL.
void setValue(java.lang.String newValue):This method is used to assigns a new value to a cookie after the cookie protocol this cookie complies with.
Creating Cookie
Example:-
1 2 |
Cookie sname=new Cookie("sitename","w3adda");//creating cookie object response.addCookie(sname);//adding cookie to response |
Deleting Cookie
Example:-
1 2 3 |
Cookie sname=new Cookie("sitename","");//deleting cookie value sname.setMaxAge(0);//set maxage to 0 seconds response.addCookie(sname);//add cookie to response |
Get Cookie
Example:-
1 2 3 4 |
Cookie ck[]=request.getCookies(); for(int i=0;i<ck.length;i++){ out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//print cookie name and value } |