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: 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)