ServletContext and ServletConfig Interfaces

All web applications in a Java EE server have their own unique context roots. A web application's context is reprecented by an implementation of the ServletContext interface. Each web application has only one servletContext and one ServletContext belongs to only one application. Servlets need to make use of the container interface to communicate with other components outside of their application or even within their application. 

A servlet can use a ServletContext object for various purposes, including:

  1. Retrieve information regarding the location of its web application

  2. Acquire the ServletContext of another application on same server

  3. Retrieve and modify application-scoped attributes and init parameters.

  4. Obtain handler or InputStream for static resources in application.

  5. Create RequestDispatcher object to forward/include resources into current output stream.

 

Useful methods of ServletContext

Useful methods to access information about current context include:

  • String getServletContextName()

    • Name of webapp as in <display.name> elements of the Deployment Descriptor or their corresponding annotations.

    • Null is returned if the element is not declared.

  • String getRealPath (String path)

    • Parameter path represents context relative path and should begin with forward slash (/). 

    • Return actual path to file on server

    • Null is returned if resources are contained in a war

  • Set getResourcePaths (String path)

    • Parameter path is a context-relative path and should begin with forward slash (/).

    • Returns a set of all resources and immediate subdirectories in the path

 

We can access static resources such as config files or htm document using

  1. Java.net.URL getResource (String path)

    1. Parameter path represents context relative path and should begin with forward slash (/).

    2. URL can be used to examine resource and its path or obain inputstream

  2. Java.io.InputStream getResourceAsStream (String path)

    1. Parameter path represents context relative path and should begin with forward slash (/).

    2. Return InputStream to read the resource given as path

    3. Prefered over getResource for InputStream

 

We can get the MIME Type of any resource as:

  • String getMimeType (String path)

    • MIME stands for Multipurpose Internet Mail Extension

    • We can override the default container assignment for MIME type through the Deployment Descriptor or corresponding annotations.

 

RequestDispatcher objects are used to communicate between components in the same web container, as well as to communicate with components of other applications on the same server. To communicate with other applications on the same server, we need to first get the the ServletContext of the other application on the same server and then obtain RequestDispatcher from the ServletContext. 

Methods to get RequestDispatcher are:

  1. RequestDispatcher getRequestDispatcher (String path)

    1. Null is returned if not possible to get RequestDispatcher.

  2. RequestDispatcher getNamedDispatcher (String name)

    1. Parameter name is the logical name of resource as specified in the Deployment Descriptor or corresponding annotations.

 

ServletConfig Interface

An implementation of the ServletConfig interface provides information specific to the current servlet with its own servlet-dependent initialization. This object is passed to the servlets init method by container during initialization.

You can get the logical name of a Servlet instance from a ServletConfig object using

  • String getServletName()

    • Class name is returned if logical name is not assigned.

You can also get the ServletContext object from a ServletConfig object as:

  • ServletContext getServletContext() 

While ServletContext provide access to Context Initialization parameters, ServletConfig provides access to Servlet initialization parameters. For details, refer to Parameters and Attributes in Servlet.

Tags: 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!