<%@ page contentType="text/html;charset=gb2312" import="java.util.Enumeration"%>
<html>
<head>
<title>设置属性</title>
</head>
<body>
<h1 align="center">获取属性</h1>
<hr>
<p>
session:
</p>
<%
String a1=session.getAttribute("user");
out.println("a1");
%>
<p>
application:
</p>
<%
String a2=application.getAttribute("user");
out.println("a2");
%>
<hr>
<%
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements())
{
 String str=e.nextElement();
 String str1=session.getAttribute("str");
 out.println("str"+":"+"str1"+"<br>");
}
%>
<%
Enumeration e1=application.getAttributeNames();
while(e.hasMoreElements())
{
 String str=e1.nextElement();
 String str1=application.getAttribute("str");
 out.println("str"+":"+"str1"+"<br>");
}
%>
</body>
</html>
之前的<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>设置属性</title>
</head>
<body>
<h1 align="center">获取属性</h1>
<hr>
<%
session.setAttribute("user","tom");
application.setAttribute("user","jim");
%>
</body>
</html>能运行,是调用他的SET,关键是第一个程序运行不了
String str=String.valueOf(e1.nextElement());这条怎么理解呢?
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 13 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
10: session:
11: </p>
12: <%
13: String a1=session.getAttribute("user");
14: out.println("a1");
15: %>
16: <p>
An error occurred at line: 20 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
17: application:
18: </p>
19: <%
20: String a2=application.getAttribute("user");
21: out.println("a2");
22: %>
23: <hr>
An error occurred at line: 28 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
25: Enumeration e=session.getAttributeNames();
26: while(e.hasMoreElements())
27: {
28:  String str=e.nextElement();
29:  String str1=session.getAttribute("str");
30:  out.println("str"+":"+"str1"+"<br>");
31: }
An error occurred at line: 29 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
26: while(e.hasMoreElements())
27: {
28:  String str=e.nextElement();
29:  String str1=session.getAttribute("str");
30:  out.println("str"+":"+"str1"+"<br>");
31: }
32: %>
An error occurred at line: 37 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
34: Enumeration e1=application.getAttributeNames();
35: while(e.hasMoreElements())
36: {
37:  String str=e1.nextElement();
38:  String str1=application.getAttribute("str");
39:  out.println("str"+":"+"str1"+"<br>");
40: }
An error occurred at line: 38 in the jsp file: /2.jsp
Type mismatch: cannot convert from Object to String
35: while(e.hasMoreElements())
36: {
37:  String str=e1.nextElement();
38:  String str1=application.getAttribute("str");
39:  out.println("str"+":"+"str1"+"<br>");
40: }
41: %>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.

解决方案 »

  1.   

    session.getAttribute、request.getAttribute这些方法的返回值均为Object类型,要转成其它类型必须强制转换
    String a1=(String)session.getAttribute("user");
      

  2.   

    String str=String.valueOf(e1.nextElement());这条怎么理解呢? 你可以理解为将Object类型,要转换为String类型
    其它问题同意1楼的
      

  3.   

    String.valueOf()是把别的类型转化为string类型.application.session,request.getAttribute("user");返回的全是Object类型的数据。
    你要是知道数据时String类型的话,可以直接强制转换下
    =(String)request.getAttribute("user");