Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to maintain user log like where user clicked that information recorded in data base................pLz help me.......!!
Posted
Comments
Richard MacCutchan 11-Feb-13 4:24am    
You need to provide a lot more detail about your problem. Are you trying to log every action on the system, or just the ones in this application?
DalliaF 8-Mar-13 2:43am    
there is an example on this site--->http://stackoverflow.com/questions/2608452/how-to-keep-the-session-of-user-login

1 solution

Hello,

The Java Servlet specification version 2.3 introduced a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.
You can write your own filter to achieve this. Here is a small example directly taken from documentation which logs the servlet access.
Java
public final class HitCounterFilter implements Filter
{
   private FilterConfig filterConfig = null;

   public void init(FilterConfig filterConfig) throws ServletException
   {
      this.filterConfig = filterConfig;
   }

   public void destroy()
   {
      this.filterConfig = null;
   }

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
   {
      StringWriter sw = null;
      PrintWriter writer = null;
 
      if (filterConfig == null) return;

      sw = new StringWriter();
      writer = new PrintWriter(sw);
      Counter counter = (Counter) filterConfig.getServletContext().getAttribute("hitCounter");
      writer.println();
      writer.println("===============");
      writer.println("The number of hits is: " + counter.incCounter());
      writer.println("===============");

      // Log the resulting string
      writer.flush();
      filterConfig.getServletContext().log(sw.getBuffer().toString());
      ...
      chain.doFilter(request, wrapper);
      ...
   }
}

This filter will be configured in web.xml as shown below
XML
<filter id="filter_2">
    <filter-name>activitylogger</filter-name>
    <filter-class>com.yourcompany.filters.activity.HitCounterFilter</filter-class>
</filter>

Regards,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900