Results 1 to 4 of 4

Thread: How to access ServletContext from a Java DeclarativeWebScript

  1. #1
    Join Date
    Sep 2010
    Posts
    25

    Default How to access ServletContext from a Java DeclarativeWebScript

    I don't see a way of getting a handle to a javax.servlet.ServletContext object so that I can get attributes out of that object which are in application scope. Here is my code:

    Code:
    public class ProductDetailsController extends DeclarativeWebScript {
    
        @Override
        public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    
            HttpServletRequest request = ((WebScriptServletRequest) req).getHttpServletRequest();
    
        }
    }

    As you can see, I tried getting a handle to an HttpServletRequest but that does not help me as well.

    What is the proper way to retrieve objects out of application scope within a Java Declarative WebScript?

  2. #2
    Join Date
    Sep 2010
    Posts
    25

    Default

    I figured out a solution to this, not entirely sure if it's the preferred way or a best practice.

    I was able to inject a javax.servlet.ServletContext object onto my DeclaritiveWebScript by having it implement ServletContextAware. Here's the code:

    Code:
    public class ProductDetailsController extends DeclarativeWebScript implements ServletContextAware {
    
        private ServletContext sc;
    
        @Override
        public Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
            System.out.println("Injected ServletContext: ["+sc+"]");
        }
    
    
        @Override
        public void setServletContext(ServletContext sc) {
            this.sc = sc;
        }
    
    }

  3. #3

    Default

    There is no direct access to ServletContext from a WebScript, so what you have done using Spring injection is perfectly acceptable.

    Cheers,

    Kev

  4. #4
    Join Date
    Sep 2010
    Posts
    25

    Default

    Thanks Kev.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •