BritaJ
asked on
Check if Cookies are enabled using Java or jsp
I have code that looks for a specific cookie and if the system can't find it, we assume that the person is a new visitor and generates a new cookie and stores that number in a database. We use this to count how many times a person comes back to our site.
Problem is, if a person has cookies turned off, then everytime they visit, they get a new number and entry into the database. Using Java or jsp how can I determine if they have cookies turned off. Thus preventing me from needless additions to the database.
Thanks
Brita
Problem is, if a person has cookies turned off, then everytime they visit, they get a new number and entry into the database. Using Java or jsp how can I determine if they have cookies turned off. Thus preventing me from needless additions to the database.
Thanks
Brita
You can do that by writing a Cookie and then seeing if you can read it back
You can play with the code below here to get an idea how it all works. You should use Firefox to easily turn off cookies for testing.
Anyway the method requestedSessionIdFromCook ie is what will useful for your purpose.
Anyway the method requestedSessionIdFromCook
<%
Integer count = (Integer)session.getAttribute("count");
if(count==null)count = new Integer(0);
session.setAttribute("count",new Integer(count.intValue() + 1));
%>
sessionId=${pageContext.session.id}<br/>
isNew=${pageContext.session.new}<br/>
fromURL=${pageContext.request.requestedSessionIdFromURL}<br/>
fromCookie=${pageContext.request.requestedSessionIdFromCookie}<br/>
count=<%=count%> <br/>
<html>
<body>
<form>
<input type="hidden" name="sid" value="${pageContext.session.id}"/>
<input type='submit' value="Add to request count in this session." />
</form>
</body>
</html>
ASKER
I tried the code you gave me and it works just fine. What I was really looking for was a simple boolean test, does this user have cookies enabled.
If yes - do some stuff (wite to the database, etc)
if no - do something else
Thanks.
If yes - do some stuff (wite to the database, etc)
if no - do something else
Thanks.
ASKER
I worked with the code you gave me some more.
I only get that the "from cookie" true if it is the second time I am there and cookies are turned on.
I don't see anything that will tell me if cookies are enabled the first time I visit the page.
Brita
I only get that the "from cookie" true if it is the second time I am there and cookies are turned on.
I don't see anything that will tell me if cookies are enabled the first time I visit the page.
Brita
> looking for was a simple boolean test, does this user have cookies enabled. You can use
if(request.isRequestedSess ionIdFromC ookie()){. ..}
>tell me if cookies are enabled the first time I visit the page.
For the first request. session.isNew() returns true. The client has not joined the session yet. Play with the code below and notice that for the first response, the server always sends the jsessionid in the url. That happens whether the client has cookies turned on or not. The server doesn't know yet if the client will be sending a cookie back in the second request.
if(request.isRequestedSess
>tell me if cookies are enabled the first time I visit the page.
For the first request. session.isNew() returns true. The client has not joined the session yet. Play with the code below and notice that for the first response, the server always sends the jsessionid in the url. That happens whether the client has cookies turned on or not. The server doesn't know yet if the client will be sending a cookie back in the second request.
<%
String url = request.getRequestURL().toString();
String encoded = response.encodeURL(url);
Integer count = (Integer)session.getAttribute("count");
if(count==null)count = new Integer(0);
session.setAttribute("count",new Integer(count.intValue() + 1));
%>
sessionId=<%=session.getId()%><br/>
isNew=<%=session.isNew()%><br/>
fromURL=<%=request.isRequestedSessionIdFromURL()%><br/>
fromCookie=<%=request.isRequestedSessionIdFromCookie()%><br/>
url=<%=url%><br/>
encoded=<%=encoded%><br/>
<a href="<%=url%>">Not encoded request</a><br/>
<a href="<%=encoded%>">Encoded request</a><br/>
count=<%=count%> <br/>
ASKER
You are missing the point.
Here is an example of what I am trying to do using javascript. I want to get this same information into my jsp code. I want to know on the first page, first visit if they have cookies enabled, not the second page.
Thanks
Brita
Here is an example of what I am trying to do using javascript. I want to get this same information into my jsp code. I want to know on the first page, first visit if they have cookies enabled, not the second page.
Thanks
Brita
<script language="javascript">
<!--
function init() {
document.cookie = "test=ok"
if (document.cookie.indexOf("test=ok")==-1) alert ("you have cookies enabled")
else alert("you don't have cookies enabled")
}
//-->
</script>
</head>
<body onload="init()">
</body>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.