Solved

setting a cookie based on the http referer

Posted on 2009-07-13
6
466 Views
Last Modified: 2012-05-07
Hi,

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...
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_21663876.html?sfQueryTermInfo=1+refer

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.

Thanks,
biglarrrr
0
Comment
Question by:biglarrrr
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:mrcoffee365
Comment Utility
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:
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/Cookie.html



0
 

Author Comment

by:biglarrrr
Comment Utility
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. :)

Thanks,
biglarrrr
0
 
LVL 26

Expert Comment

by:mrcoffee365
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Accepted Solution

by:
mrcoffee365 earned 500 total points
Comment Utility
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);


0
 

Author Comment

by:biglarrrr
Comment Utility
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);

      break;

    }

  }
 

  if (!refFound) {

    if (referrer!= null && referrer.length() > 0) {

      Cookie cookie = new Cookie("referrer",referrer);

      cookie.setMaxAge(30*60);

      response.addCookie(cookie);

      session.setAttribute("referrer", referrer);

    }

  }

}

Open in new window

0
 

Author Closing Comment

by:biglarrrr
Comment Utility
Thanks for your help. I'm sorry I didn't explain my question in better detail but you managed to read my mind. :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

18 Experts available now in Live!

Get 1:1 Help Now