aspose file tools*
The moose likes Servlets and the fly likes Cookie setting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cookie setting" Watch "Cookie setting" New topic
Author

Cookie setting

vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
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


What a wonderful world!
Jay King
Greenhorn

Joined: Oct 19, 2004
Posts: 10
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

Joined: Jul 12, 2001
Posts: 395
Do I have to add two domains? www.ourwebsite.com AND ".ourwebsite.com"? Can one cookie have two domains like that?
Jay King
Greenhorn

Joined: Oct 19, 2004
Posts: 10
.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

Joined: Jul 12, 2001
Posts: 395
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

Joined: Oct 19, 2004
Posts: 10
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

Joined: Jul 12, 2001
Posts: 395
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

Joined: Jul 12, 2001
Posts: 395
I got it from getRequestURL() method.

Thanks,

Vasu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cookie setting