Interfaces in Servlets

ServletRequest Interface :

This interface is for getting data from the client to the servlet for a service request. Some of the data provided by the ServletRequest object includes parameter names and values, attributes, and an input stream.
Methods of ServletRequest interface:
Method
Use
String getProtocol( )
Returns a description of the protocol.
Object getAttribute(String attr)
Returns the value of the attribute named attr.
String getParameterlString name)
It is used to obtain the value of a parameter by name.
String[] getParameterValues(String name)
Returns an array of String containing all values of given parameter name is mainly used to obtain values of a multi select list box.
Enumeration getParameterNames()
Returns an enumeration of all of the request parameter names.
int getContentLength()
Returns the size of the request entity data, or -1 if not known.
String getCharacterEncoding()
Returns the character set encoding for the input of this request.
String getContentType()
Returns the Internet media type of the request entity data, or null if not known.
String getRemoteAddr( )
Returns the string equivalent of the client IP address.
String getRemoteHost( )
Returns the string equivalent of the client host name.
String getScheme( )
Returns the transmission scheme of the URL used for the  request (for example, “http”, “ftp”).
ServletlnputStream getlnputStream() throws IOException
Returns an input stream for reading binary data in the request body.
abstract String getServerName()
Returns the host name of the server that received the request.
int getServerPort()
Returns the port number on which this request was received.

ServletConfig :

During the initialisation of the servlet, container creates a ServletConfig object for the servlet. ServletConfig object is used to pass the information to the servlet during initiaiization by getting configuration information from web.xml (deployment descriptor).
Methods of ServletConfig interface:
Method
Purpose
ServletContext getServletContext()
Returns an object of ServletContext.
String getinitParameter(String param)
Returns the parameter value for the parameter named param.
Enumeration getinitParameterNames()
Returns an enumeration of all the initialization parameter names
String getServletName()
Returns the name of the servlet.

ServletContext Interface:

It enables servlets to obtain information about their environment  i.e. it enables servlet  to communicate with its container, for example, write to a log file.
There is one context per web application per Java Virtual Machine.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
An obiect of ServletContext can be obtained using the getServletContext() method of the GenericServlet class
Commonly used Methods of ServletContext interface:
Method
Purpose
String getlnitParameter(String param)
Returns the parameter value for the specified parameter named, param.
Enumeration getlnitParameterNames()
Returns the names of the context’s initialization parameters.
void setAttribute(String param,Object object)
Sets the attribute specified by param to the value passed in object.
Object getAttribute(String name)
Returns the attribute for the specified name.
void removeAttribute(String name)
Removes the attribute with the given name from the servlet context.
void log(String s)
Writes s to the servlet log.
void log(String s, Throwable e)
Write s and the stack trace for e to the servlet log.

Leave a Reply

Your email address will not be published. Required fields are marked *