Pages

How to get ServletContext in Servlet, jsp, spring controller or struts action class is common need of any Java web developer. As ServletContext is an application wide object and used to store variables in global scope, getting a reference of ServletContext is pretty important. Every web application can have only one ServletContext though they can have multiple ServletConfig objects. In this article we will see :

How to get Servlet Context inside Spring MVC Controller?

How to find Servlet Context inside Struts Action class?

How to get Servlet Context inside JSP File?

How to find Servlet Context inside HttpServlet Class?

How to get Servlet Context inside Spring MVC Controller?

In our last article on Spring we have seen How to perform LDAP authentication in windows Active directory using Spring Security and here we will see how to retrieve ServletContext inside Spring Controller class. We often need ServletContext reference inside Spring Controller class to get a reference of Spring AppContext in order to retrieve bean defined inside applicationContext.xml. Spring web application has separate applicaitonContext called WebApplicationContext. In order to get this, you need a reference of the ServletContext object. Since every Controller in Spring has access to HttpRequest and HttpResponse object, we can get ServletContext from request object, but request object doesn't have direct access to ServletContext prior to Servlet Specification 3.0, hence you need to first get HttpSession object from request and than ServletContext object form session as shown in below code example:

if you are running on web_app version 3.0 and has Servlet 3.0 API you can directly get ServletContext object form HttpServletRequest as shown in below example:

ServletContext sc = request.getServletContext();

How to get ServletContext object in JSP

In our last article on JSP we have seen how to define application wide error page and here we will see how to get the Servletcontext object in JSP. This is also one of frequently asked JSP interview questions mostly for freshers and less experienced developers . Anyway getting the ServletContext reference in JSP is quite easy as compared to Spring or Struts. Since JSP has a reference of ServletContext as an implicit object and you can directly access it using "application" implicit object inside any scriptlet or expression. You can even access variable stored inside ServletContext by using expression language (EL) map called applicationScope. here is a code example of getting ServletContext inside JSP file:

and it will print in HTML as "What is your favorite programming language : Java"

You can also use JSTL core set tag for putting variables and beans on application scope defined by the ServletContext object.

How to get ServletContext object inside Servlet:

This question is also frequently asked in various Java Servlet interviews along with other J2EE technology. You can easily get a reference of ServletContext inside your Servlet in the same way we have accessed ServletContext in our spring Controller example. Until you have access of ServletRequest or HttpServletRequest you can get session object from it and then you can get a reference of ServletContext. An example is exactly similar to our earlier spring controller code. here is an example of getting ServletContext inside Servlet:

How to get ServletContext object Action class Struts:

In our last article on Jakarta Struts we have seen Top 10 jakarta struts interview questions asked in J2EE interviews and here we will how to find ServletContext reference inside Struts Action class. Similar approach of Spring Controller and Servlet can be used to get ServletContext inside Struts Action class. Since Struts Action class contains execute method which contains HttpServletRequest as parameter, you can use HttpServletRequest for getting ServletContext just like above example. here is sample example of how to access servletContext object inside Struts Action class :

Now we know how to get ServletContext in Spring Controller, Struts Action Class, Servlet, and JSP. You see in reality all examples are similar they all get the reference of ServletContext using HttpServletRequest or HttpSession. Servlet API 3.0 has an even more convenient method inside HttpServletRequest which can be used to directly access ServletContext from the request object.