JSP - SPLessons

Chapter 13

JSP Cookies

JSP Cookies

JSP Cookies – Cookies can defined as a small file that will be stored in a browser, it is utilized information tracing purpose. Already Splessons have discussed the cookies concept in servlet technology Servlet Cookies. Following is the list of important useful methods associated with the Cookie object which you can use while manipulating cookies in JSP.


JSP Cookies – Following is the example which describes more about the cookies. Following is the code to set the cookies.
index.jsp

<html>
<body bgcolor="skyblue">
<form action="main.jsp" method="GET">
<center><img src="E:/splessons.png"></br></br>
First Name: <input type="text" name="first_name"></br>
<br />
Last Name: <input type="text" name="last_name"/></br></br>
<input type="submit" value="Submit" /></center>
</form>
</body>
</html>

Here just created two forms they are First Name, Last Name and also created Submit button. The GET method is the default method to pass data from client to web server and it delivers a long string that shows up in the browser’s Location. Never utilize the GET method in the event that you have secret key or other touchy data to go to the server. The GET method has size constraint: just 1024 characters can be in a solicitation string.
main.jsp

<%
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name",
request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name",
request.getParameter("last_name"));
// Set expiry date after 24 Hrs for both the cookies.
firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);
// Add both the cookies in the response header.
response.addCookie( firstName );
response.addCookie( lastName );
%>
<html>
<head>
<title>Setting Cookies</title>
</head>
<body>
<center>
<h1>Setting Cookies</h1>
</center>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last Name:</b>
<%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>

The request.getParameter() is used to retrieve the details from the static page that id HTML page.
Output
When compile the code following is the output will be displayed.

When submit the details final result will be come.
Following is an example to read the cookies with JSP. keep the following code in main.jsp file.

<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<center>
<h1>Reading Cookies</h1>
</center>
<%
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with this domain
cookies = request.getCookies();
if( cookies != null ){
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
out.print("Name : " + cookie.getName( ) + ", ");
out.print("Value: " + cookie.getValue( )+" <br/>");
}
}else{
out.println("<h2>No cookies founds</h2>");
}
%>
</body>
</html>

Place this code in the place of main.jsp file. request.getCookies() is used to get the cookies.
Output
When compile the code following is the output will be displayed.

Following is the code to delete the cookies. Keep the following code in main.jsp file.

<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<center>
<h1>Reading Cookies</h1>
</center>
<%
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with this domain
cookies = request.getCookies();
if( cookies != null ){
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
if((cookie.getName( )).compareTo("first_name") == 0 ){
cookie.setMaxAge(0);
response.addCookie(cookie);
out.print("Deleted cookie: " +
cookie.getName( ) + "<br/>");
}
out.print("Name : " + cookie.getName( ) + ", ");
out.print("Value: " + cookie.getValue( )+" <br/>");
}
}else{
out.println(
"<h2>No cookies founds</h2>");
}
%>
</body>
</html>

public void setMaxAge(int expiry) method sets how much time (in seconds) should elapse before the cookie expires. If you don’t set this, the cookie will last only for the current session.
Output
When compile the code following is the output will be diplayed, where first name will deleted as mentioned in the code.

Summary

  • JSP Cookies – To read cookies, you need to create an array of Java. servlet. http. Cookie objects by calling the GetCookies method of HttpServletRequest.
  • JSP Cookies – response.addCookie is used to add the cookie to the Http response header.