The servlet programmer should implement SingleThreadModel interface to ensure that servlet can handle only one request at a time. It is a marker interface, means have no methods.

This interface is currently deprecated since Servlet API 2.4 because it doesn’t solves all the thread-safety issues such as static variable and session attributes can be accessed by multiple threads at the same time even if we have implemented the SingleThreadModel interface. So it is recommended to use other means to resolve these thread safety issues such as synchronized block etc.

Example of SingleThreadModel interface

Let’s see the simple example of implementing the SingleThreadModel interface.

import javax.servlet.ServletException;  
import javax.servlet.SingleThreadModel;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class MyServlet extends HttpServlet implements SingleThreadModel{  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
    PrintWriter out = response.getWriter();  
    try{Thread.sleep(10000);}catch(Exception e){e.printStackTrace();}  
    out.print(" to servlet");