The Servlet API Nutshell


javax.servlet Package

Interfaces:
 
Servlet ServletConfig ServletContext
ServletRequest ServletResponse SingleThreadModel

Classes:
 
GenericServlet ServletInputStream ServletOutputStream

Exceptions:
 
ServletException UnavailableException


Servlet:
Defines the Life Cycle methods of a servlet: init, service and destroy methods.
It also provides methods that allows servlets to get startup information, about its server environment and also a method to provide to the server a description about the servlet.

  public abstract void destroy()
     Cleans up whatever resources are being held (e.g., memory, file handles, threads) and makes sure that any persistent state is synchronized with the servlet's current in-memory state. The servlet should be designed in such a way that all threads executing service method complete by the time destroy is called.

  public abstract ServletConfig getServletConfig()
     Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet. It is the same servlet config object passed to the init method and the init method should have stored it, so that this method could return.

public abstract String  getServletInfo()
     Returns a string containing information about the servlet, such as its author, version, and copyright.

  public abstract void init(ServletConfig) throws ServletException
     Initializes the servlet. If the initialization fails it should throw an UnavailableException and should never call System.exit() method.

public abstract void  service(ServletRequest, ServletResponse) throws ServletException , IOException
     Carries out a single request from the client. The method implements a request-response paradigm. Servlets run in multi-threaded servers. The servlet shoud be made thread-safe and synchronize access to shared resources like network connections, servlet's class and instance variables.


ServletConfig:

The interface is generally implemented by services of the server in order to pass configuration information to the servlet when it gets initialized. Servlets can also implement it so as to have an eay implementation of getting the servletContext:

Here is an  implementation of  getServletContext():

public ServletContext getServletContext() {
        return getServletConfig().getServletContext();
   }

This could be replaced by:

public ServletContext getServletContext() {
        return getServletContext();
   }

GenericServlet implements the ServletConfig.

Methods:

public abstract String getInitParameter(String)
     Returns a string containing the value of the named initialization parameter of the servlet, or null if the parameter
     does not exist.

public abstract Enumeration  getInitParameterNames()
     Returns the names of the servlet's initialization parameters as an enumeration of strings, or an empty enumeration if
     there are no initialization parameters.

public abstract ServeltContext  getServletContext()
     Returns the context for the servlet.


ServletContext:

The interface is implemented by the services of the server and used by the servlet to access information about its environment and also to log significant events. It is obtained by calling the getServletContext method on the ServletConfig passed on to the servlet at the time of the initialization.

Methods:

public abstract String getAttribute(String)
     Returns the value of the named attribute of the network service, or null if the attribute does not exist.

public abstract String  getMimeType(String)
     Returns the mime type of the specified file, or null if not known.

public abstract String  getRealPath(String)
     Applies alias rules to the specified virtual path and returns the corresponding real path.For example, in an HTTP
     servlet, this method would resolve the path against the HTTP service's docroot. Returns null if virtual paths are not
     supported, or if the translation could not be performed for any reason.

public abstract String   getServerInfo()
     Returns the name and version of the network service under which the servlet is running. In an HTTP server this value
     will be the same as the value of the CGI variable SERVER_SOFTWARE
 
public abstract Servlet getServlet(String) throw ServletException
     Returns the servlet of the specified name, or null if not found.
     This is a dangerous method to call because:

public abstract Enumeration  getServletNames()
     Returns an enumeration of the Servlet object names in this server. The enumeration always include the servlet itself.
     The list contains only servlets in the current namespace.
     This is a dangerous method to call for the same reason why getServlet method is dangerous to call.

public abstract Enumeration  getServlets()
     Returns an enumeration of the Servlet objects in this server. Deprecated.

public abstract void log(Exception, String)
     Write the stacktrace and the given message string to the servlet log file. The servlet log file is server specific and is
      normally an event log file.

public abstract void  log(String)
     Writes the given message string to the servlet log file.