code: public static PortletApplicationContext getPortletApplicationContext(
              PortletRequest request, PortletContext portletContext) throws IllegalStateException {
  
          PortletApplicationContext portletApplicationContext = (PortletApplicationContext) request.getAttribute(
                  ContribDispatcherPortlet.PORTLET_APPLICATION_CONTEXT_ATTRIBUTE);
          if (portletApplicationContext == null) {
              if (portletContext == null) {
                  throw new IllegalStateException("No PortletApplicationContext found: not in a DispatcherPortlet request?");
              }
              portletApplicationContext = PortletApplicationContextUtils2.getRequiredPortletApplicationContext(portletContext);
          }
          
          return portletApplicationContext;
      }
docs: Look for the PortletApplicationContext associated with the DispatcherPortlet
  that has initiated request processing, and for the global context if none
  was found associated with the current request. This method is useful to
  allow components outside the framework, such as JSP tag handlers,
  to access the most specific application context available.
  
  @param request current portlet request
  @param portletContext current portlet context
  @return the request-specific PortletApplicationContext, or the global one
  if no request-specific context has been found
  @throws java.lang.IllegalStateException if neither a portlet-specific nor a
  global context has been found