Servlets - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Packages

Servlet Packages

shape Description

Servlet packages, while developing an application depends on the any domain programmer has to import required packages these packages will have some functionalities that provide support. As discussed earlier, while writing the code we have imported some packages, all these packages will be available in the servlet API along with the packages The programmer has to import required jar files also, Following are the some packages which are commonly used in servlet applications. Click Here To Know More About HttpServlet Click Here To Know More About GenericServlet

shape Conceptual figure

Servlet interface

shape Description

javax.servlet.Servlet is an interface which has servlet life cycle methods. So all servlets must implement this interface either directly or indirectly. Instead of directly implementing the ServletInterface, a servlet can also extend GenericServlet class which is implemented by ServletInterface or can extend HttpServlet(which extends the GenericServlet).

Five methods of ServletInterface

shape Methods

  • init(ServletConfig config): Created by the servlet container to initialize the servlet. Config is a ServletConfig object containing servlet's configuration and initialization parameters.
  • service(ServletRequest request, ServletResponse response): It is also created by the servlet container to allow request and response of a servlet. ServletRequest object contains client request and ServletResponse object contains servlet response.
  • destroy(): It is created by the servlet container to clean up servlet service.
  • getServletConfig(): It returns an object of ServletConfig which contains the initialization parameters and startup configuration of a servlet.
  • getServletInfo(): It returns a string containing the details of a servlet like author, version and copyright.

shape Examples

Example on servlet by implements Servlet Interface: index.html [html] <html> <body> <form action="./go"> <input type="submit" value="click here"/> </form> </body> </html> [/html] Here just created submit button that is click here. web.xml [xml] <web-app> <servlet> <servlet-name>Demo</servlet-name> <servlet-class>Demo</servlet-class> </servlet> <servlet-mapping> <servlet-name>Demo</servlet-name> <url-pattern>/go</url-pattern> </servlet-mapping> </web-app> [/xml] Make sure that URL name should be match with HTML form that is /go. Demo.java [java] import java.io.*; import javax.servlet.*; public class Demo implements Servlet{ ServletConfig config=null; public void init(ServletConfig config){ this.config=config; System.out.println("servlet is initialized:::"+config); } public void service(ServletRequest request,ServletResponse response) throws IOException,ServletException{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.print("<html><body>"); out.print(" <h1><b>welcome to Splessons</b></h1>"); out.print(" <h2><b>Example on Servlet Interface<b></h2>"); out.print("</body></html>"); } public void destroy(){System.out.println("servlet is destroyed");} public ServletConfig getServletConfig(){return config;} public String getServletInfo(){return "copyright 2007-2010";} } [/java] Following is the code structure of life cycle methods. Output Following is the page when user click on the button required page will be displayed. Result will be as follows.

Summary

shape Key Points

  • Import is the keyword to import the packages.
  • Every package will consists of related functions and methods.
  • Jar files also required even though imported packages while dealing with big projects.