{"id":53,"date":"2009-09-15T22:22:43","date_gmt":"2009-09-15T22:22:43","guid":{"rendered":"http:\/\/localhost\/tfcom_wp\/2009\/09\/15\/hit-counter-servlet\/"},"modified":"2009-09-15T22:22:43","modified_gmt":"2009-09-15T22:22:43","slug":"hit-counter-servlet","status":"publish","type":"post","link":"https:\/\/www.tech-freaks.com\/java\/jsp-servlets\/hit-counter-servlet.html","title":{"rendered":"Simple Hit Counter Servlet"},"content":{"rendered":"<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Problem Statement:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Write a simple servlet which counts the total number of hits on itself and display it in the web page.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Assumptions:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">1.The web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">2.The servlet is not loaded in a distributed environment.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">We will be using this example to explain certain key concepts of servlet technology. We will also explain what will happen if the above assumption is not in place.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">The below code shows a servlet which will display the number of hits on it.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Below is the excerpt from web.xml which shows the servlet registration and servlet URL mapping.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">We should be able to access the servlet using the below URL:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">http:\/\/localhost:8080\/&lt;contextName&gt;\/servlet\/SimpleCounterServlet<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Everytime the above URL is access, the hit counter shows an incremented value. You may like to try by closing the browser window and accessing from a different browser window.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Key Servlet Concept:<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">We were able to achieve the counter functionality using a single instance variable. Why?<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">1.Only one instance of the servlet is created.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">2.Init() method is called only once. This explains the reason why the counter is not reset to 0 with every hit.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">What will happen if the assumptions are removed?<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">1.If the web container is restarted, the old servlet instance will be destroyed. New servlet instance will be instantiated and init() will be called. Thus, we will lose the counter is the old servlet.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">2.In a distributed environment, there may be multiple JVMs. For each node in which the web container is distributed, a different servlet instance may be created. Hence, the instance variable approach will fail.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">For fixing the first issue, we could modify the servlet code to persist the hitCounter into a database or file. The init() would be loading the hitCounter from the file\/database.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">For fixing the second issue, we need to stored the hitCounter variable somewhere else&#8230; well, that somewhere is &#8216;ApplicationScope&#8217;.<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\"><\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden; text-align: left;\">Try the above fixes as &#8216;Home Work&#8217;!<\/div>\n<p style=\"text-align: left;\"><strong>Problem Statement<\/strong><\/p>\n<p style=\"text-align: left;\">Write a simple servlet which counts the total number of hits on itself and display it in the web page.<\/p>\n<p style=\"text-align: left;\"><span class=\"Apple-style-span\" style=\"font-weight: bold;\">Assumptions<\/span><\/p>\n<ul style=\"text-align: left;\">\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">The web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.<\/span><\/li>\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">The servlet is not loaded in a distributed environment.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: left;\">We will be using this example to explain certain key concepts of servlet technology. We will also explain what will happen if the above assumption is not in place.<\/p>\n<p style=\"text-align: left;\">The below code shows a servlet which will display the number of hits on it.<\/p>\n<pre class=\"language-java\"><code>package in.techfreaks.servlet;\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n\r\npublic class SimpleCounterServlet extends HttpServlet {\r\n\r\n\t\/\/Instance variable used for counting hits on this servlet\r\n\tprivate int iHitCounter;\r\n\r\n\t\/**\r\n\t* init method just initializes the hitCounter to zero\r\n\t*\/\r\n\r\n\tpublic void init() throws ServletException {\r\n\t\tiHitCounter = 0;\r\n\t}\r\n\r\n\t\/**\r\n\t* Work horse of this servlet\r\n\t* Displays a welcome msg along with hitCounter\r\n\t* Increments the hitCounter\r\n\t*\/\r\n\r\n\tpublic void doGet(HttpServletRequest request, HttpServletResponse response)\r\n\tthrows ServletException, IOException {\r\n\r\n\tPrintWriter out =  response.getWriter();\r\n\t\tout.println(\"&lt;h2&gt;Welcome to SimpleCounterServlet.java&lt;\/h2&gt;\");\r\n\t\tout.println(\"Hits on this servlet so far: \"+ (++iHitCounter)); \r\n\t}\r\n\r\n\t\/**\r\n\t* Passes the call to doGet method. \r\n\t* Thus, works similar to doGet\r\n\t*\/\r\n\r\n\tpublic void doPost(HttpServletRequest request, HttpServletResponse response)\r\n\tthrows ServletException, IOException {\r\n\t\tdoGet(request, response);\r\n\t}\r\n}<\/code><\/pre>\n<p style=\"text-align: left;\">Below is the excerpt from web.xml which shows the servlet registration and servlet URL mapping.\u00a0\u00a0<\/p>\n<pre class=\"language-markup\"><code>  ...\r\n  &lt;servlet&gt;\r\n    &lt;servlet-name&gt;SimpleCounterServlet&lt;\/servlet-name&gt;\r\n    &lt;servlet-class&gt;in.techfreaks.servlet.SimpleCounterServlet&lt;\/servlet-class&gt;\r\n  &lt;\/servlet&gt;\r\n  ...\r\n  &lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;SimpleCounterServlet&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;\/servlet\/SimpleCounterServlet&lt;\/url-pattern&gt;\r\n  &lt;\/servlet-mapping&gt;\r\n  ....<\/code><\/pre>\n<div style=\"text-align: left;\">We should be able to access the servlet using the below URL:<\/div>\n<p style=\"text-align: left;\">http:\/\/localhost:8080\/&lt;contextName&gt;\/servlet\/SimpleCounterServlet<\/p>\n<p style=\"text-align: left;\">Everytime the above URL is access, the hit counter shows an incremented value. You may like to try by closing the browser window and accessing from a different browser window.<\/p>\n<p style=\"text-align: left;\"><strong>Key Servlet Concept<\/strong><\/p>\n<p style=\"text-align: left;\">We were able to achieve the counter functionality using a single instance variable. Why?<\/p>\n<ul style=\"text-align: left;\">\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">Only one instance of the servlet is created<\/span><\/li>\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">init() method is called only once. This explains the reason why the counter is not reset to 0 with every hit<\/span><\/li>\n<\/ul>\n<p style=\"text-align: left;\"><span class=\"Apple-style-span\" style=\"font-weight: bold;\">What will happen if the assumptions are removed?<\/span><\/p>\n<ul style=\"text-align: left;\">\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">If the web container is restarted, the old servlet instance will be destroyed. New servlet instance will be instantiated and init() will be called. Thus, we will lose the counter is the old servlet.<\/span><\/li>\n<li><span class=\"Apple-style-span\" style=\"line-height: 15px;\">In a distributed environment, there may be multiple JVMs. For each node in which the web container is distributed, a different servlet instance may be created. Hence, the instance variable approach will fail.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: left;\">For fixing the first issue, we could modify the servlet code to persist the hitCounter into a database or file. The init() would be loading the hitCounter from the file\/database.<\/p>\n<p style=\"text-align: left;\">For fixing the second issue, we need to stored the hitCounter variable somewhere else&#8230; well, that somewhere is &#8216;ApplicationScope&#8217;.<\/p>\n<p style=\"text-align: left;\">Try the above two fixes for self study and post your solution in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: Write a simple servlet which counts the total number of hits on itself and display it in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[10],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-jsp-servlets"],"_links":{"self":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-freaks.com\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}