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

Public intgetMaxAge() 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

Example:

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 Action_cookie.jsp. Action_cookie_main.jsp

Explanation of the code:

Action_cookie.jsp 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. Action_cookie_main.jsp 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().

Output:

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.