Example usage for javax.servlet.jsp PageContext APPLICATION_SCOPE

List of usage examples for javax.servlet.jsp PageContext APPLICATION_SCOPE

Introduction

In this page you can find the example usage for javax.servlet.jsp PageContext APPLICATION_SCOPE.

Prototype

int APPLICATION_SCOPE

To view the source code for javax.servlet.jsp PageContext APPLICATION_SCOPE.

Click Source Link

Document

Application scope: named reference remains available in the ServletContext until it is reclaimed.

Usage

From source file:org.apache.struts.taglib.logic.TestIterateTag.java

/**
  * Testing <code>IterateTag</code> using name attribute in
  * the application scope.//  w w  w.ja v  a 2s  .c  o m
  * 
 * Tests the equivalent of this tag in a jsp:
 *   <logic:iterate id="theId" name="testApplicationScopeNameIterateArray"
 *       scope="application">
  * 
  */

// ========= Application
public void testApplicationScopeNameIterateArray() throws ServletException, JspException, IOException {

    String testKey = "testApplicationScopeNameIterateArray";

    String[] tst = new String[iterations];
    for (int i = 0; i < tst.length; i++) {
        tst[i] = "test" + i;
    }

    pageContext.setAttribute(testKey, tst, PageContext.APPLICATION_SCOPE);

    IterateTag tag = new IterateTag();
    tag.setPageContext(pageContext);
    tag.setId("theId");
    tag.setName(testKey);
    tag.setScope("application");

    int iteration = 0;
    tag.doStartTag();
    tag.doInitBody();
    do {
        out.print(pageContext.getAttribute("theId"));
        iteration++;

    } while (tag.doAfterBody() == tag.EVAL_BODY_TAG);
    tag.doEndTag();
    assertEquals(iterations, iteration);
}

From source file:org.apache.struts.taglib.logic.TestIterateTag.java

/**
  * Testing <code>IterateTag</code> using property attribute in
  * the application scope.//from w  ww.ja v  a2s  .  co  m
  * 
 * Tests the equivalent of this tag in a jsp:
 *   <logic:iterate id="theId" name="testApplicationScopePropertyIterateArray"
 *       scope="application">
  * 
  */

// ========= Application
public void testApplicationScopePropertyIterateArray() throws ServletException, JspException, IOException {

    String testKey = "testApplicationScopePropertyIterateArray";

    String[] tst = new String[iterations];
    for (int i = 0; i < tst.length; i++) {
        tst[i] = "test" + i;
    }
    SimpleBeanForTesting sbft = new SimpleBeanForTesting();
    sbft.setArray(tst);

    pageContext.setAttribute(testKey, sbft, PageContext.APPLICATION_SCOPE);

    IterateTag tag = new IterateTag();
    tag.setPageContext(pageContext);
    tag.setId("theId");
    tag.setName(testKey);
    tag.setScope("application");
    tag.setProperty("array");

    int iteration = 0;
    tag.doStartTag();
    tag.doInitBody();
    do {
        out.print(pageContext.getAttribute("theId"));
        iteration++;

    } while (tag.doAfterBody() == tag.EVAL_BODY_TAG);
    tag.doEndTag();
    assertEquals(iterations, iteration);
}

From source file:org.apache.struts.taglib.TagUtils.java

/**
 * Return the form action converted into a server-relative URL.
 *//*from   w  ww  . j  ava2 s.c  om*/
public String getActionMappingURL(String action, String module, PageContext pageContext,
        boolean contextRelative) {
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

    String contextPath = request.getContextPath();
    StringBuffer value = new StringBuffer();

    // Avoid setting two slashes at the beginning of an action:
    //  the length of contextPath should be more than 1
    //  in case of non-root context, otherwise length==1 (the slash)
    if (contextPath.length() > 1) {
        value.append(contextPath);
    }

    ModuleConfig moduleConfig = getModuleConfig(module, pageContext);

    if ((moduleConfig != null) && (!contextRelative)) {
        value.append(moduleConfig.getPrefix());
    }

    // Use our servlet mapping, if one is specified
    String servletMapping = (String) pageContext.getAttribute(Globals.SERVLET_KEY,
            PageContext.APPLICATION_SCOPE);

    if (servletMapping != null) {
        String queryString = null;
        int question = action.indexOf("?");

        if (question >= 0) {
            queryString = action.substring(question);
        }

        String actionMapping = getActionMappingName(action);

        if (servletMapping.startsWith("*.")) {
            value.append(actionMapping);
            value.append(servletMapping.substring(1));
        } else if (servletMapping.endsWith("/*")) {
            value.append(servletMapping.substring(0, servletMapping.length() - 2));
            value.append(actionMapping);
        } else if (servletMapping.equals("/")) {
            value.append(actionMapping);
        }

        if (queryString != null) {
            value.append(queryString);
        }
    }
    // Otherwise, assume extension mapping is in use and extension is
    // already included in the action property
    else {
        if (!action.startsWith("/")) {
            value.append("/");
        }

        value.append(action);
    }

    return value.toString();
}

From source file:org.apache.struts.taglib.TagUtils.java

/**
 * Returns the appropriate MessageResources object for the current module
 * and the given bundle.//from   ww w  .  j  ava  2s . c  o m
 *
 * @param pageContext    Search the context's scopes for the resources.
 * @param bundle         The bundle name to look for.  If this is
 *                       <code>null</code>, the default bundle name is
 *                       used.
 * @param checkPageScope Whether to check page scope
 * @return MessageResources The bundle's resources stored in some scope.
 * @throws JspException if the MessageResources object could not be
 *                      found.
 */
public MessageResources retrieveMessageResources(PageContext pageContext, String bundle, boolean checkPageScope)
        throws JspException {
    MessageResources resources = null;

    if (bundle == null) {
        bundle = Globals.MESSAGES_KEY;
    }

    if (checkPageScope) {
        resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.PAGE_SCOPE);
    }

    if (resources == null) {
        resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.REQUEST_SCOPE);
    }

    if (resources == null) {
        ModuleConfig moduleConfig = getModuleConfig(pageContext);

        resources = (MessageResources) pageContext.getAttribute(bundle + moduleConfig.getPrefix(),
                PageContext.APPLICATION_SCOPE);
    }

    if (resources == null) {
        resources = (MessageResources) pageContext.getAttribute(bundle, PageContext.APPLICATION_SCOPE);
    }

    if (resources == null) {
        JspException e = new JspException(messages.getMessage("message.bundle", bundle));

        saveException(pageContext, e);
        throw e;
    }

    return resources;
}

From source file:org.apache.struts.taglib.TestTagUtils.java

public void testMessageApplicationBadKey() {
    putBundleInScope(PageContext.APPLICATION_SCOPE, true);

    String val = null;

    try {//from  w  w w.  j  a va  2 s.  co m
        val = tagutils.message(pageContext, null, null, "foo.bar.does.not.exist");
        assertNull(val);
    } catch (JspException e) {
        fail("val should be null, no exception");
    }
}

From source file:org.apache.struts.taglib.TestTagUtils.java

public void testMessageApplicationGoodKey() {
    putBundleInScope(PageContext.APPLICATION_SCOPE, true);

    String val = null;

    try {//from www . ja  v a  2s.co  m
        val = tagutils.message(pageContext, null, null, "foo");
        assertTrue("Validate message value", "bar".equals(val));
    } catch (JspException e) {
        fail("val should be \"bar\"");
    }
}

From source file:org.apache.struts.taglib.tiles.util.TagUtils.java

/**
 * Store bean in requested context.//w ww  .ja v  a  2s .  c om
 * If scope is <code>null</code>, save it in REQUEST_SCOPE context.
 *
 * @param pageContext Current pageContext.
 * @param name Name of the bean.
 * @param scope Scope under which bean is saved (page, request, session, application)
 *  or <code>null</code> to store in <code>request()</code> instead.
 * @param value Bean value to store.
 *
 * @exception JspException Scope name is not recognized as a valid scope
 */
public static void setAttribute(PageContext pageContext, String name, Object value, String scope)
        throws JspException {

    if (scope == null)
        pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
    else if (scope.equalsIgnoreCase("page"))
        pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE);
    else if (scope.equalsIgnoreCase("request"))
        pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
    else if (scope.equalsIgnoreCase("session"))
        pageContext.setAttribute(name, value, PageContext.SESSION_SCOPE);
    else if (scope.equalsIgnoreCase("application"))
        pageContext.setAttribute(name, value, PageContext.APPLICATION_SCOPE);
    else {
        throw new JspException("Error - bad scope name '" + scope + "'");
    }
}

From source file:org.apache.tiles.jsp.context.JspUtil.java

/**
 * Returns a specific Tiles container./*from  ww w  .  j  a  v  a2  s.c om*/
 *
 * @param context The page context to use.
 * @param key The key under which the container is stored. If null, the
 * default container will be returned.
 * @return The requested Tiles container.
 * @since 2.1.2
 */
public static TilesContainer getContainer(PageContext context, String key) {
    if (key == null) {
        key = TilesAccess.CONTAINER_ATTRIBUTE;
    }
    return (TilesContainer) context.getAttribute(key, PageContext.APPLICATION_SCOPE);
}

From source file:org.apache.tiles.jsp.context.JspUtil.java

/**
 * Configures the container to be used in the application.
 *
 * @param context The page context object to use.
 * @param container The container object to set.
 * @param key The key under which the container will be stored.
 * @since 2.1.2//from   w w w  .jav  a2 s  .  c  om
 */
public static void setContainer(PageContext context, TilesContainer container, String key) {
    Log log = LogFactory.getLog(ServletUtil.class);
    if (key == null) {
        key = TilesAccess.CONTAINER_ATTRIBUTE;
    }

    if (container == null) {
        if (log.isInfoEnabled()) {
            log.info("Removing TilesContext for context: " + context.getClass().getName());
        }
        context.removeAttribute(key, PageContext.APPLICATION_SCOPE);
    }
    if (container != null && log.isInfoEnabled()) {
        log.info("Publishing TilesContext for context: " + context.getClass().getName());
    }
    context.setAttribute(key, container, PageContext.APPLICATION_SCOPE);
}

From source file:org.apache.webapp.admin.AttributeTag.java

/**
 * Render the JMX MBean attribute identified by this tag
 *
 * @exception JspException if a processing exception occurs
 *//*from  ww  w  .  j  av  a2s . co m*/
public int doEndTag() throws JspException {

    // Retrieve the object name identified by our attributes
    Object bean = null;
    if (scope == null) {
        bean = pageContext.findAttribute(name);
    } else if ("page".equalsIgnoreCase(scope)) {
        bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
    } else if ("request".equalsIgnoreCase(scope)) {
        bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
    } else if ("session".equalsIgnoreCase(scope)) {
        bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
    } else if ("application".equalsIgnoreCase(scope)) {
        bean = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
    } else {
        throw new JspException("Invalid scope value '" + scope + "'");
    }
    if (bean == null) {
        throw new JspException("No bean '" + name + "' found");
    }
    if (property != null) {
        try {
            bean = PropertyUtils.getProperty(bean, property);
        } catch (Throwable t) {
            throw new JspException("Exception retrieving property '" + property + "': " + t);
        }
        if (bean == null) {
            throw new JspException("No property '" + property + "' found");
        }
    }

    // Convert to an object name as necessary
    ObjectName oname = null;
    try {
        if (bean instanceof ObjectName) {
            oname = (ObjectName) bean;
        } else if (bean instanceof String) {
            oname = new ObjectName((String) bean);
        } else {
            oname = new ObjectName(bean.toString());
        }
    } catch (Throwable t) {
        throw new JspException("Exception creating object name for '" + bean + "': " + t);
    }

    // Acquire a reference to our MBeanServer
    MBeanServer mserver = (MBeanServer) pageContext.getAttribute("org.apache.catalina.MBeanServer",
            PageContext.APPLICATION_SCOPE);
    if (mserver == null)
        throw new JspException("MBeanServer is not available");

    // Retrieve the specified attribute from the specified MBean
    Object value = null;
    try {
        value = mserver.getAttribute(oname, attribute);
    } catch (Throwable t) {
        throw new JspException("Exception retrieving attribute '" + attribute + "'");
    }

    // Render this value to our current output writer
    if (value != null) {
        JspWriter out = pageContext.getOut();
        try {
            out.print(value);
        } catch (IOException e) {
            throw new JspException("IOException: " + e);
        }
    }

    // Evaluate the remainder of this page
    return (EVAL_PAGE);

}