Link to home
Start Free TrialLog in
Avatar of tanc02
tanc02

asked on

Cookie in JSP

How do I set and read cookie in JSP ?
Avatar of kotan
kotan
Flag of Malaysia image

To set cookie,

<%@ page import="javax.servlet.http.Cookie" %>
<%
  Cookie ck1 = new Cookie("key1", "value1");
  Cookie ck2 = new Cookie("key2", "value2");
  response.addCookie(ck1);
  response.addCookie(ck2
%>

To get back cookie,
<%@ page import="javax.servlet.http.Cookie" %>
<%
  Cookie[] ck = request.getCookies();

  for (int i = 0; i < ck.length; i++)
  {
    out.println( ck[i].getName() + ": " + ck[i].getValue() );
  }
%>
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you should put the method code in <%! ... %> so it is available as a method to the entire JSP.

CJ
Glad I could help, Thanx for the "A"