How to get Spring WebContext in class annotated @controller

23,425

Solution 1

You can just inject it into your controller:

@Autowired private ServletContext servletContext;

Or take HttpServletRequest as a parameter and get it from there:

@RequestMapping(...)
public ModelAndView myMethod(HttpServletRequest request ...){
    ServletContext servletContext = request.getServletContext()
}

Solution 2

You can implement an Interface from Spring called org.springframework.web.context.ServletContextAware

public class MyController implements ServletContextAware {
    private ServletContext servletContext; 

    @Override
    public void setServletContext(ServletContext servletContext) {
       this.servletContext=servletContext;
    }
}

Then you can use the servletContext any place in the class.

Solution 3

The following is correct approach :

@Autowired
ServletContext context;

Otherwise instead of auto wiring the ServletContext, you can implement ServletContextAware. Spring will notice this when running in a web application context and inject the ServletContext. Read this.

Solution 4

You can also do it inline:

@RequestMapping(value = "/demp", method = RequestMethod.PUT)
public String demo(@RequestBody String request) {
    HttpServletRequest re3 = ((ServletRequestAttributes) RequestContextHolder

            .getRequestAttributes()).getRequest();
    return "sfsdf";
 }

Solution 5

By accessing the session you can get the servlet context, sample code:

@Controller
public class MyController{

....
@RequestMapping(...)
public ModelAndView myMethod(HttpSession session ...){

WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext())

}

}

You can get the HttpSession from the HttpServletRequest also.

Share:
23,425
Kaushik Lele
Author by

Kaushik Lele

I am Sr. Software Engineer in an MNC in India. Around 8 Years of IT experience. I like to learn new technologies and do simple hands-on to understand the concepts first hand. I like to be in touch with basics of Computer Science like data structures, algorithms, design patterns. So I try to think over related questions and try to answer them.

Updated on September 02, 2020

Comments

  • Kaushik Lele
    Kaushik Lele almost 4 years

    In Spring MVC with annotation, we mark any POJO with @Controller. In this controller we can get WebApplicationContext, using autowired property.

    @Controller
    public class HomePageController {
    
    @Autowired
    ApplicationContext act;
    
        @RequestMapping("/*.html")
        public String handleBasic(){
            SimpleDomain sd = (SimpleDomain)act.getBean("sd1");
            System.out.println(sd.getFirstProp());
            return "hello";
    }
    

    But in this approach we do not have servletContext handy with us. So is there way we can still use older way of getting WebApplicationContext ? i.e.

    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
    

    How will we get servletContext here ?

    I am not facing any compulsion to use old way; so this question is just out of curiosity to check flexibility of spring. Also It can be a interview question.

  • itsraja
    itsraja over 10 years
    request doesn't have getServletContext(), request.getSession().getServletContext() will work
  • Luiggi Mendoza
    Luiggi Mendoza about 10 years
    @itsraja in fact, HttpServletRequest has that method, inherited from ServletRequest.
  • Luiggi Mendoza
    Luiggi Mendoza almost 10 years
    @Raj that method exists way before than Servlet 3.0