Home | | Web Programming | The javax.servlet Package

Chapter: Java The Complete Reference : Applying Java : Introducing Servlets

The javax.servlet Package

The javax.servlet package contains a number of interfaces and classes that establish the framework in which servlets operate.

The javax.servlet Package

 

The javax.servlet package contains a number of interfaces and classes that establish the framework in which servlets operate. The following table summarizes several key interfaces that are provided in this package. The most significant of these is Servlet. All servlets must implement this interface or extend a class that implements the interface. The ServletRequest and ServletResponse interfaces are also very important.


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


Let us examine these interfaces and classes in more detail.

 

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. The methods defined by Servlet are shown in Table 38-1.

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, the version number). This method is also invoked by the server.


The ServletConfig Interface

 

The ServletConfig interface allows a servlet to obtain configuration data when it is loaded. The methods declared by this interface are summarized here:


The ServletContext Interface

 

The ServletContext interface enables servlets to obtain information about their environment. Several of its methods are summarized in Table 38-2.

 

The ServletRequest Interface

 

The ServletRequest interface enables a servlet to obtain information about a client request. Several of its methods are summarized in Table 38-3.

 

The ServletResponse Interface

 

The ServletResponse interface enables a servlet to formulate a response for a client. Several of its methods are summarized in Table 38-4.

 

Method : Description

 

Object getAttribute(String attr) : Returns the value of the server attribute named attr.

 

String getMimeType(String file) : Returns the MIME type of file.

 

String getRealPath(String vpath) : Returns the real (i.e., absolute) path that  corresponds to the relative path vpath.

 

String getServerInfo( ) : Returns information about the server.

 

void log(String s) : Writes s to the servlet log.

 

void log(String s, Throwable e) : Writes s and the stack trace for e to the servlet log.

 

void setAttribute(String attr, Object val) : Sets the attribute specified by attr to the value  passed in val.





The GenericServlet Class

 

The GenericServlet class provides implementations of the basic life cycle methods for a servlet. GenericServlet implements the Servlet and ServletConfig interfaces. In addition, a method to append a string to the server log file is available. The signatures of this method are shown here:

 

void log(String s)

 

void log(String s, Throwable e)

 

Here, s is the string to be appended to the log, and e is an exception that occurred.

 

The ServletInputStream Class

 

The ServletInputStream class extends InputStream. It is implemented by the servlet container and provides an input stream that a servlet developer can use to read the data from a client request. In addition to the input methods inherited from InputStream, a method is provided to read bytes from the stream. It is shown here:

 

int readLine(byte[ ] buffer, int offset, int size) throws IOException

 

Here, buffer is the array into which size bytes are placed starting at offset. The method returns the actual number of bytes read or –1 if an end-of-stream condition is encountered.

 

The ServletOutputStream Class

 

The ServletOutputStream class extends OutputStream. It is implemented by the servlet container and provides an output stream that a servlet developer can use to write data to a client response. In addition to the output methods provided by OutputStream, it also defines the print( ) and println( ) methods, which output data to the stream.

 

The Servlet Exception Classes

 

javax.servlet defines two exceptions. The first is ServletException, which indicates that a servlet problem has occurred. The second is UnavailableException, which extends ServletException. It indicates that a servlet is unavailable.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Applying Java : Introducing Servlets : The javax.servlet Package |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.