Q:

JSP hit counter with cookies

Im working on a school project, that contains a hit counter with using cookies. I used to make counters just passing an incrementing value into variables in beans at every refresh, but I've been told, that this is bad solution. 
The specifications for this tasks are clear :

create a hit counter
use a cookie to do this
set expiration date of cookie

Is there somebody with a solution ? Im getting pretty clueless about it..
Thanks for each helpful answer.

A:

You can try something like below in your doXXX() method :
Cookie[] cookies = request.getCookies();
int count = 0;
for(Cookie cookie:cookies){
    if(cookie.getName().equals("www.yourDomain.com.HitCounter")){
        count = Integer.parseInt(cookie.getValue());
        count++;
        cookie.setValue(count+"");
    }
}
if(count == 0){
    Cookie cookie = new Cookie("www.yourDomain.com.HitCounter","1");
    cookie.setMaxAge(3600*10*24);// expire in 10 days.
    response.addCookie(cookie);
}

To know about cookies in Servlets , please visit the Javadoc.

