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:
Mapping of Servlet ("CounterServlet.java") in web.xml file
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:
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial