ServletContext Java EE 6

In this tutorial you will learn about the modification in ServletContext interface's made in Java EE 6.

ServletContext Java EE 6

In this tutorial you will learn about the modification in ServletContext interface's made in Java EE 6.

ServletContext Java EE 6

ServletContext Java EE 6

In this tutorial you will learn about the modification in ServletContext interface's made in Java EE 6.

Definition of ServletContext is remain same, as usual you know the definition of ServletContext as it is an interface into which the methods are defined for the purpose of communicating a Servlet with its servlet container. Servlet container for each web application creates an only one i.e. a single context on the application deployment at per Java Virtual Machine that can share the common information to all the components. Here is one thing should be remember that a ServletContext object is contained within the ServletConfig object. To initialize a global variable for a web application it should be make an entry of <contex-param></context-param> as a sub element of <web-app></web-app> element in web.xml file. for example :

<web-app>
<context-param>
<param-name>userId</param-name>
<param-value>bipul</param-value>
</context-param>
</web-app>

You can read the complete tutorial about the <context-param> from here.

In the Java EE 6 there are various of methods are added in ServletContext. Some of these are as follows :

  • addFilter() : This method adds the Filter in the context with different parameters.

    • addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass) : Using this method a Filter can be added with its class type and given name to this ServletContext.
    • addFilter(java.lang.String filterName, Filter filter) : Using this method a "filter" instance can be registered by a specified name under a Filter given by "filterName" to this ServletContext.
    • addFilter(java.lang.String filterName, java.lang.String className) : Using this method a Filter can be added by its name and className to this ServletContext.

  • addListener() : This method adds the Listener in the context with different parameters.

    • addListener(java.lang.Class<? extends java.util.EventListener> listenerClass) : Using this method for a given class type a listener can be added.
    • addListener(String className) : Using this method a listener can be added by a specified name to this ServletContext.
    • addListener(T t) : Using this method a listener can be added to this ServletContext.

  • addServlet() : This method adds the Servlet in the context with different parameters.

    • addServlet(String servletName, java.lang.Class<? extends Servlet> servletClass) : Using this method a Servlet can be added with its class type and given name to this ServletContext.
    • addServlet(String servletName, Servlet servlet) : Using this method a "servlet" instance can be registered by a specified name under a Servlet given by "servletName" to this ServletContext.
    • addServlet(String servletName, String className) : For a ServletContext using this method a servlet can be added by a specified name and class name.

  • <T extends Filter> T createFilter(java.lang.Class<T> clazz) throws ServletException : Using this method a specified Filter class can be instantiated.

  • <T extends java.util.EventListener> T createListener(java.lang.Class<T> clazz) throws ServletException : Using this method a specified EventListener class can be instantiated.

  • <T extends Servlet> T createServlet(java.lang.Class<T> clazz) throws ServletException : Using this method a specified Servlet class can be instantiated.

  • getFilterRegistration(String filterName) : Using this method FilterRegistration of Filter can be found by their specified name.
  • java.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations() : Using this method a map of FilterRegistration objects, into which the filter name is mapped as a key, can be found which are registered with this ServletContext.

  • getServletRegistration(java.lang.String servletName) : Using this method ServletRegistration of Servlet can be found by their specified name.

  • java.util.Map<java.lang.String,? extends ServletRegistration> getServletRegistrations() : Using this method a map of ServletRegistration objects, into which the servlet name is mapped as a key, can be found which are registered with this ServletContext.

  • setInitParameter(java.lang.String name, java.lang.String value) : Using this method an initialization parameter of context can be set by a specified name and the corresponding specified value.