Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

JSP Cookies

 
 
Print   Email
 
     
 

What is a cookie?


Cookie a small data file reside in user’s system. Cookie is made by web server to identify users. When user do any request send to web server and web server know information of user by these cookie. After process request, web server response back to request by knowing through this cookie. JSP provides cookie classes, javax.servlet.http.Cookie, by this classes we can create and retrieve cookie. Once we set value in cookie, it lived until cookie gets expired. Cookie plays a big role in session, because maximum session is tracked by cookie in JSP.

1. JSP Example of creating Cookie

createCookie.jspOutput
 

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="createNewCookie.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="78%">&nbsp;</td>
    </tr>
  <tr>
    <td>Cookie value Set </td>
    <td><input type="text" name="cookieSet" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
</table>
</form>
</body>
</html>

createNewCookie.jsp

 
<%@ page language="java" import="java.util.*"%>
<%
String cookieSet=request.getParameter("cookieSet");

Cookie cookie = new Cookie ("cookieSet",cookieSet);
response.addCookie(cookie);
%>

<html>
<head>
<title>Cookie Create Example</title>
</head>
<body>
    <%
   Cookie[] cookies = request.getCookies();
    
    for (int i=0; i<cookies.length; i++) {
      out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>");
    }
    %>
</body>
</html>

 

Cookie cookie = new Cookie ("cookieSet",cookieSet);

We are creating new object of cookie class.
Here new Cookie(“Key”, “value”);
Then we are adding in cookie of response object.
response.addCookie(cookie); This is adding in cookie object new data.

Expire JSP cookie

Cookie cookie = new Cookie ("cookieSet",cookieSet);

cookie setMaxAge(0);


This will expire cookie.,with 0 second. We can set cookie age who long is reside for user.

Retrieve cookie from JSP

Cookie is in array object, we need to get in request.getCookies() of array.

Cookie[] cookies = request.getCookies();

for (int i=0; i<cookies.length; i++) {
      out.println(cookies[i].getName()+" : "+cookies[i].getValue()+"<br/>");
}

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy