Example usage for javax.servlet.jsp PageContext SESSION_SCOPE

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

Introduction

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

Prototype

int SESSION_SCOPE

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

Click Source Link

Document

Session scope (only valid if this page participates in a session): the named reference remains available from the HttpSession (if any) associated with the Servlet until the HttpSession is invalidated.

Usage

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

public void testSessionScopePropertyIterateArray() throws ServletException, JspException, IOException {

    String testKey = "testSessionScopePropertyIterateArray";

    String[] tst = new String[iterations];
    for (int i = 0; i < tst.length; i++) {
        tst[i] = "test" + i;
    }/* www  .ja va2  s. co  m*/

    SimpleBeanForTesting sbft = new SimpleBeanForTesting();
    sbft.setArray(tst);

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

    IterateTag tag = new IterateTag();
    tag.setPageContext(pageContext);
    tag.setId("theId");
    tag.setName(testKey);
    tag.setScope("session");
    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.TestTagUtils.java

public void testMessageSessionBadKey() {
    putBundleInScope(PageContext.SESSION_SCOPE, true);

    String val = null;

    try {/*w w  w. j  ava2  s  . c om*/
        val = tagutils.message(pageContext, null, null, "foo.bar.does.not.exist");
        fail("MessageResources should never be put in session scope.");
    } catch (JspException e) {
        assertNull(val);
    }
}

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

/**
 * Store bean in requested context.//  w  ww .java 2s  . c o m
 * 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.webapp.admin.AttributeTag.java

/**
 * Render the JMX MBean attribute identified by this tag
 *
 * @exception JspException if a processing exception occurs
 *//*from w w w .  j  av  a 2 s. 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);

}

From source file:org.hdiv.taglib.html.HiddenTag2Test.java

private void runMyTest(String whichTest, String locale) {

    pageContext.setAttribute(Globals.LOCALE_KEY, new Locale(locale, locale), PageContext.SESSION_SCOPE);
    pageContext.setAttribute(Constants.BEAN_KEY, new SimpleBeanForTesting("Test Value"),
            PageContext.REQUEST_SCOPE);/*w  ww  . j a va 2s . c o  m*/
    request.setAttribute("runTest", whichTest);
    try {
        pageContext.forward("/test/org/hdiv/taglib/html/TestHiddenTag2.jsp");
    } catch (Exception e) {
        e.printStackTrace();
        fail("There is a problem that is preventing the tests to continue!");
    }
}

From source file:org.hyperic.hq.ui.taglib.display.TableTag.java

/**
 * This functionality is borrowed from struts, but I've removed some struts
 * specific features so that this tag can be used both in a struts
 * application, and outside of one./*from   ww  w.jav a  2 s .c  o  m*/
 * 
 * Locate and return the specified bean, from an optionally specified scope,
 * in the specified page context. If no such bean is found, return
 * <code>null</code> instead.
 * 
 * @param pageContext
 *            Page context to be searched
 * @param name
 *            Name of the bean to be retrieved
 * @param scope
 *            Scope to be searched (page, request, session, application) or
 *            <code>null</code> to use <code>findAttribute()</code> instead
 * 
 * @exception JspException
 *                if an invalid scope name is requested
 */

public Object lookup(PageContext pageContext, String name, String scope) throws JspException {

    Object bean = null;
    if (scope == null)
        bean = pageContext.findAttribute(name);
    else if (scope.equalsIgnoreCase("page"))
        bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
    else if (scope.equalsIgnoreCase("request"))
        bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
    else if (scope.equalsIgnoreCase("session"))
        bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
    else if (scope.equalsIgnoreCase("application"))
        bean = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
    else {
        Object[] objs = { name, scope };
        if (prop.getProperty("error.msg.cant_find_bean") != null) {
            String msg = MessageFormat.format(prop.getProperty("error.msg.cant_find_bean"), objs);
            throw new JspException(msg);
        } else {
            throw new JspException("Could not find " + name + " in scope " + scope);

        }
    }

    return (bean);
}

From source file:org.openmrs.web.taglib.HtmlIncludeTag.java

@Override
public synchronized int doStartTag() throws JspException {
    log.debug("\n\n");

    if (rewrites.containsKey(file)) {
        file = rewrites.get(file);/* ww  w  . ja v  a  2 s  .  c  om*/
    }

    // see if this is a JS or CSS file
    boolean isJs = false;
    boolean isCss = false;

    String fileExt = file.substring(file.lastIndexOf("."));

    if (this.type != null && this.type.length() > 0) {
        if (HtmlIncludeTag.POSSIBLE_TYPES_CSS.indexOf(type) >= 0) {
            isCss = true;
        } else if (HtmlIncludeTag.POSSIBLE_TYPES_JS.indexOf(type) >= 0) {
            isJs = true;
        }
    }

    if (!isCss && !isJs && fileExt.length() > 0) {
        if (HtmlIncludeTag.POSSIBLE_TYPES_CSS.indexOf(fileExt) >= 0) {
            isCss = true;
        } else if (HtmlIncludeTag.POSSIBLE_TYPES_JS.indexOf(fileExt) >= 0) {
            isJs = true;
        }
    }

    if (isJs || isCss) {
        String initialRequestId = getInitialRequestUniqueId();
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        if (log.isDebugEnabled()) {
            log.debug("initialRequest id: [" + initialRequestId + "]");
            log.debug("Object at pageContext." + HtmlIncludeTag.OPENMRS_HTML_INCLUDE_MAP_KEY + " is "
                    + pageContext.getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_MAP_KEY,
                            PageContext.SESSION_SCOPE)
                    + "");
        }

        if (!isAlreadyUsed(file, initialRequestId)) {
            StringBuilder output = new StringBuilder();
            String prefix = "";
            try {
                prefix = request.getContextPath();
                if (file.startsWith(prefix + "/")) {
                    prefix = "";
                }
            } catch (ClassCastException cce) {
                log.debug("Could not cast request to HttpServletRequest in HtmlIncludeTag");
            }

            // the openmrs version is inserted into the file src so that js and css files are cached across version releases
            if (isJs) {
                output.append("<script src=\"").append(prefix).append(file);
                output.append("?v=").append(OpenmrsConstants.OPENMRS_VERSION_SHORT);
                if (appendLocale) {
                    output.append("&locale=").append(Context.getLocale());
                }
                output.append("\" type=\"text/javascript\" ></script>");
            } else if (isCss) {
                output.append("<link href=\"").append(prefix).append(file);
                output.append("?v=").append(OpenmrsConstants.OPENMRS_VERSION_SHORT);
                if (appendLocale) {
                    output.append("&locale=").append(Context.getLocale());
                }
                output.append("\" type=\"text/css\" rel=\"stylesheet\" />");
            }

            if (log.isDebugEnabled()) {
                log.debug("isAlreadyUsed() is FALSE - printing " + this.file + " to output.");
            }

            try {
                pageContext.getOut().print(output.toString());
            } catch (IOException e) {
                log.debug("Could not produce output in HtmlIncludeTag.java");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("isAlreadyUsed() is TRUE - suppressing file print for " + this.file + "");
            }
        }
    }

    resetValues();

    return SKIP_BODY;
}

From source file:org.openmrs.web.taglib.HtmlIncludeTag.java

@SuppressWarnings("unchecked")
private boolean isAlreadyUsed(String fileName, String initialRequestId) {
    boolean isUsed = false;

    if (fileName != null) {
        log.debug("initialRequestId: " + initialRequestId);

        // retrieve the request id that the last mapping was added for
        String lastRequestId = (String) pageContext
                .getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_REQUEST_ID_KEY, PageContext.SESSION_SCOPE);

        // retrieve the htmlinclude map from the page request
        //HashMap<String,String> hmIncludeMap = (HashMap<String, String>) initialRequest.getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_KEY);
        Map<String, String> hmIncludeMap = (Map<String, String>) pageContext
                .getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_MAP_KEY, PageContext.SESSION_SCOPE);

        // reset the hmIncludeMap if not found or if not on the initial request anymore
        if (hmIncludeMap == null || !initialRequestId.equals(lastRequestId)) {
            log.debug("Creating new hmIncludeMap");
            hmIncludeMap = new HashMap<String, String>();
        } else {//from  ww w  .  j  ava 2 s  .  c o m
            log.debug("Using hmIncludeMap from object");
        }

        if (hmIncludeMap.containsKey(fileName)) {
            log.debug("HTMLINCLUDETAG HAS ALREADY INCLUDED FILE " + fileName);
            isUsed = true;
        } else {
            log.debug("HTMLINCLUDETAG IS WRITING HTML TO INCLUDE FILE " + fileName);
            log.debug("HashCode for file is " + fileName.hashCode());

            hmIncludeMap.put(fileName, "true");

            // save the hmIncludeMap to the
            pageContext.setAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_MAP_KEY, hmIncludeMap,
                    PageContext.SESSION_SCOPE);

            // save the name of the initial page
            pageContext.setAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_REQUEST_ID_KEY, initialRequestId,
                    PageContext.SESSION_SCOPE);
        }
    }

    return isUsed;
}

From source file:org.rhq.enterprise.gui.legacy.taglib.display.TableTag.java

/**
 * This functionality is borrowed from struts, but I've removed some struts specific features so that this tag can
 * be used both in a struts application, and outside of one.
 *
 * <p/>Locate and return the specified bean, from an optionally specified scope, in the specified page context. If
 * no such bean is found, return <code>null</code> instead.
 *
 * @param  pageContext Page context to be searched
 * @param  name        Name of the bean to be retrieved
 * @param  scope       Scope to be searched (page, request, session, application) or <code>null</code> to use <code>
 *                     findAttribute()</code> instead
 *
 * @throws JspException if an invalid scope name is requested
 *///from  ww  w  . j a  v a 2 s .  c om

public Object lookup(PageContext pageContext, String name, String scope) throws JspException {
    log.trace("looking up: " + name + " in scope: " + scope);

    Object bean;
    if (scope == null) {
        bean = pageContext.findAttribute(name);
    } else if (scope.equalsIgnoreCase("page")) {
        bean = pageContext.getAttribute(name, PageContext.PAGE_SCOPE);
    } else if (scope.equalsIgnoreCase("request")) {
        bean = pageContext.getAttribute(name, PageContext.REQUEST_SCOPE);
    } else if (scope.equalsIgnoreCase("session")) {
        bean = pageContext.getAttribute(name, PageContext.SESSION_SCOPE);
    } else if (scope.equalsIgnoreCase("application")) {
        bean = pageContext.getAttribute(name, PageContext.APPLICATION_SCOPE);
    } else {
        Object[] objs = { name, scope };
        if (prop.getProperty("error.msg.cant_find_bean") != null) {
            String msg = MessageFormat.format(prop.getProperty("error.msg.cant_find_bean"), objs);
            throw new JspException(msg);
        } else {
            throw new JspException("Could not find " + name + " in scope " + scope);
        }
    }

    return (bean);
}

From source file:org.sakaiproject.metaobj.security.control.tag.AuthZMapTag.java

public void setScope(String scope) {
    if (scope.equalsIgnoreCase("page")) {
        this.scope = PageContext.PAGE_SCOPE;
    } else if (scope.equalsIgnoreCase("request")) {
        this.scope = PageContext.REQUEST_SCOPE;
    } else if (scope.equalsIgnoreCase("session")) {
        this.scope = PageContext.SESSION_SCOPE;
    } else if (scope.equalsIgnoreCase("application")) {
        this.scope = PageContext.APPLICATION_SCOPE;
    }//from  w  w  w . ja  va2  s . c  o  m

    // TODO: Add error handling?  Needs direction from spec.
}