?
Solved

Cookie in JSP

Posted on 2002-04-25
4
Medium Priority
?
410 Views
Last Modified: 2010-04-01
How do I set and read cookie in JSP ?
0
Comment
Question by:tanc02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6970256
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() );
  }
%>
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 400 total points
ID: 6971058
Kotan is right.. but IMHO to retrieve a value of a cookie it is usually better to create a method:

public static String getCookieValue(String cookieName) {
     Cookie[] theCookies = request.getCookies();
     if (theCookies != null) {
        for (int i =0; i< cookies.length; i++) {
           Cookie currentCookie = theCookies[i];
           if (currentCookie.getName().equalsIgnoreCase(cookieName)
            return currentCookie.getValue();
           }
        }
   return null;
}

so using this method you can do:
getCookieValue("testcookie");

This will check if a cookie called testcookie exists and
retrieve its value and return it.

Null is returned if the cookie does not exist.

so to use it:

String myCookieValue = getCookieValue("mycookiename");
if (myCookieValue == null) {
   // cookie does not exist
}
else {
  // cookie value exists and use it as you need.
}


I usually put the method in a utility class or something but you can put it in the JSP itself too.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6971067
you should put the method code in <%! ... %> so it is available as a method to the entire JSP.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6972770
Glad I could help, Thanx for the "A"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question