In this section, we will discuss about handling cookies in JSP with an example.
In this section, we will discuss about handling cookies in JSP with an example.In this section, we will discuss about handling cookies in JSP with an example. Cookies are small chunk of textual information that a web server sends to a web browser. The cookies are saved to clients hard disk in the form of small text file. Cookies have very important role in the session tracking. By reading information from the cookie which the web server sent it previously, the previously visited sites can offer visitors with a number of conveniences:-
Browsers generally only accept 20 cookies per site and 300 cookies total, and each cookie is limited to 4KB.
Drawbacks :
Cookie class : In JSP , cookie are the object of the class
javax.servlet.http.Cookie .
A cookie's value can uniquely identify a
client, so cookies are commonly used for session management. A cookie has a
name, a single value, and optional attributes such as a comment, path and domain
qualifiers, a maximum age, and a version number. A Cookie object may be added to
an HttpResponse
object using HttpResponse.addCookie()
.
To extract a cookie from an HttpRequest
object, you can use HttpRequest.getCookies()
, which returns an array of Cookie
objects representing all the cookies included in the request.
Cookies can be constructed using the following code:
Cookie cookie-name = new Cookie(java.lang.String name,
java.lang.String value) ;
|
Cookie class have the following methods :
Method | Description |
getComment() | Returns the comment describing the purpose of
this cookie, or null if no such comment has been defined. |
getMaxAge() | Returns the maximum specified age of the cookie. |
getName() | Returns the name of the cookie. |
getPath() | Returns the prefix of all URLs for which this cookie is targeted. |
getValue() | Returns the value of the cookie. |
setComment(String ) | If a web browser presents this cookie to a
user, the cookie's purpose will be described using this comment. |
setMaxAge(int) | Sets the maximum age of the cookie. The cookie
will expire after that many seconds have passed. Negative values indicate the default behavior: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted |
setPath(String) | This cookie should be presented only with
requests beginning with this URL. |
setValue(String) | Sets the value of the cookie. Values with
various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers. |
Example :
In this example , we will set the cookie & then display it in a JSP page.
Below the code of the form which prompts the user to enter his/her name :
cookieform.jsp
<html> <head> <title>Input Form</title> </head> <body> <form method="post" action="setcookie.jsp"> <p><b>Enter Your Name: </b><input type="text" name="username"><br> <input type="submit" value="Submit"> </form> </body> </html> |
Given below the code for setting value of cookie :
setcookie.jsp
<%@ page language="java" import="java.util.*"%> <% String username=request.getParameter("username"); if(username==null) username=""; Date now = new Date(); Cookie cookie = new Cookie ("username",username); cookie.setMaxAge(365 * 24 * 60 * 60); response.addCookie(cookie); %> <html> <head> <title>Saving Cookie</title> </head> <body> <p><a href="showcookievalue.jsp">click here to see Next Page to view the cookie value</a></p> </body> |
Given below the code for showing the value of the cookie ,which is user's entered name :
showcookievalue.jsp
<%@ page language="java" %> <% String cookieName = "username"; Cookie cookies [ ] = request.getCookies (); Cookie myCookie = null; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies [i].getName().equals (cookieName)) { myCookie = cookies[i]; break; } } } %> <html> <head> <title>Show Saved Cookie</title> </head> <body>
<% if (myCookie == null) { %> No Cookie found with the name <%=cookieName%> <% } else { %> <h3><font color="red">Welcome: <%=myCookie.getValue()%></font></h3> <% } %> </body> </html> |
OUTPUT :
After submitting name, final output(value of the cookie) displays as :