setting a cookie based on the http referer

Posted on 2009-07-13
Last Modified: 2012-05-07

I need to set a cookie for every user based on the referer. I figured out how to retrieve a cookie in a JSP here...

Now, I need to figure out how to set a cookie based on the value of that referer (URL). I already have a setCookie() javascript function created btw. I'd really appreciate it if someone could help me out.

Question by:biglarrrr
  • 3
  • 3
LVL 27

Expert Comment

ID: 24848695
To set a cookie from a JSP page, you create a Cookie object and add it to the HttpServletResponse.  Something like this:

Cookie aCookie = new Cookie( "mycookiename", "mycookievalue");
aCookie .setPath("/");
response.addCookie( aCookie );

The response object is the special object in a JSP page that refers to the HttpServletResponse.

See the Cookie api here for more values to set if you want a permanent cookie rather than a session cookie:


Author Comment

ID: 24850850
Thanks for replying mrcoffee365...

I know how to create a cookie, I just can't figure out how to create a cookie which holds the value of the Referer. I read that I'll need to use request.getHeader("Referer") to get the referer, but how do I take the value of that and drop it in a cookie? I only want this to be a session cookie.

I'm sorry, I'm not exactly the greatest when it comes to JavaScript. :)

LVL 27

Expert Comment

ID: 24851115
Okay, are you looking for javascript code or JSP code?  They are not the same thing, and you seem to be using the terms interchangeably.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 27

Accepted Solution

mrcoffee365 earned 500 total points
ID: 24851195
In case you mean JSP, how to set the value of the cookie is in the line I posted:

Cookie aCookie = new Cookie( "mycookiename", "mycookievalue");

If you want it to have the url in the referer, then put that in for the string of the value.

Cookie aCookie = new Cookie( "mycookiename", refererVal);


Author Comment

ID: 24851710
THANK YOU!!! That's the part I couldn't figure out. And it was all because I didn't put quotes around "mycookiename". :( Thanks so much for your help on this! I've paste my code below...
String ref_value = null;
boolean refFound = false;
String referrer = request.getHeader("Referer");
System.out.println("referer url = " + referrer);
Cookie[] cookies = request.getCookies();
if (cookies != null) {
  for (int i=0; i< cookies.length;i++) {
    Cookie cookie = cookies[i];
    if (cookie.getName().equals("referrer")) {
      ref_value = cookie.getValue();
      refFound = true;
      session.setAttribute("referrer", ref_value);
  if (!refFound) {
    if (referrer!= null && referrer.length() > 0) {
      Cookie cookie = new Cookie("referrer",referrer);
      session.setAttribute("referrer", referrer);

Open in new window


Author Closing Comment

ID: 31603070
Thanks for your help. I'm sorry I didn't explain my question in better detail but you managed to read my mind. :)

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using placeholders where value is empty 3 26
Problem with small jquery script 6 52
Firefox script error when open a specific page 13 111
javascript, button, count 6 150
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (, worldwide spending on cybersecurity …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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