Solved

Cookie in JSP

Posted on 2002-04-25
4
396 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

806 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