Servlet – Single Thread Model Interface
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:
- index.html
- Myservlet.java
- 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
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet
extends HttpServlet implements SingleThreadModel {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.print( "welcome" );
try {
Thread.sleep( 2000 );
}
catch (Exception e) {
e.printStackTrace();
}
out.print( " to servlet" );
out.close();
}
}
|
C. File: web.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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
Share your thoughts in the comments
Please Login to comment...