[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2477
  • Last Modified:

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
0
BritaJ
Asked:
BritaJ
  • 4
  • 3
1 Solution
 
CEHJCommented:
You can do that by writing a Cookie and then seeing if you can read it back
0
 
rrzCommented:
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 requestedSessionIdFromCookie  is what will useful for your purpose.
<%
  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>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BritaJAuthor Commented:
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.
0
 
BritaJAuthor Commented:
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

0
 
rrzCommented:
> looking for was a simple boolean test, does this user have cookies enabled. You can use
if(request.isRequestedSessionIdFromCookie()){...}  
>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/>

Open in new window

0
 
BritaJAuthor Commented:
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
<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>

Open in new window

0
 
rrzCommented:
>I want to know on the first page, first visit if they have cookies enabled, not the second page.  
Are you talking about the first request to your web app ?
It is at this time that the session is created on the server.  The server doesn't know if the client will accept the cookie or not. That is why it always sends the jsesionid in the url in the first response along with the cookie it has created. The client upon receiving the first response from your web app, will decide to accept the cookie or not. If the client does not accept the cookie, then  the server will have to rely on the urlwriting session tracking mechanism. That is why is it a good idea to encode all the links to your web app that you put on your page.  We can use  
response.encodeURL(java.lang.String url)  
or we can use the JSTL tag  <c:url>  
Below I have added some javascript to my code.  The browser will know when it accepts the cookie. Bu the server will not know until the browser sends it back.
 

<html>
<head>
<script language="javascript">
<!--
function init() {
                 var allCookies = document.cookie;
                 alert(allCookies);
 
}
//-->
</script>
</head>
<body onload="init()">
<%
  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/>
</body>
</html>

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now