how to get getServletContext() in spring mvc Controller

  1. Use below code to autowire ServletContext object in SpringMVC

    @Autowired
    ServletContext context; 
    

    and after that try to execute your code like

    String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;
    
  2. You can get it in your controller like this;

    private ServletContext context;
    
    public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
    }
    

    but for this your controller must implement ServletContextAware interface

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)