Problem Statement:
Write a simple servlet which counts the total number of hits on itself and display it in the web page.
Assumptions:
1.The web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.
2.The servlet is not loaded in a distributed environment.
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.
The below code shows a servlet which will display the number of hits on it.
Below is the excerpt from web.xml which shows the servlet registration and servlet URL mapping.
We should be able to access the servlet using the below URL:
http://localhost:8080/<contextName>/servlet/SimpleCounterServlet
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.
Key Servlet Concept:
We were able to achieve the counter functionality using a single instance variable. Why?
1.Only one instance of the servlet is created.
2.Init() method is called only once. This explains the reason why the counter is not reset to 0 with every hit.
What will happen if the assumptions are removed?
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.
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.
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.
For fixing the second issue, we need to stored the hitCounter variable somewhere else... well, that somewhere is 'ApplicationScope'.
Try the above fixes as 'Home Work'!

Problem Statement

Write a simple servlet which counts the total number of hits on itself and display it in the web page.

Assumptions

  • The web container will not be restarted. If it is restarted or servlet destroyed, the hit counter will be reset.
  • The servlet is not loaded in a distributed environment.

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.

The below code shows a servlet which will display the number of hits on it.

package in.techfreaks.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleCounterServlet extends HttpServlet {

	//Instance variable used for counting hits on this servlet
	private int iHitCounter;

	/**
	* init method just initializes the hitCounter to zero
	*/

	public void init() throws ServletException {
		iHitCounter = 0;
	}

	/**
	* Work horse of this servlet
	* Displays a welcome msg along with hitCounter
	* Increments the hitCounter
	*/

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

	PrintWriter out =  response.getWriter();
		out.println("<h2>Welcome to SimpleCounterServlet.java</h2>");
		out.println("Hits on this servlet so far: "+ (++iHitCounter)); 
	}

	/**
	* Passes the call to doGet method. 
	* Thus, works similar to doGet
	*/

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		doGet(request, response);
	}
}

Below is the excerpt from web.xml which shows the servlet registration and servlet URL mapping.  

  ...
  <servlet>
    <servlet-name>SimpleCounterServlet</servlet-name>
    <servlet-class>in.techfreaks.servlet.SimpleCounterServlet</servlet-class>
  </servlet>
  ...
  <servlet-mapping>
    <servlet-name>SimpleCounterServlet</servlet-name>
    <url-pattern>/servlet/SimpleCounterServlet</url-pattern>
  </servlet-mapping>
  ....
We should be able to access the servlet using the below URL:

http://localhost:8080/<contextName>/servlet/SimpleCounterServlet

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.

Key Servlet Concept

We were able to achieve the counter functionality using a single instance variable. Why?

  • Only one instance of the servlet is created
  • init() method is called only once. This explains the reason why the counter is not reset to 0 with every hit

What will happen if the assumptions are removed?

  • 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.
  • 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.

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.

For fixing the second issue, we need to stored the hitCounter variable somewhere else... well, that somewhere is 'ApplicationScope'.

Try the above two fixes for self study and post your solution in the comments below.

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.