Open In App

Servlet – Single Thread Model Interface

Improve
Improve
Like Article
Like
Save
Share
Report

Single Thread Model Interface was designed to guarantee that only one thread is executed at a time in a given servlet instance service method. It should be implemented to ensure that the servlet can handle only one request at a time. It is a marker interface and has no methods. Once the interface is implemented the system guarantees that there’s never more than one request thread accessing a single instance servlet. This interface is currently deprecated because this doesn’t solve 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 implemented the SingleThreadModel interface. That’s why to resolve the thread-safety issues it is recommended to use a synchronized block.

Syntax:

public class Myservlet  extends Httpservlet implements SingleThreadModel { 
}                                                                         

Implementation: SingleThreadModel interface

We created three files to make this application:

  1. index.html
  2. Myservlet.java
  3. web.xml

The index.html file creates a link to invoke the servlet of URL-pattern “servlet1” and Myservlet class extends  HttpServlet and implements the SingleThreadModel interface. Class Myservlet is a servlet that handles Single requests at a single time and sleep()  is a static method in the Thread class used to suspend the execution of a thread for two thousand milliseconds. When another user will try to access the same servlet, the new instance is created instead of using the same instance for multiple threads.

A. File: index.html

HTML




<!DOCTYPE html>
<html>
<head>
     <title>HttpSession Event Listeners</title>
</head>
<body>
     <a href="servlet1">open the servlet</a>
</body>
</html>


 

 

B. File: Myservlet.java

 

Java




// Java Program to Illustrate MyServlet Class
 
// Importing required classes
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
// Class
public class MyServlet
    extends HttpServlet implements SingleThreadModel {
 
    // Method
    // Use doGet() when you want to intercept on
    // HTTP GET requests
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException
    {
        // Sets the content type of the response being sent
        // to the client
        response.setContentType("text/html");
 
        // Returns a PrintWriter object that can send
        // character text to the client
        PrintWriter out = response.getWriter();
 
        out.print("welcome");
 
        // Try block to check for exceptions
        try {
            // Making thread to sleep for 2 seconds
            Thread.sleep(2000);
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
            // Display exception/s with line number
            e.printStackTrace();
        }
 
        out.print(" to servlet");
 
        // Closing the output stream
        // using close() method
        out.close();
    }
}


 

 

 
C. File: web.xml 

 

XML




<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 <servlet>
  <servlet-name>MyServlet</servlet-name>
    
  <servlet-class>MyServlet</servlet-class>
    
 </servlet>
 <servlet-mapping>
    
  <servlet-name>MyServlet</servlet-name>
    
  <url-pattern>/servlet1</url-pattern>
 </servlet-mapping>
   
 <welcome-file-list>
    
  <welcome-file>index.html</welcome-file>
    
 </welcome-file-list>
   
</web-app>


 

 

Output: When you run your index.html file, you’ll see the following results.

 

 

To get the output, click on the link.

 

Drawback of SingleThreadModel Interface

  • When there are thousands of concurrent requests to the web container the container may either serialize requests to the same instance of the servlet or create that many instances.
  • In the first case container’s ability to process concurrent requests will be severely hampered due to serialization.
  • In the later case, the container encounters more object allocation (more object creation overhead and memory usage).

 



Last Updated : 25 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads