Servlets - SPLessons

Servlet Packages

Chapter 41

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

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:


		<form action="./go">
			<input type="submit" value="click here"/>

Here just created submit button that is click here.




Make sure that URL name should be match with HTML form that is /go.

import javax.servlet.*;  
public class Demo implements Servlet{  
ServletConfig config=null;  
public void init(ServletConfig config){  
System.out.println("servlet is initialized:::"+config);  
public void service(ServletRequest request,ServletResponse response)  
throws IOException,ServletException{  
PrintWriter out=response.getWriter();

<h1><b>welcome to Splessons</b></h1>");
<h2><b>Example on Servlet Interface<b></h2>"); 
public void destroy(){System.out.println("servlet is destroyed");}  
public ServletConfig getServletConfig(){return config;}  
public String getServletInfo(){return "copyright 2007-2010";}  

Following is the code structure of life cycle methods.

Following is the page when user click on the button required page will be displayed.

Result will be as follows.


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.