Saturday, February 22, 2014

The javax.servlet Package

The javax.servlet package contains a number of interfaces and classes that establish the framework in which servlets operate. The most significant of these is Servlet. All servlets must implement this interface or extend a class that implements the interface

Declares life cycle methods for a servlet.
Allows servlets to get initialization parameters
Enables servlets to log events and access information about their environment.
Used to read data from a client request.
Used to write data to a client response.

The following table summarizes the core classes that are provided in the javax.servlet package:

Implements the Servlet and ServletConfig interfaces.
Provides an input stream for reading requests from a client.
Provides an output stream for writing responses to a client.
Indicates a servlet error occurred.
Indicates a servlet is unavailable.

The Servlet Interface

  1. All servlets must implement the Servlet interface.
  2. It declares the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet.
  3. A method is also provided that allows a servlet to obtain any initialization parameters.
    1. Void destroy()
    2. ServletConfig getServletConfig()
    3. String getServletInfo()
    4. Void init(ServletConfig sc) throws ServletException
    5. Void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
  4. The init( ), service( ), and destroy( ) methods are the life cycle methods of the servlet. These are invoked by the server. The getServletConfig( ) method is called by the servlet to obtain initialization parameters. A servlet developer overrides the getServletInfo( ) method to provide a string with useful information (for example, author, copyright etc)

The ServletConfig Interface

The ServletConfig interface allows a servlet to obtain configuration data when it is loaded. Here are some methods

  1. ServletContext getServletContext( )
  2. String getInitParameter(String param)
  3. Enumeration getInitParameterNames( )
  4. String getServletName( )

The ServletContext Interface

The ServletContext interface enables servlets to obtain information about their environment. Here are some methods

  1. Object getAttribute(String attr)
  2. String getMimeType(String file)
  3. String getRealPath(String vpath)
  4. String getServerInfo( )
  5. void setAttribute(String attr, Object val)

No comments:

Post a Comment