Announcement Announcement Module
Collapse
No announcement yet.
How to access ServletContext from a Java DeclarativeWebScript Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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;
        }
    
    }

    Comment


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

      Cheers,

      Kev

      Comment


      • #4
        Thanks Kev.

        Comment

        Working...
        X