Solved

Cookie in JSP

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error when compiling my project in eclipse luna 30 538
lessBy10  challenge 15 97
spring example non maven 4 104
collection output issue 9 91
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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