Back to: Java Servlets Tutorials
Java Servlet Interface with Examples
In this article, I am going to discuss Java Servlet Interface with Examples. Please read our previous article where we discussed the Java Servlet API. At the end of this article, you will understand the following pointers.
- Java Servlet Interface
- Servlet Interface Methods
- 1. init() method
- 2. service() method
- 3. getServletConfig() method
- 4. getServletInfo() method
- 5. destroy() method
- Example: To understand the Java Servlet Interface
Java Servlet Interface
public interface Servlet
This interface is for developing servlets. A servlet may be a body of Java code that’s loaded into and runs inside a servlet engine, as an internet server. It receives and responds to requests from clients. For example, a client may have information from a database; a servlet is often written that receives the request, gets and processes the info as required by the client, and then returns it to the client.
All servlets implement this interface. Servlet writers typically do that by subclassing either GenericServlet, which implements the Servlet interface, or by subclassing GenericServlet’s descendent, HttpServlet. Developers got to directly implement this interface as long as their servlets cannot (or choose not to) inherit from GenericServlet or HttpServlet.
The Servlet interface defines methods to initialize a servlet, to receive and answer client requests, and to destroy a servlet and its resources. These are referred to as life-cycle methods, and are called by the network service within the following manner:
- Servlet is created then initialized.
- Zero or more service calls from clients are handled
- Servlet is destroyed then it is garbage collected and finalized
Initializing a servlet involves doing any expensive one-time setup, like loading configuration data from files or starting helper threads. Service calls from clients are handled employing a request and response paradigm. They believe the underlying network transport to supply quality of service guarantees, like reordering, duplication, message integrity, privacy, etc. Destroying a servlet involves undoing any initialization work and synchronizing the persistent state with the present in-memory state.
Servlet Interface Methods
The Servlet Interface contains the following 5 methods:
1. init() method:
public void init (ServletConfig config) throws ServletException
The init() method is named just one occasion by the servlet container throughout the lifetime of a servlet. By this init() method the servlet gets to know that it has been placed into service. The servlet cannot be put into the service if:
- The init() method does not return within a fixed time set by the webserver.
- It throws a ServletException
Parameters: The init() method takes a ServletConfig object that contains the initialization parameters and servlet’s configuration and throws a ServketException if an exception has occurred.
2. service()
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
Once the servlet starts getting the requests, the service() method is named by the servlet container to reply. The servlet services the client’s request with the assistance of two objects. These two objects javax.servlet.ServletResponse and javax.servlet.ServletResponse is passed by the servlet container.
The status code of the response always should be set for a servlet that throws or sends a mistake.
Parameters: The service() method takes the ServletRequest object that contains the client’s request and therefore the object ServletResponse contains the servlet’s response. The service() method throws ServletException and IOExceptions exception.
3. getServletConfig():
public ServletConfig getServletConfig()
It contains parameters for the initialization and startup of the servlet and returns a ServletConfig object. This object is then passed to the init method. When this interface is implemented then it stores the servletConfig object so as to return it.
Returns- the ServletConfig object.
4. getServletInfo():
public String getServletInfo()
It returns information about the servlet like version, author, etc. This method returns a string that should be in the form of plain text and not any kind of mark-up.
Returns – a string that contains the knowledge about the servlet.
5. destroy()
public void destroy()
This method is named once we got to close the servlet. The servlet container calls the destroy() method before removing a servlet instance from the service. Once the servlet container calls the destroy() method, no service methods will then be called. That is after the exit of all the threads running within the servlet, the destroy() method is named. That is after the exit of all the threads running within the servlet, the destroy() method is named. Hence, the servlet gets an opportunity to wash up all the resources like memory, threads, etc. which are being held.
Example: To understand the Java Servlet Interface
import java.io.*; import javax.servlet.*; public class ServletInterface implements Servlet { ServletConfig config = null; public void init (ServletConfig config) { this.config = config; System.out.println ("servlet is initialized"); } public void service (ServletRequest req, ServletResponse res) throws IOException, ServletException { res.setContentType ("text/html"); PrintWriter out = res.getWriter (); out.print ("<html><body>"); out.print ("<b>hello simple servlet</b>"); out.print ("</body></html>"); } public void destroy () { System.out.println ("servlet is destroyed"); } public ServletConfig getServletConfig () { return config; } public String getServletInfo () { return "copyright 2007-2020"; } }
Output
In the next article, I am going to discuss the Generic Servlet class in detail. Here, in this article, I try to explain Java Servlet Interface with Examples. I hope you enjoy this Java Servlet Interface with Examples article.