<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">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.Following are the steps to be taken to implement a simple page hit counter which is based on Servlet Life Cycle:</p>
<ul style="text-align: left;">
<li>Initialize a global variable in init() method.</li>
<li>Increase global variable every time either doGet() or doPost() method is called.</li>
<li>If required, you can use a database table to store the value of global variable in destroy() method. This value can be read inside init() method when servlet would be initialized next time. This step is optional.</li>
<li>If you want to count only unique page hits with-in a session then you can use isNew() method to check if same page already have been hit with-in that session. This step is optional.</li>
<li>You can display value of the global counter to show total number of hits on your web site. This step is also optional.</li>
</ul>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>CounterServlet.java</b></p>
<pre class="highlight">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 Countn" 
 +"<;TR>;n" +" <;TD>;Total Session Accessesn" + 
 "<;TD>;" + count + "n" + 
 "<;/TABLE>;n" 
 +"<;/BODY>;<;/HTML>;" ); 
 } 
} 
</pre>
<p><b>Mapping of Servlet (&#8220;CounterServlet.java&#8221;) in web.xml file</b></p>
<pre class="highlight"><;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>; 
</pre>
<p>Running the servlet by this url:<br />
http://localhost:8080/CounterServlet<br />
displays the figure below:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-21.png" border="0" /></div>
<p>When servlet is hit six times by the user the counter value will be increased by six as shown in figure below:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-22.png" border="0" /></div>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servlet-upload-file-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/servlets-auto-page-refresh/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/servlet-upload-file-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/servlets-auto-page-refresh/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '255'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…