File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cookie setting

 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have arequirement that a survey is popped up when a user enters the site first time. If he fills it once we need not pop it up for a year again. And when he is in one session if he minimises or kills it we need not pop it up again if he clicks the home from the website or comes to the website by openiing a new window from the existing browser. I am doing this through javascript. I check for the availability of the cookie and if it is there then don't pop up; else I will pop up the survey. All is well except when the user types in http://ourwebsite.com instead of http://www.ourwebsite.com. The survey pops up if he types in the site name without the www before the website address.


I am setting the cookie like this..



Any idea what I am doing wrong?


Thanks,

Vasu
 
Jay King
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add this line of code.
cookie.setDomain(".ourwebsite.com");

~Jay
JavaRSS.com, Java News, Java Articles and Java Blogs, Just one Bookmark.
[ December 15, 2004: Message edited by: Jay King ]
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I have to add two domains? www.ourwebsite.com AND ".ourwebsite.com"? Can one cookie have two domains like that?
 
Jay King
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.ourwebsite.com will take care of both www.ourwebsite.com and ourwebsite.com.

~Jay
JavaRSS.com, Java News, Java Articles and Java Blogs, Just one Bookmark.
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It was helpful. One thing I wanted to do though is to not hardcode the domain name. Is there a way to get it somehow from request or somewhere and set that to the cookie?

Thanks,

Vasu
 
Jay King
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use methods in HttpServletRequest class.

For example:
host = request.getRemoteHost();
if (host == null)
host = request.getRemoteAddr();

You need to check to see if it starts with www or not. You may have other subdomains like abc.ourwebsite.com. So you need to parse the URL to get the ".ourwebsite.com" and set it in the cookie.

~Jay
JavaRSS.com, Java News, Java Articles and Java Blogs, Just one Bookmark.
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jay,

Here is my problem : getRemoteHost() returns null. getRemoteAddr() returns the ip address ( 233.0.0.1, for example). Is there way to get the host name?

Thanks,

vasu
 
vasu maj
Ranch Hand
Posts: 396
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it from getRequestURL() method.

Thanks,

Vasu
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic