Example usage for javax.servlet.jsp PageContext REQUEST_SCOPE

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

Introduction

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

Prototype

int REQUEST_SCOPE

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

Click Source Link

Document

Request scope: the named reference remains available from the ServletRequest associated with the Servlet until the current request is completed.

Usage

From source file:org.openmrs.module.htmlformflowsheet.web.taglibs.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.REQUEST_SCOPE);

        // retrieve the htmlinclude map from the page request
        //HashMap<String,String> hmIncludeMap = (HashMap<String, String>) initialRequest.getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_KEY);
        HashMap<String, String> hmIncludeMap = (HashMap<String, String>) pageContext
                .getAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_MAP_KEY, PageContext.REQUEST_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   w  w  w  . j  ava 2s  .c om*/
            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.REQUEST_SCOPE);

            // save the name of the initial page 
            pageContext.setAttribute(HtmlIncludeTag.OPENMRS_HTML_INCLUDE_REQUEST_ID_KEY, initialRequestId,
                    PageContext.REQUEST_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.ja v  a  2 s.co  m*/

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.  j  av a 2  s.c  o m*/

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

From source file:org.seasar.struts.taglib.S2FormTag.java

/**
 * Render the beginning of this form.//from ww  w.j a  v  a  2  s.co m
 *
 * @exception JspException if a JSP exception has occurred
 */
public int doStartTag() throws JspException {

    // Look up the form bean name, scope, and type if necessary
    this.lookup();

    // Create an appropriate "form" element based on our parameters
    StringBuffer results = new StringBuffer();

    results.append(this.renderFormStartElement());
    results.append(this.renderToken());

    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e);
    }

    // Store this tag itself as a page attribute
    pageContext.setAttribute(Constants.FORM_KEY, this, PageContext.REQUEST_SCOPE);

    this.initFormBean();

    return (EVAL_BODY_INCLUDE);

}

From source file:org.seasar.struts.taglib.S2FormTag.java

/**
 * Render the end of this form./*from ww  w . j a  va2  s.c om*/
 *
 * @exception JspException if a JSP exception has occurred
 */
public int doEndTag() throws JspException {

    // Remove the page scope attributes we created
    pageContext.removeAttribute(BEAN_KEY, PageContext.REQUEST_SCOPE);
    pageContext.removeAttribute(FORM_KEY, PageContext.REQUEST_SCOPE);

    // Render a tag representing the end of our current form
    StringBuilder results = new StringBuilder("</form>");

    // Render JavaScript to set the input focus if required
    if (this.focus != null) {
        results.append(this.renderFocusJavascript());
    }

    // Print this value to our output writer
    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e.toString(), e);
    }

    // Continue processing this page
    return (EVAL_PAGE);

}

From source file:org.seasar.struts.taglib.S2FormTag.java

protected void lookup() throws JspException {
    if (action == null) {
        action = ActionUtil.calcActionPath();
    } else if (!action.startsWith("/")) {
        action = ActionUtil.calcActionPath() + action;
    }// w ww .  jav a  2 s  .  c  o m
    String path = action;
    String queryString = "";
    int index = action.indexOf('?');
    if (index >= 0) {
        path = action.substring(0, index);
        queryString = action.substring(index);
    }
    String[] names = StringUtil.split(path, "/");
    S2Container container = SingletonS2ContainerFactory.getContainer();
    StringBuilder sb = new StringBuilder(50);
    ComponentDef actionComponentDef = null;
    for (int i = 0; i < names.length; i++) {
        if (container.hasComponentDef(sb + names[i] + "Action")) {
            actionComponentDef = container.getComponentDef(sb + names[i] + "Action");
            String actionPath = RoutingUtil.getActionPath(names, i);
            String paramPath = RoutingUtil.getParamPath(names, i + 1);
            if (StringUtil.isEmpty(paramPath)) {
                action = actionPath + "/" + queryString;
            }
            break;
        }
        if (container.hasComponentDef(sb + "indexAction")) {
            actionComponentDef = container.getComponentDef(sb + "indexAction");
            String actionPath = RoutingUtil.getActionPath(names, i - 1) + "/index";
            String paramPath = RoutingUtil.getParamPath(names, i);
            break;
        }
        sb.append(names[i] + "_");
    }
    if (actionComponentDef == null) {
        JspException e = new JspException(action);
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
        throw e;
    }
    IFn findActionConfig = Clojure.var("sa-compojure.action-customizer", "find-action-config");
    IPersistentMap actionConfig = (IPersistentMap) findActionConfig.invoke(actionComponentDef);
    this.beanName = ((IPersistentMap) actionConfig.valAt(Keyword.intern("action-mapping")))
            .valAt(Keyword.intern("name")).toString();
}

From source file:org.seasar.struts.taglib.S2FormTag.java

protected void initFormBean() throws JspException {
    int scope = PageContext.SESSION_SCOPE;
    if ("request".equalsIgnoreCase(beanScope)) {
        scope = PageContext.REQUEST_SCOPE;
    }//from  www .j a  v a 2  s  .  c om

    Object bean = pageContext.getAttribute(beanName, scope);
    if (bean == null) {
        bean = pageContext.getRequest().getAttribute(beanName);
        if (bean == null) {
            throw new JspException("ActionForm not found.");
        }
    }

    pageContext.setAttribute(Constants.BEAN_KEY, bean, PageContext.REQUEST_SCOPE);
}

From source file:org.squale.welcom.taglib.field.FieldTag.java

/**
 * @throws JspException exception pouvant etre levee
 * @return the first error associated with the current property if there is one
 *//*  w w  w .jav a 2  s .co m*/
protected String retrieveError() throws JspException {
    final ActionErrors errors = (ActionErrors) pageContext.getAttribute(Globals.ERROR_KEY,
            PageContext.REQUEST_SCOPE);
    error = null;

    if ((errors != null) && !errors.isEmpty()) {
        final Iterator it = errors.get(property);

        if (it.hasNext()) {
            final ActionError report = (ActionError) it.next();
            error = LayoutUtils.getLabel(pageContext, report.getKey(), report.getValues());
        }
    }

    return error;
}

From source file:org.theospi.portfolio.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;

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

From source file:us.mn.state.health.lims.taglib.JavascriptValidatorTag.java

/**
 * Generates the dynamic JavaScript for the form.
 * @param config//from ww w.  j  av a 2s.c  o  m
 * @param resources
 * @param locale
 * @param form
 */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale,
        Form form) throws JspException {

    StringBuffer results = new StringBuffer();

    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);

    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();

    List actions = this.createActionList(resources, form);

    final String methods = this.createMethods(actions, this.stopOnError(config));

    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }

    results.append(this.getJavascriptBegin(methods));

    for (Iterator i = actions.iterator(); i.hasNext();) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;

        if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }

        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext();) {
            Field field = (Field) x.next();
            // Skip indexed fields for now until there is a good way to handle
            // error messages (and the length of the list (could retrieve from scope?))
            if (field.isIndexed() || field.getPage() != page || !field.isDependency(va.getName())) {

                continue;
            }

            String message = Resources.getMessage(application, request, messages, locale, va, field);

            message = (message != null) ? message : "";

            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \""
                    + escapeQuotes(message) + "\", ");

            results.append("new Function (\"varName\", \"");

            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = null;
                //bugzilla 1512
                if (varName.startsWith("datePattern")) {
                    if (var.getValue().equals("date.format.validate")) {
                        String messageKey = var.getValue();
                        varValue = ResourceLocator.getInstance().getMessageResources().getMessage(locale,
                                messageKey);
                    } else {
                        varValue = var.getValue();
                    }
                } else {
                    varValue = var.getValue();
                }
                String jsType = var.getJsType();

                // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }

                String varValueEscaped = ValidatorUtils.replace(varValue, "\\", "\\\\");
                varValueEscaped = ValidatorUtils.replace(varValueEscaped, "\"", "\\\"");

                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                    // So everyone using the latest format doesn't need to change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }

            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }

    return results.toString();
}