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
Interface
|
Description
|
Servlet
|
Declares life cycle methods for a servlet.
|
ServletConfig
|
Allows servlets to get initialization parameters
|
ServletContext
|
Enables servlets to
log events and access information about their environment.
|
ServletRequest
|
Used to read data from a client request.
|
ServletResponse
|
Used to write data to a client response.
|
The following table summarizes the core classes that are provided in the javax.servlet package:
Class
|
Description
|
GenericServlet
|
Implements the Servlet and ServletConfig interfaces.
|
ServletInputStream
|
Provides an input stream for reading requests from a client.
|
ServletOutputStream
|
Provides an output
stream for writing responses to a client.
|
ServletException
|
Indicates a servlet error occurred.
|
UnavailableException
|
Indicates a servlet is unavailable.
|
The Servlet Interface
- All servlets must implement the Servlet interface.
- It declares the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet.
- A method is also provided that allows a servlet to obtain any initialization parameters.
- Void destroy()
- ServletConfig getServletConfig()
- String getServletInfo()
- Void init(ServletConfig sc) throws ServletException
- Void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
- 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
- ServletContext getServletContext( )
- String getInitParameter(String param)
- Enumeration getInitParameterNames( )
- String getServletName( )
The ServletContext Interface
The ServletContext interface enables servlets to obtain information about their environment. Here are some methods
- Object getAttribute(String attr)
- String getMimeType(String file)
- String getRealPath(String vpath)
- String getServerInfo( )
- void setAttribute(String attr, Object val)
Comments
Post a Comment