Friday, March 27, 2015

example for cookies in JSP

Brief about cookies in JSP

Cookies are text files stored in client computer and are used for tracking information and authentication purpose, JSP transparently supports HTTP cookies sing underlying servlet technology. 

This process includes following stuffs :

  1. Server sends a set of cookies to the client browser.
  2. Client browser stores and prompt to save or not.
  3. Next time when the client visits the same address the cookies are used to get the information of the user last saved.


Setting Cookies in JSP

Example:


JSP File (index.jsp)

 <!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <h2> Nirajan Ghimirey's Workshop Login Form </h2>  
 <form action="setcookie.jsp" method="POST">  
 <label>Name</label><input type="text" name="full_name">  
 <br />  
 <label>Email: </label><input type="text" name="email" /><br/>  
 <input type="submit" value="Submit" />  
 </form>  
 </body>  
 </html>  

Use below script to set cookie in JSP project.

JSP File (setcookie.jsp)
 <%  
   // Create cookies for name and email.     
   Cookie fullname = new Cookie("full_name",  
 request.getParameter("full_name"));  
   Cookie email = new Cookie("email",  
 request.getParameter("email"));  
   // Set expiry date after 24 Hrs for both the cookies.  
 fullname.setMaxAge(60 * 60 * 24);  
 email.setMaxAge(60 * 60 * 24);  
   // Add both the cookies in the response header.  
 response.addCookie(fullname);  
 response.addCookie(email);  
 %>  
 <html>  
 <head>  
 <title>nirajanghimireyworkshop.blogspot.com</title>  
 </head>  
 <body>  
 <center>  
 <h1>Setting Cookies Example</h1>  
 </center>  
 <p><b>Full Name: </b>  
 <%= request.getParameter("full_name")%>  
 </p><p><b>Email: </b>  
 <%= request.getParameter("email")%>  
 </p>  
 </body>  
 </html>  


Process for reading cookies in JSP.

Reading Cookies

Example:
<!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</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>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> 

JSP File (index.jsp)

<!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</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>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> 

Script for deleting cookie in JSP.

Delete Cookies

Example:

JSP File (index.jsp)

<!DOCTYPE html>  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
 <title>nirajanghimireyworkshop.blogspot.com</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> Cookies Name and Value</h2>");  
 for (int i = 0; i <cookies.length; i++){  
 cookie = cookies[i];  
 if((cookie.getName( )).compareTo("full_name") == 0 ){  
 cookie.setMaxAge(0);  
 response.addCookie(cookie);  
 out.print("<b>Deleted cookie: </b> " +  
 cookie.getName( ) + "<br/>");  
      }  
 out.print("Name : " + cookie.getName( ) + ", ");  
 out.print("Value: " + cookie.getValue( )+" <br/>");  
    }  
 }else{  
 out.println(  
    "<h2>No cookies founds</h2>");  
  }  
 %>  
 </body>  
 </html>  


No comments:

Post a Comment