Java Blog

 
 
 
----------------------------------------------------------------------------------
                    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.*;

public class SessionServlet extends HttpServlet
{

    public void 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[] = new int[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>\");        
        }
      }
}