In my last guide about Servlet API, I have explained that to create any Servlet you must implement the Servlet interface directly or indirectly (indirectly implementation means extending those classes that implements Servlet interface, These classes are GenericServlet and HttpServlet).
If you are creating protocol dependent servlet such as http servlet then you should extend HttpServlet class else for protocol independent Servlet you extend GenericServlet class.
In this guide I am not going to explain GenericServlet and HttpServlet in detail as I have covered them in the separate guide.
Edit: Here are the links of those guides: GenericServlet | HttpServlet.
In short you have 3 ways to create a servlet:
1) By extending HttpServlet class
2) By extending GenericServlet class
3) By implementing Servlet interface
Note: However you should always prefer the first way of creating servlet i.e. by extending HttpServlet class.
Servlet Interface methods
Here is the list of methods available in Servlet interface.
1) void destroy(): This method is called by Servlet container at the end of servlet life cycle. Unlike service() method that gets called multiple times during life cycle, this method is called only once by Servlet container during the complete life cycle. Once destroy() method is called the servlet container does not call the service() method for that servlet.
2) void init(ServletConfig config): When Servlet container starts up (that happens when the web server starts up) it loads all the servlets and instantiates them. After this init() method gets called for each instantiated servlet, this method initializes the servlet.
3) void service(ServletRequest req, ServletResponse res): This is the only method that is called multiple times during servlet life cycle. This methods serves the client request, it is called every time the server receives a request.
4) ServletConfig getServletConfig(): Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
5) java.lang.String getServletInfo(): Returns information about the servlet, such as author, version, and copyright.
Example:
In this example we have created a servlet class by extending Servlet interface.
index.html
<a href="welcome">Click here to call the servlet</a>
DemoServlet.java
import java.io.*; import javax.servlet.*; public class DemoServlet implements Servlet{ ServletConfig config=null; public void init(ServletConfig config){ this.config=config; System.out.println("Initialization complete"); } public void service(ServletRequest req,ServletResponse res) throws IOException,ServletException{ res.setContentType("text/html"); PrintWriter pwriter=res.getWriter(); pwriter.print("<html>"); pwriter.print("<body>"); pwriter.print("<h1>Servlet Example Program</h1>"); pwriter.print("</body>"); pwriter.print("</html>"); } public void destroy(){ System.out.println("servlet life cycle finished"); } public ServletConfig getServletConfig(){ return config; } public String getServletInfo(){ return "A Demo program written by Chaitanya"; } }
web.xml
<web-app> <servlet> <servlet-name>Beginnersbook</servlet-name> <servlet-class>DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Beginnersbook</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>
Leave a Reply