javax.servlet.ServletContext

A web application can have multiple servlets to process client requests. Each Servlet will have individual ServletConfig object. However, all the servlets are running in the same context. This context is unique for the web application. ServletConfig instance holds the reference to the same context. When the web application is installed in the web container, an instance that implements ServletContext interface is created. This instance is the abstraction of the web:application itself. A ServletContext object is the Servlet’s connection to both the container and other parts of the web application, multiple servlets can share information and resources by using ServletContext object. With the help of this object one Servlet can communicate with another Servlet running in the same web application.

List of important methods in ServletContext interface Methods to deal with attributes and init parameters:-

•     public String getInitParameter(String name):- Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster’s email address or the name of a system that holds critical data.

•     public Enumeration getInitParamNames():-Returns the names of the context’s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

•     public void setAttribute(String name, Object value):- Binds an object to a given attribute name in this servlet context, if the name specified is already used for an attribute, this method will replace the attribute.

•     public Object getAttribute(String name):- Returns the attribute bound to the ServletContext object with the given name, or null if there is no attribute by that name.

•     public Enumeration getAttributeName():- Returns an Enumeration containing the attribute names available within this Servlet context.

Methods to get info about Servlet container and logging information:

•     public void log(String info):- Writes the specified message to a Servlet log file, usually an event log. The name and type of the Servlet log file is specific to the Servlet container.

•     public String getServerInfo():-Returns the name and version of the Servlet container on which the Servlet is running.

•     public int getMajorVersion():-Returns the major version of the Java Servlet API that this Servlet container supports. It returns a number 2. (In Servlet 2.4 version 2 is major version)

•     public int getMinorVersion():- This method returns a number 4 (if contains supports Serviets 2.4).

Method to communicate with other servlets in the web application

•     RequestDispatcher getRequestDispatcher(String name):- Returns the RequestDispatcher object.

javax.servlet.http.HttpServletResponse interface

This interface extends ServletResponse interface. The Serviet engine creates an object that implements HttpServletResponse and passes it as an argument to the servlet’s servicing methods. We use this object to customize the response that has to be given to the client.

List of important methods in this interface

•     addCookie(Cookie c): -This method adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.

•     addHeader(String name, String value): Adds a response header with the given name and value.

•     sendError(int sc, String msg): – This method sends an error response to the client using the specified status. The server defaults to creating the response to look like an HTML formatted server error page containing specific message.

•     String encodeURL(String url): – This method encodes the specified URL by including the session ID in it, or , if encoding not needed, returns the URL unchanged. The implementation of this method includes logic to determine whether the session id needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary. For robust session tracking, all URLs emitted by a Servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers, which do not support cookies.

•     sendRedirect(String location): -sends a temporary redirect response to the client using the specified redirect location URL .

•     setContentType(String type); – Sets the content type of the response being sent to the client, if the response has not been committed yet. It takes a String specifying the MIME type of the content as argument.

•     setContentLength(int len): – Sets the length of the content body in the response. In Http servlets, this method sets the HTTP Content-Length header.

•     String getContentType(): – Returns the content type used for the MIME body sent in this response.

•     ServletOutputStream getOutputStream(): – Returns ServletOutputStream suitable for writing binary data in the response. Calling flush() on the ServletOutputStream commits the response.

•     PrintWriter getWriter(): – Returns a PrintWriter object than can send character text to the client. Calling flush() on the PrintWriter commits the response.

•     Boolean isCommitted(): – Returns true or false indicating if the response has been committed. A committed response has already had its status code and headers written.

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!