Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Hit Counter Servlet Example 
 

This example illustrates about counting how many times the servlet is accessed.

 

Hit Counter Servlet Example

                         

This example illustrates about counting how many times the servlet is accessed. When first time servlet (CounterServlet) runs then session is created and value of the counter will be zero and after again accessing of servlet  the counter value will be increased by one. In this program isNew() method is used whether session is new or old and getValue() method is used to get the value of counter.

 

 

 

 

Here is the source code of CounterServlet.java:

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

public class CounterServlet extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws 
ServletException, IOException {
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Integer count = new Integer(0);
    String head;
    if (session.isNew()) {
      head = "This is the New Session";
    else {
      head = "This is the old Session";
      Integer oldcount =(Integer)session.getValue("count")
      if (oldcount != null) {
        count = new Integer(oldcount.intValue() 1);
      }
    }
    session.putValue("count", count);
    out.println("<HTML><BODY BGCOLOR=\"#FDF5E6\">\n" +
      "<H2 ALIGN=\"CENTER\">" + head + "</H2>\n" "<TABLE BORDER=1 ALIGN=CENTER>\n"
      "<TR BGCOLOR=\"#FFAD00\">\n" 
      +"  <TH>Information Type<TH>Session Count\n" 
        +"<TR>\n" +" <TD>Total Session Accesses\n" +
      "<TD>" + count + "\n" +
      "</TABLE>\n" 
      +"</BODY></HTML>" );
  }
}

Mapping of Servlet ("CounterServlet.java") in web.xml file

<servlet>
    <servlet-name>CounterServlet</servlet-name>
    <servlet-class>CounterServlet</servlet-class>
</servlet> 
<servlet-mapping>
    <servlet-name>CounterServlet</servlet-name>
    <url-pattern>/CounterServlet</url-pattern>
</servlet-mapping>

Running the servlet by this url: http://localhost:8080/CodingDiaryExample/CounterServlet  displays the figure below:

When servlet is hit two times by the user the counter value will be increased by two as shown in figure below:

Download Source Code

                         

» View all related tutorials
Related Tags: c database error data input servlet user screen display name this message ai tab check if example with exists to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

hi

can any one help me in this.. i have a html/jsp form in that i have a field .. enter password... first time password will be stored in the databse... from second time when user enters passowrd wrongly for 3 times then it has to dispay some msg contact ur administrator. can any one send me this code? plzzzzzzzzzzzzzzzz send it ASAp

Posted by hafij on Friday, 11.14.08 @ 04:42am | #81672

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.