Example & Tutorial understanding programming in easy ways.

What is ServletContext object?

The ServletContext is an object of javax.servlet.ServletContext interface provides access to web application parameters to the servlet. It is just like the static parameters of the object. The ServletContext is unique object and available to all the servlets in the web application. 

When we want some init parameters to be available to multiple or all of the servlets in the web application, we can use ServletContext object and define its parameters in web.xml using the following <context-param> element. We can get the ServletContext object by the getServletContext() method of ServletConfig. 

Servlet containers may also provide context objects that are unique to a group of servlets and which is tied to a specific portion of the URL path namespace of the host that can be shared to the multiple servlets.

ServletContext is enhanced in Servlet we can use methods through which we can programmatically add Listeners and interceptors, Filters and Servlet to the application. It also provides some utility methods such as getMimeType(), getResourceAsStream() etc to get the servlet context.

Read More →