SingleThreadModel Interface


The servlet programmer needs to enforce SingleThreadModel interface to make sure that servlet can manage most effective requests one at a time. It is a marker interface, method haven't any methods.

This interface is presently deprecated for the reason that Servlet API 2.4 as it doesn't solves all of the thread-protection problems including static variable and consultation attributes may be accessed through more than one threads on the equal time even though we've got applied the SingleThreadModel interface. So it's miles encouraged to apply different approaches to solve those thread protection problems including synchronized block etc.

SingleThreadModel Interface Example

Implementing the SingleThreadModel Interface simply in the below given Example.

import java.io.IOException;  
import java.io.PrintWriter;  
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");  

download this example (developed using Myeclipse IDE)

download this example (developed usingEclipse IDE)

download this example (developed using Netbeans IDE)