我写了两个文件,分别为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;
}
}请问怎样修改?谢谢!

解决方案 »

  1.   

    把include操作换成include指令
    <%@ include file="....."%>这种的。
      

  2.   

    至于上面的include操作和include指令的差别,你找本书参考一下吧。
      

  3.   

    不是你include的问题吧??
    Attempted a bean operation on a null object
    这句明明就是说你调用了null对象上的方法。
    你看看这句吧
    <jsp:useBean id="cBean" scope = "request" class = "my.Counter" ></jsp:useBean>
    cBean应该为null
      

  4.   

    改用<%@ include file="hello.jsp"%>,并把hello.jsp的“<%@ page" 这行去掉。 jsp:include  方式是jsp运算完毕后再包含,在运算时cbean是空的
      

  5.   

    这是jsp2.0技术手册的一个例子。书上用它说明
    只有加上<jsp:include>标签,才能看出request
    和page范围中的javaBean的不同之处。改为include指令运行后达到期望的目的,估计是
    书上的错误!
      

  6.   

       错了,如果用include指令,那么被包含的网页均可
    使用Counter.jsp中的javaBean,这样<jsp:useBean>
    中的scope的作用就没有体现出来!!
      大家再看看吧,
      

  7.   

    在hello.jsp中加上这句 <jsp:useBean id="cBean" scope = "request" class = "my.Counter" ></jsp:useBean>