File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Hit Counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Hit Counter" Watch "Hit Counter" New topic
Author

Hit Counter

sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
Hi guys
i am using a hit counter
i have created a bean with scope as application and am checking for "session.isNew()"
if this returns true then i increment the counter else i dont.
but the problem is that if i load my page count.jsp and then
go to suppose hotmail.com in the same browser then come back again to my page count.jsp then i want the counter to increment!but it doesnt....
could anyone plz help me with this
Now there is a second option which i am using
I am checking for the referer in the header which gives me the previous page.
i.e
request.getHeader("Referer")
gives me
null if i am putting the url in the browser
if i am coming from the same domain it gives me the following url
http://sameera:8080/examples/jsp/index.jsp
i am just checking that if the referrer is null or anything other than my machine( where server is present Tomcat 3.2.1) i increment the count but in this case
if i open a new browser and put the url
http://sameera:8080/examples/jsp/counter.jsp
it increments the counter but when isay refresh it again increments it which i dont want could any one gimme a solution
plzz
thanks in advance
i would surely post the code .. if the above explanation is confusing
plz help surely
Sam


SCJP
Sam Dalton
Author
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
Hi
Session.isNew will only return true if the session is actually new. In you case (where you are visiting another site in the same browser and coming back) it will not be, because the session is still valid from the last visit to the site. If you open you page in another browser instance, you will probably find that the counter increases. If it doesn't we probably have a more fundamental problem, and you should post again.
Hope this helps
Sam
[ March 11, 2002: Message edited by: Sam Dalton ]

<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
Hi Sam
Well yes if i open a new browser then the count does increase
but it doesnt increases in the same window and yeah it is bcoz the session remains the same
but could u plz tell me a solution for this
like i want the count to increase when
1)Any of the pages of my site are being accessed.
2)if i open a browser open my page and then in same browser i go to hotmail then again i put the url of my site then in this condition also i want it to increase
# And i dont want it to increase when i load my page and refresh
Could u plz help me out
Thnks
sameera
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
Originally posted by sameera kocharekar:
Hi Sam
Well yes if i open a new browser then the count does increase
but it doesnt increases in the same window and yeah it is bcoz the session remains the same
but could u plz tell me a solution for this
like i want the count to increase when
1)Any of the pages of my site are being accessed.
2)if i open a browser open my page and then in same browser i go to hotmail then again i put the url of my site then in this condition also i want it to increase
# And i dont want it to increase when i load my page and refresh
Could u plz help me out
Thnks
sameera


to full fill ur requirement i can suggest u a soln. but this soln only works if links to ur application pages doesn't exist any where out side ur application in whole world. according to me what u should do is just when ever any of teh page of ur application has been accessed by any one just check the HTTP referrer Header. if that is null then and only then increment ur hit counter else not.
with the help of above technic ur hit counter always increased by mentioned amount when ever any one directly enter url in the browsers address bar and never when some one click a link to jump to ur any of the JSP page.
Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117
Ranchers,
A question on the hit counter. (I posted this in the SCWCD forum) If Sameera is using a bean to store the hits - what happens when the Server is shut down and restarted. Where is the latest count value stored and how? I assume they want to keep track of a running total.
Regards,
Stephen Batsas
SCJP2
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
Hi there
well yeah i am checking for the referer in the header and if it is null then only i increase the count
But in this case if i load my page say
http://sameera:8080/examples/jsp/counter.jsp
(suppose this is the main page of my application)
and check for the referer which is null
and i incr the count
but now if i say refresh ot click the refresh button of the browser then for each refresh it adds 1 to the count.
could u plz tell me a solution for this refresh problem.
And yeah i am storing the hitcount in a file so even if i stop the server no probs..Thnaks
Sam
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
ur hit counter algorithm should look like this.
if (referrer==null && session.isNew())
hitCounter++;
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
Hi guys
i am back again
well thanks gautam
i had been doing the two things
i.e. checking for referer as null and checking for session.isNew() separately
now i am doing it together.Now my refresh problem has been solved.
But i still have a problem
Now take this condition
I open a browser and type the url
http://sameera:8080/examples/jsp/counter.jsp
and the hitcount increses by 1
now in the same browser i type
www.javaranch.com
i acces 1-2 pages concept is to go to another site and come back again to my page
ie now again i type
http://sameera:8080/examples/jsp/counter.jsp
now the referer is NULL but since the session.isNew() returns false the count does not increase
And i want this also to be considered as a hit
IS it possible?
I have tried really hard for this....
Plz do help me
Thanks
Sam
sameerleekha
Greenhorn

Joined: Jan 22, 2002
Posts: 26
hi sameer,
u are opening ur jsp page in the same browser.
so no question of session damage due to browser.
u are incrementing by checking session.isNew().
but this may give false because of the expiry of the session and in this case u will not be able to increment. and this is what is happening at ur side.


sameer
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
Well Well sameera
u have got me wrong i guess
See i get session.isNew() false bcoz i am in the same browser and not bcoz session expiry
What i want is that it should increase the count
I open a browser and type the url
http://sameera:8080/examples/jsp/counter.jsp
and the hitcount increses by 1
now in the same browser i type
www.javaranch.com
i acces 1-2 pages concept is to go to another site and come back again to my page
ie now again i type
http://sameera:8080/examples/jsp/counter.jsp
now the referer is NULL but since the session.isNew() returns false the count does not increase
And i want this also to be considered as a hit
Sam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hit Counter