Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Remote Method InvocationRSS Feeds

Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object

Posted By: Finlay Evans     Category: Java     Views: 4217

Write a Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object.It should also display all other current sessions by showing their session Ids and number of times they have visited that page.

Code for Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object in Java

----------------------------------------------------------------------------------
                    Session.html
----------------------------------------------------------------------------------

<html>
    <head Session Details>
        <title Session Details>
        </title>
    </head>
    
    <body bgcolor=pink>
        <form name="f1" method="GET" action="/Prac7/try2.do">
            <BR><h1>Session Details</h1><br><br>

            Enter The Name of User : 
            <input type=text name=user>

            <br><br>
            <input type=submit value=SUBMIT>
        </form>
    </body>
</html>


-------------------------------------------------------------------------------------------------
                    Session.java
-------------------------------------------------------------------------------------------------

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.regex.*;

publicclass SessionServlet extends HttpServlet
{

    publicvoid doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException
      {
        HttpSession hs = req.getSession(true);
            PrintWriter out = res.getWriter();
            res.setContentType("text/html");

        String row;

        File file = new File("Session.txt");
        RandomAccessFile raf = new RandomAccessFile(file,"rw");


        String sessionId = hs.getId();
        if (sessionId != null) 
        {
             out.println("Session Id of User : " + sessionId + "<br>");
        }
        else 
        {
            hs.setAttribute("sessionId", "999");
        }
        
        String user = req.getParameter("user");
        out.println("user : " + user + "<br>");    
        if (user != null) 
        {
             out.println("Name of User : " + user + "<br>");
        }
        else 
        {
            hs.setAttribute("user", "ses_user");
        }
        
        Date date = new Date();
            out.println("Current Date: " + date + "<br>");

        row = sessionId + "/" + user + "/" + date.toString();
        raf.seek(file.length());
        raf.writeBytes(row);
        raf.writeBytes("\n");

        String s;        
        String sid[] = new String[10];
        String sname[] = new String[10];
        String sdate[] = new String[10];
        int scnt[] = newint[10];
        int cnt=0,flag=0,i;

        raf.seek(0);
        while((s = raf.readLine()) != null)
        {
            flag=0;
            Pattern pat = Pattern.compile("[/]");
            String strs[] = pat.split(s);
            
            if(cnt==0)
            {
                sid[cnt] = strs[0];
                sname[cnt] = strs[1];
                sdate[cnt] = strs[2];
                cnt++;
            }

            for(i=0;i<cnt;i++)
            {
                if(strs[1].equals(sname[i]))
                {
                    scnt[i]++;
                    flag=1;
                }        
            }
            if(flag==0)
            {
                sid[cnt] = strs[0];
                sname[cnt] = strs[1];
                sdate[cnt] = strs[2];
                cnt++;
            }
        }

        raf.close();

        out.println("<br><br><br>");                            
        out.println("Session Count Details : " + "<br><br>");
        for(i=0;i<cnt;i++)
        {
            scnt[i] += 1;
            out.println("Session id : " + sid[i] + "<br>");                    
            out.println("Session user name : " + sname[i] + "<br>");                    
            out.println("Last Accessed Date : " + sdate[i] + "<br>");
            out.println("No of times Site Visisted : " + scnt[i] + "<br>");    
            out.println("<br>");        
        }
      }
}
  
Share: 



Finlay Evans
Finlay Evans author of Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!