关于ServletContext中getAttributeNames()方法类型转换的问题

初学ServletContext,用getAttribute()方法设置了两个属性和对应的属性值,本来想用getAttributeNames()方法获得,由于设置的两个值是String,于是直接把getAttribute()强转为String,运行时候出现错误。
Enumeration en=con.getAttributeNames();
		while(en.hasMoreElements())
		{
			String name=(String) en.nextElement();
			Object str= con.getAttribute(name);
			System.out.println(str.toString());
		}

是因为ServletContext对象在Tomcat服务器启动时候已经含有一些属性信息了,不只是String类型的,所以强转时候出错。

下面是打印出来的隐含自带信息。



[Ljava.lang.String;@67be341e
----------------------------------------------
E:\apache-tomcat-6.0.44\work\Catalina\localhost\Aweb
----------------------------------------------
/E:/apache-tomcat-6.0.44/webapps/Aweb/WEB-INF/classes/;/E:/apache-tomcat-6.0.44/webapps/Aweb/WEB-INF/lib/jsf-api.jar;/E:/apache-tomcat-6.0.44/webapps/Aweb/WEB-INF/lib/jsf-impl.jar;/E:/apache-tomcat-6.0.44/webapps/Aweb/WEB-INF/lib/jstl-1.2.jar;/E:/apache-tomcat-6.0.44/lib/;/E:/apache-tomcat-6.0.44/lib/annotations-api.jar;/E:/apache-tomcat-6.0.44/lib/catalina-ant.jar;/E:/apache-tomcat-6.0.44/lib/catalina-ha.jar;/E:/apache-tomcat-6.0.44/lib/catalina-tribes.jar;/E:/apache-tomcat-6.0.44/lib/catalina.jar;/E:/apache-tomcat-6.0.44/lib/ecj-4.3.1.jar;/E:/apache-tomcat-6.0.44/lib/el-api.jar;/E:/apache-tomcat-6.0.44/lib/jasper-el.jar;/E:/apache-tomcat-6.0.44/lib/jasper.jar;/E:/apache-tomcat-6.0.44/lib/jsp-api.jar;/E:/apache-tomcat-6.0.44/lib/servlet-api.jar;/E:/apache-tomcat-6.0.44/lib/tomcat-coyote.jar;/E:/apache-tomcat-6.0.44/lib/tomcat-dbcp.jar;/E:/apache-tomcat-6.0.44/lib/tomcat-i18n-es.jar;/E:/apache-tomcat-6.0.44/lib/tomcat-i18n-fr.jar;/E:/apache-tomcat-6.0.44/lib/tomcat-i18n-ja.jar;/E:/apache-tomcat-6.0.44/bin/bootstrap.jar;/D:/Java%20jdk/lib/tools.jar;/D:/Java%20jdk/jre/lib/ext/access-bridge-64.jar;/D:/Java%20jdk/jre/lib/ext/dnsns.jar;/D:/Java%20jdk/jre/lib/ext/jaccess.jar;/D:/Java%20jdk/jre/lib/ext/localedata.jar;/D:/Java%20jdk/jre/lib/ext/sunec.jar;/D:/Java%20jdk/jre/lib/ext/sunjce_provider.jar;/D:/Java%20jdk/jre/lib/ext/sunmscapi.jar;/D:/Java%20jdk/jre/lib/ext/zipfs.jar
----------------------------------------------
org.apache.naming.resources.ProxyDirContext@784fd2c7
----------------------------------------------
com.sun.faces.config.WebConfiguration@40974486
----------------------------------------------

org.apache.catalina.util.DefaultAnnotationProcessor@b13da8b
----------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值