The three important resources of servlet API.
2. javax.servlet.GenericServlet (Abstract class)
3. javax.servlet.http.httpServlet (Abstract class)
• In Java an abstract class can contain only abstract methods or only concrete methods or mix of both.
• The above give predefined HttpServlet class is an abstract class containing no abstract methods.
• If you want to make methods / logics of one java class accessible only through subclass the make that class as abstract class even though that class contains only concrete methods.
• Every servlet program is a java class that is developed based on servlet api. There are three ways to develop servlet program.
1. Take a java class implementing javax.servlet.Servlet interface and provide implementation for all the five methods of that interface.
2. Take java class extending from javax.servlet.GenericServlet class and provide implementation for service() method.
3. Take java class extending from javax.servlet.http.httpServlet class and override one of the seven doxxx() methods or one of the two service(- -) methods.
• In the above said three approaches the overridden / implemented service (-,-) / doxxx(-) programmer places request processing logic that generates web pages by processing the request. But programmer never calls these methods manually but servlet container calls these methods automatically for every request given by client to servlet program.
• Programmer just supplies his servlet program related java class to servlet container then onwards servlet container is responsible to manage the whole life cycle of servlet program. (From object birth to death every operation will be take care by container).
• For all the request given to servlet program the servlet container creates one object but for every request given to servlet program, the servlet container create one separate request, response objects and calls service(-,-) method by keeping these requests, response objects as argument values.
• Servlet program uses request object to read details from the request.
• Servlet program uses response object to send response content to browser window through web server.
• When 10 requests are given to a servlet program from single or different browser windows (clients)
• Servlet container creates 10 threads on servlet program objects representing 10 requests.
• Servlet container creates 10 sets of request, response objects and calls service (-,-) or methods for 10 times having request, response objects as arguments values.
• To make our servlet program java class visible to servlet container the java class must be taken as public class.
The javax.servlet.Servlet Interface
The javax.servlet.Servlet interface is the basic interface of the Java Servlet API. This provides a standard abstraction for the servlet container to understand the Servlet object created by the user. That means the interface is designed to describe the user-defined Java object to the Servlet container, which encapsulates the application logic to plug into the servlet container for processing the client request. The servlet object must be a subtype of the interface to be managed by the servlet container. The following are the five methods declared in this interface:
1. public abstract void init(ServletConfig)
2. public abstract void service (Service Request, Service Response) throws servlet Exception, IO Exception
3. public abstract void destroy()
4. public abstract void ServletConfiggetServletConfig()
5. public abstract String getServletlnfo()
The javax.servlet.GenericServlet Class
The GenericServlet class implements the servlet interface and, for convenience, the ServletConfig interface. Servlet develops typically subclass GenericServlet, or its descendent HttpServlet, unless the servlet needs another class as a parent. (If a servlet does need to subclass another class, the servlet must implement the Servlet interface directly. This would be necessary when, for example, RMI or CORBA objects act as servlets.)
The GenericServlet class was created to make writing servlets easier. It provides simple versions of the life-cycle and ini t destroy methods, and of the methods in the ServletConfig interface. It also provides a log method, from the ServletContext interface. The servlet writer must override only the service method, which is abstract. Though not required, the servlet implementer should also override the getServletInfo method, and will want to specialize the ini t and destroy methods if expensive servlet-wide resources are to be managed. The following are the methods residing in GenericServlet class.
• public void destroy()
• public String getlnitParameter(String name)
• public Enumeration getlnitParameterNames()
• public ServletConfiggetServletConfig()
• public ServletContextgetServletContext()
• public String getServletlnfo()
• public void init(ServletConfigconfig) throws ServletException
• public void init() throws ServletException
• public void log (Exception e,Stringmsg)
• public void log (String message,Throwable t)
• public abstract· void service (ServletRequest req, ServletResponse res)
throw Servlet Exception, IOException
The javax.servlet.http.HttpServlet Class
It is an abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method. The methods normally overridden are;
• doGet, if HTTP GET requests are supported. Overriding the doGet method automatically also provides support for the HEAD and conditional GET operations. Where practical, the getLastModified method should also be overridden, to facilitate caching the HTTP response data. This improves performance by enabling smarter conditional GET support.
• doPost, if HTTP POST requests are supported.
• doPut, if HTTP PUT requests are supported.
• doDelete, if HTTP DELETE requests are supported.
• life cycle methods-in it and destroy, if the servlet writer needs to manage resources that are held for the lifetime of the servlet. Servlets that do not manage resources do not need to specialize these methods.
• getServletInfo, To provide descriptive information through a service's administrative interfaces.
Notice that the service method is not typically overridden. The service method, as provided, supports standard HTTP requests by dispatching them to appropriate methods, such as the methods listed above that have the prefix "do", In addition, the service method also supports the HTTP1.l protocol's TRACE and OPTIONS methods by dispatching to the doTrace and doOptions methods. The doTrace and doOptions methods are not typically overridden.
Servlets typically run inside multi-threaded servers; servlets must be written to handle multiple service requests simultaneously. It is the servlet writer's responsibility to synchronize access to any shared resources. Such resources include in-memory data such as instance or class variables of the servlet, as well as external components such as files, database and network connections.