Logo 
Search:

OOAD Articles

Submit Article
Home » Articles » OOAD » Servlet RSS Feeds

Program using Servlet to show how many times current page has been accesses and what is the current time at server side as well as at client side

Posted By: Milind Mishra     Category: OOAD     Views: 7735

Program using Servlet to show how many times current page has been accesses and what is the current time at server side as well as at client side.

Code for Program using Servlet to show how many times current page has been accesses and what is the current time at server side as well as at client side in OOAD

// countaccess.html

<html>
<body>
<form action="http://localhost:8080/examples/servlet/countAccess" method="POST">
<input type="hidden"value='<%=new java.util.Date()%>' name="clientDate">
<input type=submit value= submit>
</form>
</body>
</html>


countAccess.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
publicclass countAccess extends HttpServlet
{

    privateint accessCount = 0;

  protected synchronized void IncrementCount() {
    accessCount++;
  }

  protected synchronized int getCount() {
      return accessCount;
  }

  publicvoid doPost(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
    IncrementCount(); 
        PrintWriter out= res.getWriter();
   try
    {
        res.setContentType("text/html");
    Date serverDate=new Date();
    String clientDateStr = req.getParameter("clientDate");

    out.println("<P><B>Server Side Date and time in millisecond= "+serverDate);
    out.println("<P><B>Client Side Date and time in millisecond= "+clientDateStr);
       out.println("<P><B>Page Accesed "+getCount()+" Times");
 
   }
    catch(Exception e)
    {
        System.out.println(e);        
    }
  }
}


// OUTPUT

Server Side Date and time in millisecond= Sun Dec 11 09:25:10 IST 2005 
Client Side Date and time in millisecond= Sun Dec 11 09:25:06 IST 2005 
Page Accesed 1 Time 


Server Side Date and time in millisecond= Sun Dec 11 09:25:35 IST 2005 
Client Side Date and time in millisecond= Sun Dec 11 09:25:31 IST 2005 
Page Accesed 2 Times
  
Share: 



Milind Mishra
Milind Mishra author of Program using Servlet to show how many times current page has been accesses and what is the current time at server side as well as at client side is from India.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in OOAD:


 
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!