Home Jsp Record user login and logout timing In JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Record user login and logout timing In JSP

Advertisement
In this section, we have developed a web application of maintain record of the user Login and Logout Time in JSP.

Record user login and logout timing In JSP

     

In this section, we have developed a web application of maintain record of the user Login and Logout Time  in JSP. Six files are used  "home.jsp","login.jsp", "userloginmid.jsp", "welcome.jsp","logout.jsp" and "userstatus.jsp" in the code given below.

Brief description of the flow of application :

1). Create a webpage "home.jsp"  to login the user and show the user login status link. 

2). Create a webpage "login.jsp"  to login the user. 

3). Create a webpage "welcome.jsp"  to display a message after successful  login.

4). Create a webpage  "userstatus.jsp" to display all the Login and Logout User status.

Step:1 Create Table for Database :

"userregister"

 create table `userregister` (
 `id` double ,
 `username` varchar (50),
 `password` varchar (50),
 `first_name` varchar (50),
 `last_name` varchar (50),
 `city` varchar (50),
 `state` varchar (50),
 `country` varchar (50)
 );

"admin"

 create table `admin` (
 `id` double ,
 `userid` double ,
 `intime` datetime ,
 `outtime` datetime 
 ); 

 

Step:2 Create a web page  "home.jsp"  to login  and display the user status link.

<HTML>
 <HEAD>
  <TITLE>Login Application</TITLE>
  </HEAD>
  <BODY>
  <br><br><br><br><br>
  <table align="center">
  <%
  if(session.getAttribute("username")!=null && session.getAttribute("username")!="")
  
  %>
  <tr><td align="left"><a href="logout.jsp">Click Here to Logout</a></td></tr>
  <%
  }
  else
  {
  %>
  <tr><td align="left"><a href="login.jsp">Click Here to User Login</a></td></tr>
  <%
  }
  %>
  <tr><td align="left">&nbsp;</td></tr>
  <tr><td align="left"><a href="userstatus.jsp">View Users Logon status</a></td></tr>
  </table>
  </BODY>
</HTML>

 Step:3 To create a "login.jsp" to login the user

<html>
<head>
</head>
<body>
<br><br><br>
<table align="center" width="400px" border=0>
<tr><td align="right" width="300"><a href="home.jsp">Home</a></td></td></tr>
</table>
<table align="center"><tr><td colspan=2><h1>User Login</h2></td></tr></table>
<form name="loginform" method="post" action="userloginmid.jsp">
<table align="center" width="300px" style="border:1px solid #000000;" >
  <tr><td colspan=2>&nbsp;</td></tr>
  <tr>
  <td><b>Login Name</b></td>
  <td><input type="text" name="userName" value=""></td>
  </tr>
  <tr>
  <td><b>Password</b></td>
  <td><input type="password" name="password" value=""></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
</table>
</form>
</body>
</html>

 
 Step:4 To create a "welcome.jsp" to display the message after succesful login the user.

<%page import="java.sql.*,java.util.*" %>
<html>
<body>
<br><br>
<table align="center" width="300px" border=0>
<%
String username="";
if(session.getAttribute("username")!=null && session.getAttribute("username")!="")
{
username=session.getAttribute("username").toString();
%>
  <tr>
 <td align="right" width="690"><a href="home.jsp">Home</a></td>
 <td align="right"><a href="logout.jsp">Logout</a></td>
  </tr>
<%
}
else
{
%>
  <tr>
 <td align="right"><a href="home.jsp">Home</a></td>
 <td align="right"><a href="login.jsp">Login</a></td>
  </tr>
<%
}
%>
<tr><td colspan=2>&nbsp;</td></tr>
<tr><td colspan=2><h1>Welcome <%=username%></h1></td></tr>
<table>
</body>
</html>


 Step:5 Create a  webpage  "userstatus.jsp"  to display the User Status .

<%page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<html>
<head>
<TITLE>Login Application</TITLE>
</head>
<body>
<br><br>
<table align="center" width="400px" border=0>
  <%
  if(session.getAttribute("username")!=null && session.getAttribute("username")!="")
  {
  %>
  <tr>
  <td align="right" width="350"><a href="home.jsp">Home</a></td>
  <td align="right"><a href="logout.jsp">Logout</a></td>
  </tr>
  <%
  }
  else
  {
  %>
  <tr>
  <td align="right" width="350"><a href="home.jsp">Home</a></td>
  <td align="right"><a href="login.jsp">Login</a></td>
  </tr>
  <%
  }
  %>
</table>
<table width="400px" align="center" border=0
  <tr style="background-color:#D9B0D8;">
  <td align="center"><b>User Id</b></td>
  <td align="center"><b>Username</b></td>
  <td align="center"><b>Login</b></td>
  <td align="center"><b>Logout</b></td>
  </tr> 
<%
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "user_register";
String driver = "com.mysql.jdbc.Driver";
String username = "root"
String userPassword = "root";
String user = "";
try 
{
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,username,userPassword);
  Statement st = conn.createStatement();
  Statement st1 = conn.createStatement();
  String queryString = "select * from admin order by userid";
  ResultSet rs = st.executeQuery(queryString);
  ResultSet rs1=null;
  String queryStringuser="";
  SimpleDateFormat sdfDestination=null;
  int count=0;
  String strcolor = "";
  while(rs.next())
  {
  count = count%2;
  if(count==0)
  {
  strcolor = "#D9D9D9";
  }
  else
 {
  strcolor = "#EFEFEF";
  }
  count++;
  queryStringuser = "select * from userregister where id="+rs.getInt(2);
  rs1 = st1.executeQuery(queryStringuser);
  while(rs1.next())
  {
  user  = rs1.getString(2);
  }
 String inStr="";
  String outStr="";
  java.util.Date date;
  String intime="";
  String outtime="";
  if((rs.getString(3)!=null && rs.getString(3)!=""))
  {
  inStr = rs.getString(3).toString().substring(0,rs.getString(3).toString().indexOf("."));
  try
 {
 date = new  SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(inStr);
  sdfDestination = new SimpleDateFormat("MMM dd, hh:mm:ss");
  intime = sdfDestination.format(date);
  }
  catch (Exception e)
  {
  e.getMessage();
  }
  }

  if((rs.getString(4)!=null && rs.getString(4)!=""))
  {
  outStr = rs.getString(4).toString().substring(0,rs.getString(3).toString().indexOf("."));
  try
  {
  date = new  SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(outStr);
  sdfDestination = new SimpleDateFormat("MMM dd, hh:mm:ss");
  outtime = sdfDestination.format(date);
  }
  catch (Exception e)
  {
  e.getMessage();
  }
  }
  %>
  <tr style="background-color:<%=strcolor%>;">
   <td align="center"><%=rs.getInt(2)%></td>
   <td align="left" style="padding-left:10px;"><%=user%></td>
   <td align="center"><%=intime%></td><td align="center"><%=outtime%></td>
   </tr>
  <%
  }
  conn.close();

catch (Exception e)
{
  e.printStackTrace();
}
%> 

Output:

Login Authentication form :

After Successful Login user :

Status of all the User

Download the full code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Ask Questions?    Discuss: Record user login and logout timing In JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Charle Marcelo
April 8, 2011
Database Connection

I had problems in running this program because when the time that I will login, it displayed a blank page and it shows this message in console: MySQL Connect Example. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1493) at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128) at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.apache.jsp.userloginmid_jsp._jspService(userloginmid_jsp.java:80) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)
prashanth
April 17, 2011
unable to create admin table

unable to create admin table since it is giving error in datetime datatypye....can i get the query to crete the admin table mentioned above in this page
Navdeep Khandelwal
May 30, 2013
About Login /Logout System

i have a table in which i store login and logout time of a employee but i also want to calculate the total time between login and logout time in different column .can you help me to out from this problem
pratik bhatt
February 20, 2013
To say Thank you

Really a nice example and very helpful to me...
Madhura
February 26, 2013
JSP

i din't get action="userloginmid.jsp in login.jsp. where will login.jsp be redirected?
Mohd Imran
March 17, 2012
Record user login and logout timing In JSP

I little bit confused in this program, problem is that which database language I use. You Written following code - Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,username,userPassword); Only I have to replace this connection code to used database connection code, Am I right, Please Help I am a biggner of JSP.
Rashid
May 9, 2012
Thanks

this code is work very nice .. thanks dear
muhammad
June 4, 2012
html and jsp

hi der! i have an assignment and its very hard to understand please help; i have been asked to design a web application with the following pages, login.jsp, main page, form.jsp and display page.
Warrior
September 3, 2012
Y it's not working properly.

from the code "userloginmid.jsp",it's not properly completed. that's the problem. Plz gave complete code.
neel
March 28, 2013
where is userloginmid.jsp"???

where is userloginmid.jsp ????
neel
March 28, 2013
getting blanl

when clicking on submit a blanl page is cu,i with userlogin.jsp in address bar
manjunath
May 9, 2011
jsp and postgresql

jsp userauthentication with postgresql
DMCA.com