我写了两个文件,分别为Counter.jsp和hello.jsp.源文件如下:hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head><title>ch8</title></head>
<body>
Hello...<br>
欢迎<jsp:getProperty name = "cBean" property = "count"/>次访问!
</body>
</html>Counter.jsp文件
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="cBean" scope = "request" class = "my.Counter" ></jsp:useBean>
欢迎回来!<br>
您是第<font color = "red"><jsp:getProperty name = "cBean" property ="count"/></font>访问本站<br><br>
<jsp:include page="hello.jsp" flush="true"/>
</body>
</html>
运行时报错:
org.apache.jasper.JasperException: Attempted a bean operation on a null object.
org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(JspRuntimeLibrary.java:603)
org.apache.jsp.ch8_002djavaBean.hello_jsp._jspService(hello_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
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)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:696)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:667)
org.apache.jsp.ch8_002djavaBean.counter_jsp._jspService(counter_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
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)javaBean为
package my;public class Counter { private int count = 0;
public Counter(){
}
public void setCount(int newCount){
count = newCount;
}
public int getCount(){
return ++count;
}
}请问怎样修改?谢谢!
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head><title>ch8</title></head>
<body>
Hello...<br>
欢迎<jsp:getProperty name = "cBean" property = "count"/>次访问!
</body>
</html>Counter.jsp文件
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="cBean" scope = "request" class = "my.Counter" ></jsp:useBean>
欢迎回来!<br>
您是第<font color = "red"><jsp:getProperty name = "cBean" property ="count"/></font>访问本站<br><br>
<jsp:include page="hello.jsp" flush="true"/>
</body>
</html>
运行时报错:
org.apache.jasper.JasperException: Attempted a bean operation on a null object.
org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(JspRuntimeLibrary.java:603)
org.apache.jsp.ch8_002djavaBean.hello_jsp._jspService(hello_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
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)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:696)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:667)
org.apache.jsp.ch8_002djavaBean.counter_jsp._jspService(counter_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
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)javaBean为
package my;public class Counter { private int count = 0;
public Counter(){
}
public void setCount(int newCount){
count = newCount;
}
public int getCount(){
return ++count;
}
}请问怎样修改?谢谢!
<%@ include file="....."%>这种的。
Attempted a bean operation on a null object
这句明明就是说你调用了null对象上的方法。
你看看这句吧
<jsp:useBean id="cBean" scope = "request" class = "my.Counter" ></jsp:useBean>
cBean应该为null
只有加上<jsp:include>标签,才能看出request
和page范围中的javaBean的不同之处。改为include指令运行后达到期望的目的,估计是
书上的错误!
使用Counter.jsp中的javaBean,这样<jsp:useBean>
中的scope的作用就没有体现出来!! 大家再看看吧,