Introduction
ServletConfig is used by only single servlet to get configuration information, whereas ServletContext is used by multiple objects to get configuration information.
ServletConfig object is one per servlet class and destroyed once the servlet execution is completed. ServletConfig object gets created during initialization process of the servlet and this object is public to a particular servlet only.
ServletContext object is global to entire web application and ServletContext object gets created during the web application deployment. ServletContext object is available as long as web application is live on the server. ServletContext object is available even before giving the first request.
Remember these objects will work only in servlet container specific environment.
There are two ways to get Container specific objects in the spring bean:
- Implementing Spring *Aware interfaces, for these ServletContextAware and ServletConfigAware interfaces. So your bean needs to implement
ServletContextAware
andServletConfigAware
interfaces and override thesetServletContext()
andsetServletConfig()
methods. - Using
@Autowired
annotation with bean variable of typeServletContext
andServletConfig
.
Now you will see how to get these objects in Spring Beans.
Prerequisites
Java at least 8, Spring Boot 2.2.6 – 2.4.4, Gradle 6.1.1 – 6.8.3
Project Setup
You can create a gradle based project in Eclipse. The name of the project is spring-servletcontext-servletconfig.
The build.gradle script looks as below:
buildscript {
ext {
springBootVersion = '2.2.6.RELEASE' //to 2.4.4
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java-library'
id 'org.springframework.boot' version "${springBootVersion}"
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}
Get using *Aware Interfaces
You will see how to get ServletConfig and ServletContext objects by implementing *Aware interfaces.
@Controller
@RequestMapping("/controller")
public class SpringServletContextConfigController implements ServletConfigAware, ServletContextAware {
private ServletConfig servletConfig;
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public void setServletConfig(ServletConfig servletConfig) {
this.servletConfig = servletConfig;
}
@RequestMapping("/servlet/config")
public ResponseEntity<String> getServletConfig() {
return new ResponseEntity<String>("Servlet Config: " + servletConfig, HttpStatus.OK);
}
@RequestMapping("/servlet/context")
public ResponseEntity<String> getServletContext() {
return new ResponseEntity<String>("Servlet Context: " + servletContext, HttpStatus.OK);
}
}
Even you can get the ServletContext object from ServletConfig object.
Get using @Autowired ServletContext and ServletConfig
You will directly autowire ServletContext and ServletConfig objects.
@RestController
@RequestMapping("/rest/controller")
public class SpringServletContextConfigRestController {
@Autowired
private ServletConfig servletConfig;
@Autowired
private ServletContext servletContext;
@GetMapping("/servlet/config")
public ResponseEntity<String> getServletConfig() {
return new ResponseEntity<String>("Servlet Config: " + servletConfig, HttpStatus.OK);
}
@GetMapping("/servlet/context")
public ResponseEntity<String> getServletContext() {
return new ResponseEntity<String>("Servlet Context: " + servletContext, HttpStatus.OK);
}
}
Remember in the above Controller method code, I have implemented ServletConfigAware interface and set ServletConfig object but it will give you null
.
As noted in the javadoc for ConfigurableWebApplicationContext.setServletConfig(ServletConfig)
, the method is “only called for a WebApplicationContext
that belongs to a specific Servlet”. The javadoc of ServletConfigAware
where it says the following:
Only satisfied if actually running within a Servlet-specific WebApplicationContext. Otherwise, no ServletConfig will be set.
There’s no Servlet-specific WebApplicationContext
in a Spring Boot application so setServletConfig(ServletConfig)
is not called and the aware callback is not driven either.