Ex No: 13 COOKIE MANIPULATION USING JSP
Aim: To create a JSP page to manipulate cookies.
Algorithm
Step1: Create a folder “Ex13” to place the HTML and JSP files.
Step2: Create files “index.html”, “ex13.jsp” and “getcookie.jsp”.
Source Code

index.html


<!DOCTYPE html>
<html>
 <head>
  <title>JSP Cookie</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Cookie Manipulation</h1>  
  </header>  
  <article>
    <section>  
     <form method="POST" action="ex13.jsp">
    <label>Enter Name:</label>
    <input type="text" name="txtname" /><br>
    <input type="submit" value="Submit"/>
     </form> 
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>

ex13.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
 <head>
  <title>JSP Cookie</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Cookie Manipulation</h1>  
  </header>  
  <article>
    <section>  
     <%
       String name, timestamp, result="";
       Cookie cookie;
       name = request.getParameter("txtname");
       Date today = new Date();
       timestamp = today.toString();  
       if(name.equals(""))
       {
         out.println("<a href='index.html'>Home</a>");  
         result = "Enter a valid User Name";
       }
       else
       {
         cookie = new Cookie("username", name);
         cookie.setMaxAge(365*24*60*60);
         response.addCookie(cookie);
         out.println("<a href='getcookie.jsp'>Get the Details in Cookie</a>");
         result = "Cookie Saved Successfully";
       }
     %>
     <p><% out.print(result);%></p> 
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>

getcookie.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
 <head>
  <title>JSP Cookie</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Cookie Manipulation</h1>  
  </header>  
  <article>
    <section>  
     <%
       String username, cookiename;
       Cookie cookie = null;
       Cookie[] cookies = null;
       cookies = request.getCookies();
       username = "User Name Not available in Cookie";
       if(cookies!=null)
       {
        for(int i=0;i<cookies.length;i++)
        {
         cookie = cookies[i];
         cookiename = cookie.getName();
         if(cookiename.equals("username"))
         {
          username = cookie.getValue();
         }
        }
       }
     %>
     <p><% out.println("User Name Stored in Cookie is <b>"+username+"</b>"); %></p>
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>
Result: Thus JSP page is written to manipulate cookie.