Log User Activities
Java-Log4j - HTML - Apache - Eclipse
Logging example:
22-06-2013 13:53:32 INFO LogInController:33 - 2K8CSE logs in. 22-06-2013 13:54:02 ERROR LogInController:40 - 2K8CSE failed to log in. 22-06-2013 13:54:16 ERROR LogInController:40 - ABCD failed to log in.
Screen shots:
After successful Log in:
Welcome to home page.
Steps:
1. Add log4j-1.2.16 jar file to the project.
2. Add log4j.properties to the project.
# Root logger option log4j.rootLogger=INFO, file, stdout # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\Users\\nn\\Desktop\\nnEclipse4\\UserAuditExample\\systemlog.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n3. In LogInController (controller, servlet) add the following code in constructor.
Logger log = Logger.getLogger(LogInController.class);
4. Add the following code to log the information to the log file.
log.info(message); eg: log.info(username+" logs in."); log.error(username+" failed to log in.");
Project Explorer:
login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Log in</title> <script type="text/javascript"> function validateForm() { var x=document.getElementById("username"); if (x.value=="") { document.getElementById('username_innermsg').innerHTML="Please enter the User Name."; x.focus(); return false; } document.getElementById('username_innermsg').innerHTML=''; var x=document.getElementById("paswd"); if (x.value=="") { document.getElementById('password_innermsg').innerHTML="Please enter the Password."; x.focus(); return false; } document.getElementById('password_innermsg').innerHTML=''; } function resetInnerHtmlMsgs() { document.getElementById('username_innermsg').innerHTML=''; document.getElementById('password_innermsg').innerHTML=''; } </script> </head> <body> <center> <h1>Log in</h1> <form action="LogInController" method="post" onsubmit="return validateForm()"> <table> <tr> <td>User Name :</td><td><input type="text" name="username" id="username"></td><td width="200px"> <i style="color: red;" id="username_innermsg"></i></td> </tr> <tr> <td>Password :</td><td><input type="password" name="paswd" id="paswd"></td><td width="200px"> <i style="color: red;" id="password_innermsg"></i></td> </tr> <tr><td><input type="submit" value="Login"></td><td><input type="reset" value="Cancel" onclick="resetInnerHtmlMsgs()"/></td></tr> </table> </form> </center> </body> </html>
LogInController.java
package com.logexample.controller; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.logexample.model.LogIn; @WebServlet("/LogInController") public class LogInController extends HttpServlet { private static final long serialVersionUID = 1L; private Logger log; public LogInController() { super(); log = Logger.getLogger(LogInController.class); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LogIn login= new LogIn("2K8CSE", "Password@123"); RequestDispatcher rd= null; String username= request.getParameter("username"); String password=request.getParameter("paswd"); if(username.equals(login.getUsername())&&password.equals(login.getPassword())) { log.info(username+" logs in."); rd=request.getRequestDispatcher("success.html"); rd.forward(request, response); return; } else { log.error(username+" failed to log in."); rd=request.getRequestDispatcher("error.html"); rd.forward(request, response); return; } } }
Login.java
package com.logexample.model; public class LogIn { private String username; private String password; public LogIn(String username, String password) { super(); this.username = username; this.password = password; } public LogIn() { super(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
success.html
<html> <head> <title>Home</title> </head> <body> <center><br/><br/> <h2>Welcome to home page.</h2> </center> </body> </html>
Download: UserAuditExample