Skip to main content

A Hit Count Using Java Servlet's Session Tracking

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTracker extends HttpServlet
{
        public void doGet(HttpServletRequest req,HttpServletResponse res)
        throws ServletException,IOException
        {
               res.setContentType("text/html");
               PrintWriter out=res.getWriter();
               HttpSession session=req.getSession(true);
               Integer count=(Integer)session.getValue("tracker.count");
               if(count==null)
               {
                        count=new Integer(1);
               }else{
                        count=new Integer(count.intValue()+1);
               }
               session.putValue("tracker.count",count);
               out.println("<HTML><BODY><H1>SESSION TRACKING DEMO</H1>");
               out.println("You've visited this page "+count+((count.intValue()==1)?" time.":"times."));
               out.println("<p>");
               out.println("<H2>Here is your session data:</H2>");
               String[] names=session.getValueNames();
               for(int i=0;i<names.length;i++)
               {
                       out.println(names[i]+": "+session.getValue(names[i])+"<BR>");
               }
      }
}

Comments

Post a Comment

Popular posts from this blog

Sample : String Reverse In Java

import java.io.*; class test {     public static void main(String args[])     {         DataInputStream in=new DataInputStream(System.in);         try         {             String text;             System.out.println("\nEnter The Text");             text=in.readLine();             System.out.println("\nIn Reverse Order::\n");             for(int i=text.length()-1;i>=0;i--)             {                 System.out.print(text.charAt(i));             }          ...

Getting Browser Information in javaScript

<html> <head> <script> function getBrowserName() { tag_link.innerHTML="You are surfing internet through  "+'<b>'+navigator.appName+'</b>'+" browser family." } </script> </head> <body onLoad="getBrowserName()"> <center> <p id="tag_link" style="font-size:30"></p> </center> </body> </html>