ServletContext is an interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it. The information in the ServletContext will be common to all the components. Remember that each servlet will have its own ServletConfig. The ServletContext is created by the container when the web application is deployed and only after that the context is available to each servlet in the web application.
Web application initialization:
1. First of all the web container reads the deployment descriptor file and then creates a name/value pair for each <context-param> tag.
2. After creating the name/value pair it creates a new instance of ServletContext.
3. It’s the responsibility of the Container to give the reference of the ServletContext to the context init parameters.
4. The servlet and jsp which are part of the same web application can have the access to the ServletContext.
The Context init parameters are available to the entire web application not just to the single servlet like servlet init parameters.
How can we do the mapping of the Context init parameters in web.xml
<servlet>
<servlet-name>Mapping</servlet-name>
<servlet-class>ContextMapping</servlet-class>
</servlet>
<context-param>
<param-name>Email</param-name>
<param-value>admin@java.net</param-value>
</context-param>
In the servlet code we will write this as
<servlet-name>Mapping</servlet-name>
<servlet-class>ContextMapping</servlet-class>
</servlet>
<context-param>
<param-name>Email</param-name>
<param-value>admin@java.net</param-value>
</context-param>
In the servlet code we will write this as
ServletContext context = getServletContext();
pw.println(context.getInitParameter("Email");
ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container.
ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file.
The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.
The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.
pw.println(context.getInitParameter("Email");
ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container.
ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file.
The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.
The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.
No comments:
Post a Comment