NOTE: The only way to reliably identify a returning user is via authentication. Using cookies, you're really checking for returning connections from the same browser.
If the user clears cookies, re-installs the browser, uses another browser, or uses another computer, your web application most likely will not remember them. With that said, here is one way to identify returning users without authentication:
The HttpSession is one way to detect a returning user. When a user has activity on your Web application, you can store a key that uniquely identifies that user. This places a cookie in their browser so that when they return, your Web application can pull that data from the session.
From the HttpSession javadocs:
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
Record user activity:
ArrayList activity = new ArrayList<String>();
activity = (ArrayList<String>) request.getSession().getAttribute("activity");
if(activity == null)
activity = new ArrayList<String();
activity.add("Searched for parts for Ford Thunderbird");
request.getSession().setAttribute("activity", activity);
Display user activity:
ArrayList activity = new ArrayList<String>();
activity = (ArrayList<String>) request.getSession().getAttribute("activity");
if(activity == null) {
log.info("No activity to display. New user");
} else {
for(String a : activity) {
log.info(a);
}
}
Setting the timeout in web.xml:
<!-- Define the default session timeout for your application,
in minutes. From a servlet or JSP page, you can modify
the timeout for a particular session dynamically by using
HttpSession.getMaxInactiveInterval(). -->
<session-config>
<session-timeout>30</session-timeout> <!-- 30 minutes -->
</session-config>