Solved

Cookie in JSP

Posted on 2002-04-25
4
392 Views
Last Modified: 2010-04-01
How do I set and read cookie in JSP ?
0
Comment
Question by:tanc02
  • 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 100 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now