Freakyuser
Freakyuser

Reputation: 2814

getServletConfig().getServletContext() equivalent in Spring

I referred to a lot of posts but still I am unable to find a correct working answer.
I want to get it from my Java class itself and not using EL in jsp.

How to get the servlet context path in Spring?

Upvotes: 28

Views: 35998

Answers (3)

Carlos Gavidia-Calderon
Carlos Gavidia-Calderon

Reputation: 7253

Another way is implementing ServletConfigAware in the class that depends on ServletContext. In the setServletConfig method you'll get an instance of ServletContext and you can do there what you have to do.

public class MyClass implements ServletConfigAware {

    private ServletConfig config;

    public void setServletConfig(ServletConfig servletConfig) {
        this.config = servletConfig;
    }

Upvotes: 8

Bastien Jansen
Bastien Jansen

Reputation: 8836

A solution is posted here: ServletContext and Spring MVC

@Autowired
ServletContext context;

Upvotes: 4

Evgeniy Fitsner
Evgeniy Fitsner

Reputation: 946

for SpringMVC

@Autowired
ServletContext servletContext;

Upvotes: 59

Related Questions