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>\");        
        }
      }
}

    Related Post:
  1. Write a class whose objects holds a current value and have a method to add that value, printing the new value

  2. Program which simulate the dictionary in java

  3. Corba program to Write a Echo server and client with UDP server and client

  4. Program to copy the elements of an array into another array in reverse order and print them

  5. Program that reads and writes random access file

  6. Servlet which takes input from an HTML client page and calculates and outputs the premium amount

  7. Program to show the use of Logical OR Operator (|) in If Statements

  8. Program to show the use of If Statement

  9. Program to show an example of using a recursive method in a class

  10. An applet program to display sum of two number of textField

  11. Corba program of date operations

  12. Corba program of Count Client Applet, IDL,Server, Client

  13. An applet program to give demo of getDocumentBase() and getCodeBase() methods

  14. Develop a game application in CORBA for which the player will guess a number between 1 to 100, which will be compared to the random number generated b

  15. An applet program that displays Text at the center of the string which is passed as parameter

  16. Program of inventory management and demonstrating interactive input and output

  17. Program to show the use of Bitwise Operator (&) AND

  18. Program of FTP client and server

  19. Program to compute the sum of the digits of a given integer number. eg: I/o=4 then O/p=4+3+2+1=10

  20. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class