What are Cookies?
- Cookies are the text files which are stored on the client machine.
- They are used to track the information for various purposes.
- It supports HTTP cookies using servlet technology
- The cookies are set in the HTTP Header.
- If the browser is configured to store cookies, it will keep information until expiry date.
JSP cookies methods
Following are the cookies methods:
Public void setDomain(String domain)
This JSP set cookie is used to set the domain to which the cookie applies
Public String getDomain()
This JSP get cookie is used to get the domain to which cookie applies
Public void setMaxAge(int expiry)
It sets the maximum time which should apply till the cookie expires
It returns the maximum age of cookie in JSP
Public String getName()
It returns the name of the cookie
Public void setValue(String value)
Sets the value associated with the cookie
Public String getValue()
Get the value associated with the cookie
Public void setPath(String path)
This set cookie in JSP sets the path to which cookie applies
Public String getPath()
It gets the path to which the cookie applies
Public void setSecure(Boolean flag)
It should be sent over encrypted connections or not.
Public void setComment(String cmt)
It describes the cookie purpose
Public String getComment()
It the returns the cookie comments which has been described.
How to Handle Cookies in JSP
- Creating the cookie object
- Setting the maximum age
- Sending the cookie in HTTP response headers
In this JSP cookies example, we will learn how to call cookie constructor in JSP by creating cookies of username and email, and add age to the cookie for 10 hours and trying to get the variable names in the action_cookie.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie</title> </head> <body> <form action="action_cookie_main.jsp" method="GET"> Username: <input type="text" name="username"> <br /> Email: <input type="text" name="email" /> <input type="submit" value="Submit" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% Cookie username = new Cookie("username", request.getParameter("username")); Cookie email = new Cookie("email", request.getParameter("email")); username.setMaxAge(60*60*10); email.setMaxAge(60*60*10); // Add both the cookies in the response header. response.addCookie( username ); response.addCookie( email ); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Cookie JSP</title> </head> <body> <b>Username:</b> <%= request.getParameter("username")%> <b>Email:</b> <%= request.getParameter("email")%> </body> </html>
Explanation of the code
Code Line 10-15: Here we are taking a form which has to be processed in action_cookie_main.jsp. Also, we are taking two fields “username” and “email” which has to be taken input from the user with a submit button.
Code Line 6-9: Creating two cookie objects of “username” and “email” using request.getParameter.
Code Line 12-13: Here we are adding age to both the cookies, which have been created of 10 hours i.e. cookies will expire in that age.
Code Line 16-17: Adding cookies to the session of username and email and these two cookies can fetched when requested by getParameter().
When you execute the above code you get the following output:
When we execute the action_cookie.jsp we get two fields username and email, and it takes user input and then we click on the submit button.
We get the output from action_cookie_main.jsp where variables are stored in the cookies JSP on the client side.