Servlet interface:
The Servlet interface contains the common methods for all servlets i.e. provides the common behavior for all servlets.
public interface Servlet
Servlet interface is in javax.servlet package (javax.servlet.Servlet).
Methods of servlet interface:
1. init(ServletConfig config): It is used to initialize the servlet. This method is called only once by the web container when it loads the servlet.
Syntax:
public void init(ServletConfig config)throws ServletException
2. service(ServletRequest request, ServletResponse response): It is used to respond to a request. It is called for every new request by the web container.
Syntax:
public void service(ServletRequest req,ServletResponse res)throws ServletException, IOException
3. destroy(): It is used to destroy the servlet. This method is called only once by the web container when all servlet threads have exited or in a timeout case.
Syntax:
public void destroy()
4. getServletConfig(): It returns a servlet config object. This config object is passed in the init method. The servlet config object contains initialization parameters and startup configuration for this servlet.
Syntax:
public ServletConfig getServletConfig()
5. getServletInfo(): It returns a string of information about the servlet’s author, version, copyright etc.
Syntax:
public String getServletInfo()
Servlet “Hello World” example by implementing the Servlet interface.
HelloWorld.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class HelloWorld implements Servlet { private static final long serialVersionUID = 1L; //no-argument constructor. public HelloWorld() { } ServletConfig config=null; @Override public void init(ServletConfig config) throws ServletException { this.config = config; System.out.println("Do initialization here."); } @Override public void destroy() { System.out.println("Do clean-up process here."); } @Override public ServletConfig getServletConfig() { return config; } @Override public String getServletInfo() { return "w3schools.blog"; } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Hello World example using servlet interface."); out.close(); } }
web.xml
<!--?xml version="1.0" encoding="UTF-8"?--> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class> com.w3schools.business.HelloWorld </servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> </web-app>