JSP Cookie


<%@page contentType=”text/html” pageEncoding=”UTF-8″%>

<!DOCTYPE html>

<%

Cookie f_name=new Cookie(“first_name”,request.getParameter(“fname”));

Cookie l_name=new Cookie(“last_name”,request.getParameter(“lname”));

f_name.setMaxAge(60*60*24);

l_name.setMaxAge(60*60*24);
response.addCookie(f_name);

response.addCookie(l_name);
%>

<%

Cookie ck=null;

Cookie[] ck1=null;

ck1=request.getCookies();
if(ck1!=null)

{

for(int i=0;i<ck1.length;i++)

{

ck=ck1[i];

out.println(“Name :”+ck.getName());

out.println(“Value :”+ck.getValue());

}

}

else

out.println(“cookie not found”);

%>
cookiedemo.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
</head>
<body>
<form action=”testcookie.jsp” method=”post”>
Enter first name:<input type=”text” name=”fname”><br>
Enter last name :<input type=”text” name=”lname”><br>
<input type=”submit” value=”submit”>
</form>
</body>
</html>