Servlet API

Two packages contain the classes and interfaces that are required to build servlets. These are javax.servlet and javax.servlet.http. These constitutes are called Servlet API.

javax.servlet Package:

javax.servlet package contains several interfaces and classes that establish the framework in which servlets operate. All servlets must implement this interface or extend a class that implements the interface. The ServletRequest and ServletResponse interfaces are also very important.

InterfaceDescription
ServletConfigIt allows servlets to get initialization parameters.
ServletContextIt enables servlets to log events and access information about their
environment.
ServletRequestIt used to read data from a client request.
ServletResponseIt used to write data to client response.
GenericServletIt implements the Servlet and ServletConfig interfaces.
ServletInputStreamIt provides an input stream for reading requests from a client.
ServletOutputStreamIt provides an output stream for writing responses to a client.
ServletExceptionIt indicates a servlet error occurred.
UnavailableExceptionIt indicates a servlet is unavailable.

ServletRequest Interface:

ServletRequest interface enables a servlet to obtain information about a client request.

MethodDescription
Object getAttribute(String attr)It returns the value of the attribute named attr.
String getCharacterEncoding( )It returns the character encoding of the request.
int getContentLength( )It returns the size of the request. The value –1 is returned if the size is unavailable.
String getContentType( )It returns the type of the request. A null value is returned if the type cannot be determined.
ServletInputStream getInputStream( )
throws IOException
It returns a ServletInputStream that can be used to read binary data from the request. An IllegalStateException is thrown if getReader( ) has already been invoked for this request.
String getParameter(String pname)It returns the value of the parameter named pname.
Enumeration getParameterNames( )It returns an enumeration of the parameter names for this request.
String[ ] getParameterValues(String name)It returns an array containing values associated with the parameter specified by name.
String getProtocol( )It returns a description of the protocol.
BufferedReader getReader( )
throws IOException
It returns a buffered reader that can be used to read text from the request. An IllegalStateException is thrown if getInputStream( ) has already been invoked for this request.
String getRemoteAddr( )It returns the string equivalent of the client IP address.
String getRemoteHost( )It returns the string equivalent of the client hostname.
String getScheme( )It returns the transmission scheme of the URL used for the request
(for example, “http”, “ftp”).
String getServerName( )It returns the name of the server.
int getServerPort( )It returns the port number.

ServletResponse Interface:

ServletResponse interface enables a servlet to formulate a response for a client.

MethodDescription
String getCharacterEncoding( )It returns the character encoding for the response.
ServletOutputStream
getOutputStream( )
throws IOException
It returns a ServletOutputStream that can be used to write binary data to the response. An IllegalStateException is thrown if getWriter( ) has already been invoked for this request.
PrintWriter getWriter( )
throws IOException
It returns a PrintWriter that can be used to write character data to the response. An IllegalStateException is thrown if getOutputStream( ) has already been invoked for this request.
void setContentLength(int size)It sets the content length for the response to size.
void setContentType(String type)It sets the content type for the response to type.

ServletConfig Interface:

ServletConfig interface allows a servlet to obtain configuration data when it is loaded.

MethodDescription
ServletContext getServletContext( )It returns the context for this servlet.
String getInitParameter(String param)It returns the value of the initialization parameter named param.
Enumeration getInitParameterNames( )It returns an enumeration of all initialization parameter names.
String getServletName( )It returns the name of the invoking servlet.

ServletContext Interface:

ServletContext interface enables servlets to obtain information about their environment.

MethodDescription
Object getAttribute(String attr)It returns the value of the server attribute named attr.
String getMimeType(String file)It returns the MIME type of file.
String getRealPath(String vpath)It returns the real path that corresponds to the virtual path vpath.
String getServerInfo( )It 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)It sets the attribute specified by attr to the value passed in val.