Thursday, April 7, 2016

Get Number of Website Visitors and Their Details:

Index.jsp:

<%@page import="java.sql.*"%>
<%@page import="java.io.*, java.util.Date"%>

<!DOCTYPE html>
<html lang="hi">
<head>  
    <title>Grab Visitor detail</title>
   <meta charset="utf-8"> //below are the bootstrap included path.
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 
  <link rel="stylesheet" href="css/ezomart.css" type="text/css">
 <link rel="icon" type="image/png" href="images/favicon.png" sizes="160x160">

// jsp code to get Ip of user and current date and time when user opens this page.

<%
    Integer hitsCount = (Integer) application.getAttribute("hitCounter"); 
    if( hitsCount ==null || hitsCount == 0 ){    
       /* First visit */    // setting counter (1...n) it will increase when ever a new visit to page happens
             hitsCount = 1;
    }else{
       /* return visit */
              hitsCount += 1;
    }
// setting count attribute and getting IP, HOST of user.
    application.setAttribute("hitCounter", hitsCount);
   String ipAddress = request.getRemoteAddr();
   String host=request.getRemoteHost();
  application.setAttribute("IP", ipAddress);
  application.setAttribute("host", host);

  Date d=new Date();  // Getting date
  String datee=d.toString();  // converting date to string
    Connection con=null;
Statement st=null;
ResultSet rs=null;   
 // start opening DB and insert count IP and date into DB for saving visitor record.  
         try{                           
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
         con=DriverManager.getConnection("jdbc:odbc:DB_NAME");
   st=con.createStatement();
 // storing visitor record to DB
 String sql="insert into visits values ('"+hitsCount+"','"+ipAddress+"','"+datee+"')";         
    
    int x=st.executeUpdate(sql);
   if(x!=0)
    {
                 
   }
                       else{
 response.sendRedirect("load_error.jsp"); // below is load_error.jsp
                       }

             con.close();
        st.close();}
         catch(SQLException e)
                                 { 
         response.sendRedirect("load_error.jsp");
         }
    %>

</head>
<body>

Akshay Morkhandikar Welcome's you.
have nice day.

</body>
</html>

load_error.jsp:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Error 500</h1>
        <p>please try again <a href="index.jsp">Home</a></p>
    </body>
</html>

No comments:

Post a Comment