JSP定制标记——通过pageContext访问Attribute值

Tag9.java

package tag9;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.Enumeration;

public class Tag9 extends TagSupport {
    public int doStartTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            out.println(this.getAttributes());
        } catch (IOException ioe) {
            throw new JspException(ioe.getMessage());  
        }
        return SKIP_BODY;  
    }  
    private String getAttributes() {
        StringBuffer returns = new StringBuffer();
        int[] scopes = {pageContext.PAGE_SCOPE,
                        pageContext.REQUEST_SCOPE,
                        pageContext.SESSION_SCOPE,
                        pageContext.APPLICATION_SCOPE
        };
        String[] strScopes = {"page_scope",
                              "request_scope",
                              "session_scope",
                              "application_scope"
        };
                                
        for (int i = 0; i < scopes.length; i++) {
            returns.append("<p>" + strScopes[i] + ":<br>");
            for (Enumeration  enums = pageContext.getAttributeNamesInScope(scopes[i]); enums.hasMoreElements(); ) {
                String attributeName = (String) enums.nextElement();
                Object attributeValue = pageContext.findAttribute(attributeName);
                returns.append(attributeName + " = [" + attributeValue.getClass().getName() + "] " + attributeValue + "<br>");  
            }
        }
        return returns.toString();
    }
}

tag9.jsp

<%@ page contentType="text/html;charset=GBK" %>
<%@ taglib prefix="tag" uri="tag9.tld" %>

<html>
    <body>
        <%
            pageContext.setAttribute("attr1", "aaaaaaaaaaaa");
            pageContext.setAttribute("attr2", new Integer(213443));
            pageContext.setAttribute("attr7", "ggggggggggggggggg");
            request.setAttribute("attr3","bbbbbbbbbbbbb");
            request.setAttribute("attr4", new Float(23.34));
            request.setAttribute("attr5", "ffffffffffffff");
            session.setAttribute("attr5", "cccccccccccccccc");
            session.setAttribute("attr6", new Boolean(true));
            session.setAttribute("attr7", "eeeeeeeeeeeeeeee");
            application.setAttribute("attr7", "dddddddddddddddd");
            application.setAttribute("attr8", new Byte((byte)12));
        %>
        <tag:tag9 />
    </body>
</html>

 

 

结果:

page_scope:
javax.servlet.jsp.jspOut = [org.apache.jasper.runtime.JspWriterImpl] org.apache.jasper.runtime.JspWriterImpl@1dc64a5
javax.servlet.jsp.jspPage = [org.apache.jsp.tag9_jsp] org.apache.jsp.tag9_jsp@53c3f5
javax.servlet.jsp.jspSession = [org.apache.catalina.session.StandardSessionFacade] org.apache.catalina.session.StandardSessionFacade@166340c
attr7 = [java.lang.String] ggggggggggggggggg
attr2 = [java.lang.Integer] 213443
javax.servlet.jsp.jspApplication = [org.apache.catalina.core.ApplicationContextFacade] org.apache.catalina.core.ApplicationContextFacade@e33e18
javax.servlet.jsp.jspPageContext = [org.apache.jasper.runtime.PageContextImpl] org.apache.jasper.runtime.PageContextImpl@101ac93
javax.servlet.jsp.jspConfig = [org.apache.catalina.core.StandardWrapperFacade] org.apache.catalina.core.StandardWrapperFacade@e90943
attr1 = [java.lang.String] aaaaaaaaaaaa
javax.servlet.jsp.jspResponse = [org.apache.coyote.tomcat5.CoyoteResponseFacade] org.apache.coyote.tomcat5.CoyoteResponseFacade@15f1f9c
javax.servlet.jsp.jspRequest = [org.apache.coyote.tomcat5.CoyoteRequestFacade] org.apache.coyote.tomcat5.CoyoteRequestFacade@1474ea

request_scope:
attr5 = [java.lang.String] ffffffffffffff
attr4 = [java.lang.Float] 23.34
attr3 = [java.lang.String] bbbbbbbbbbbbb

session_scope:
attr7 = [java.lang.String] ggggggggggggggggg
attr5 = [java.lang.String] ffffffffffffff
attr6 = [java.lang.Boolean] true

application_scope:
org.apache.catalina.jsp_classpath = [java.lang.String] /K:/tomcat/webapps/tags/WEB-INF/classes/;K:/tomcat/shared/classes/;K:/tomcat/common/classes/;K:/tomcat/common/endorsed/xercesImpl.jar;K:/tomcat/common/endorsed/xml-apis.jar;K:/tomcat/common/lib/ant-launcher.jar;K:/tomcat/common/lib/ant.jar;K:/tomcat/common/lib/commons-collections-2.1.1.jar;K:/tomcat/common/lib/commons-dbcp-1.2.1.jar;K:/tomcat/common/lib/commons-el.jar;K:/tomcat/common/lib/commons-pool-1.2.jar;K:/tomcat/common/lib/jasper-compiler.jar;K:/tomcat/common/lib/jasper-runtime.jar;K:/tomcat/common/lib/jsp-api.jar;K:/tomcat/common/lib/naming-common.jar;K:/tomcat/common/lib/naming-factory.jar;K:/tomcat/common/lib/naming-java.jar;K:/tomcat/common/lib/naming-resources.jar;K:/tomcat/common/lib/servlet-api.jar;K:/tomcat/common/lib/j2ee.jar;K:/tomcat/common/lib/mysql-connector-java-3.0.15-ga-bin.jar;/K:/j2se/lib/tools.jar;/K:/tomcat/bin/bootstrap.jar;/K:/j2se/jre/lib/ext/dnsns.jar;/K:/j2se/jre/lib/ext/ldapsec.jar;/K:/j2se/jre/lib/ext/localedata.jar;/K:/j2se/jre/lib/ext/sunjce_provider.jar
javax.servlet.context.tempdir = [java.io.File] K:/tomcat/work/Catalina/localhost/tags
attr7 = [java.lang.String] ggggggggggggggggg
org.apache.catalina.resources = [org.apache.naming.resources.ProxyDirContext] org.apache.naming.resources.ProxyDirContext@107bd0d
attr8 = [java.lang.Byte] 12
org.apache.catalina.WELCOME_FILES = [[Ljava.lang.String;] [Ljava.lang.String;@10ca208

 

分析:

1、在page_scope和application_scope中保存有很多服务器预定义的Attribute。

2、对于PageContext的findAttribute()方法。它会从page_scope开始,依次寻找与key匹配的value值。如果不同scope中的key重名,较低scope中的attribute将覆盖较高scope中的对应值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值