Solved

Cookie in JSP

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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