Use log4j to generate a log. The log class is then added to the filter. Make the log when the user is accessible when the JSP or Servlet is asked. Easy to debug. Below is the address of the user access to the user and writes the file with log4j. Package xzt.rs.tools; import xzt.rs.tools.cundate; import java.io. *; import javax.servlet. *; import javax.servlet.http. *; import java.util. *; // for Date Classimport org.apache.log4j. *; / ** Simple filter That Prints a report in the log file * WHENEVER THE Associated Servlets or JSP Pages * Are Accessed. * /
public class LogFilter implements Filter {protected FilterConfig config; private ServletContext context; private String filterName; private Cundate cd = new Cundate (); // static Logger logger = Logger.getLogger (LogFilter.class.getName ()); // public void DOFILTER (ServletRequest Request, Filterchain Chain) throws servletexception, ioException {httpservletRequest Req = (httpservletRequest) Request;
Chain.dofilter (Request, Response); // PropertyConfigurator.configure ("log4j.properties);
Logfilter.logger.info (req.getRemoteHost () "Trying to access" Req.getRequestURL () "in" "(report:" filtername ".)" );
//}
public void init (FilterConfig config) throws ServletException {this.config = config; // In case it is needed by subclass context = config.getServletContext ();. filterName = config.getFilterName ();}
Public void destroy () {}
} // Get the simple class of the current date. Package xzt.rs.tools. *; public class cupate {public string gettime4 () {
Date d = new date (); // get current date to d. SimpleDateFormat FM = new SimpleDateFormat ("YYYY-MM-DD-HH-MM-SS"); // set date format string strdate = fm.format (d ); // get date to string strdate via format fm. Return strdate;}} Configuring a filter in Web.xml, allowing users to access files, time, and users of the user when accessing JSP or servlets.
# Print to the file
Log4j.Appender.a1 = org.apache.log4j.rollingfileappender
Log4j.Appender.a1.file = f: /resin/resin-2.0.4/bak/sdcdcbak.log
Log4j.Appender.a1.maxfilesize = 1024KB
Log4j.Appender.a1.maxbackupindex = 100
Log4j.Appender.a1.Layout = Org.apache.log4j.patternlayout
Log4j.Appender.a1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N
# Print to the console
# log4j.appender.b1 = org.apache.log4j.consoleappender
# log4j.appender.b1.layout = org.apache.log4j.patternlayout
# log4j.Appender.b1.Layout.conversionPattern =% - 4R% -5P [% T]% 37C% 3X -% M% N
# log4j.Appender.b1.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N