tags:

views:

437

answers:

4

Hi all,,

I had made my project in J2EE..

I wanted to keep track of who all visited my website,, with some specific information of theirs (for e.g. time they visited, IPAddress etc.. ) How can i do this with the help of java program,,

I can store the information in the database. so what I need is the logic as to how can i retrieve the information of the person who visited the website. Tomcat Server,, and i am using jsp+Servlets..

Thanks..

A: 

Check out the ServletRequest API. In particular look at getRemoteAddr(), the remote IP address of your client, getRemoteHost(), and getRemotePort(). Hopefully most of your clients won't be using proxies, or you'll get the proxies' information.

Another technique is to set a unique identifying cookie for each client, and then every subsequent request will carry that cookie. This is probably a better approach than tracking IP addresses. Here's a tutorial on handling cookies in servlets.

Web server level analysis tools are another approach, per altCognito's great suggestion.

Jim Ferrans
i am a beginner.. please give a small example to demonstrate this class..
AGeek
+3  A: 

AWStats is what you really should consider. Run it against your Access logs. I understand you want to use a Java app, but you really, really should consider AWStats because it is a mature product. It will take a LONG time to replicate in Java what AWStats does already.

altCognito
A: 

We also used AWStats at my previous job. Management and clients seemed to be satisfied with it.

I suppose that when you said I had made my project in J2EE, you mean an Enterprise Application which runs on a J2EE server such as Glassfish, JBoss, Weblogic, Geronimo, etc.

If you don't need sophisticated solution, you can get away with a servlet filter or a valve setting in Tomcat(which is usually embedded in most J2EE servers).

A: 

Here is a small example of getting info from a HttpServletRequest using a ServletRequestListener.

First, add the listener to the web.xml config file. This file should be located in the WEB-INF folder.


    <listener>
        <description>RequestListener</description>
        <listener-class>web.MyRequestListener</listener-class>
    </listener>
In the above configuration the ServletRequestListener named MyRequestListener is located in the web package.

Next, create MyRequestListener in the web package as follows.


package web;

import javax.servlet.*;

public class MyRequestListener implements ServletRequestListener {

    public void requestInitialized(ServletRequestEvent event) {
        HttpServletRequest request = (HttpServletRequest)event.getServletRequest();
        System.out.println("request initialized");
        System.out.println("Request Remote Addr = " + request.getRemoteAddr());
        System.out.println("Request Remote Host = " + request.getRemoteHost());
        System.out.println("Request Remote Port = " + request.getRemotePort());
        java.util.Enumeration e = request.getAttributeNames();
        while(e.hasMoreElements()) {
            String attName = (String)e.nextElement();
            Object val = request.getAttribute(attName);
            System.out.println("Request Att (" + attName + ") = " + val.toString());
        }
        e = request.getParameterNames();
        while(e.hasMoreElements()) {
            String paramName = (String)e.nextElement();
            Object val = request.getParameter(paramName);
            System.out.println("Request Param (" + paramName + ") = " + val.toString());
        }
        e = request.getHeaderNames();
        while(e.hasMoreElements()) {
            String headerName = (String)e.nextElement();
            Object val = request.getHeader(headerName);
            System.out.println("Header (" + headerName + ") = " + val.toString());
        }
    }

    public void requestDestroyed(ServletRequestEvent event) {
        System.out.println("request destroyed");
    }
}
All the code does is print out attributes, parameters, and info from the HTTP header. If you need the date for the request you can create a java.util.Date when requestInitialized() is entered.

Keep in mind that a ServletRequestListener's requestInitialized() will be called every time there is a HTTP request from a browser (or bot) so it may be better to use a tool external to your application to track usage. If you are looking for external tools you may want to consider Google Analytics, or Urchin if your network configuration does not allow you to use Google Analytics.

rich