`
zhoujianghai
  • 浏览: 439097 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Attempted a bean operation on a null object(tomcat5.0以上版本)

阅读更多
今天做scwcd模拟题的时候,碰到这样一个题目:
A servlet sets a session-scoped attribute product with an instance of com.example.Product and
forwards to a JSP.
Which two output the name of the product in the response? (Choose two.)

A. ${product.name}
B. <jsp:getProperty name="product" property="name" />
C. <jsp:useBean id="com.example.Product" />
<%= product.getName() %>
D. <jsp:getProperty name="product" class="com.example.Product"
property="name" />
E. <jsp:useBean id="product" type="com.example.Product">
<%= product.getName() %>
</jsp:useBean>

答案:AB

我测试用的Product.java代码大致如下:
public class Product {

private String name;

public Product() {
this("123");
}
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

我测试的时候写的servlet如下:
public class Test2 extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

req.getSession().setAttribute("product", new Product("123"));
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}

}

index.jsp主要代码如下:
  <jsp:getProperty name="product" property="name"/>

没想到出现了Attempted a bean operation on a null object异常。
查看index_jsp.java时,发现这样一段代码:
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString(org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(_jspx_page_context.getAttribute("product", PageContext.PAGE_SCOPE), "name")));

记得使用<jsp:getProperty />时,查找指定bean的范围是依次是page,request,session,application的,怎么在这里只在page范围内查找呢。
这个标签很久没用了,很多东西都忘了。
于是google了下,还真有人遇到同样的问题。
<jsp:useBean id="product" class="com.example.Product" scope="session"></jsp:useBean>
   <!--此语句在Tomcat5.0以上版本中必须要有,否则出现上述错误-->

于是我在index.jsp加了<jsp:useBean id="product" class="com.example.Product" scope="session"></jsp:useBean>
在浏览器已测试,成功! 果然是这个原因。
此时再看了下index_jsp.java代码:
发现了如下代码:
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((test.Product)_jspx_page_context.findAttribute("product")).getName())));


分享到:
评论

相关推荐

    win7 odbc SQORAS32: An Unsupported operation was attempted

    标题“win7 odbc SQORAS32: An Unsupported operation was attempted”涉及到的是在Windows 7操作系统中,用户遇到一个ODBC(Open Database Connectivity)相关的错误。ODBC是微软提供的一种标准API,允许应用程序...

    tomcat-7_API_帮助文档

    application were reloaded, the loadLibrary() call would be attempted a second time. To avoid this problem, place classes that load native libraries outside of the web application, and ensure that the...

    oracle10g配置ODBC报错,oracle-5699495补丁

    在配置Oracle 10g的ODBC数据源时,用户可能会遇到"an unsupported operation was attempted"的错误提示,这通常意味着系统尝试执行了一个不被当前环境支持的操作。 错误“an unsupported operation was attempted”...

    2016年上午基础知识.pdf

    The pragmatic justification for this approach is that it is easier to achieve ( 71 ) on small steps, whereas it is much harder to get everyone on board if too much is attempted. Usually there are ...

    解决配置oracle数据源报错问题

    配置oracle 10g odbc数据源的时候提示an unsupported operation was attempted 具体解决方法:下载压缩包,用压缩包内的文件替换本地电脑内的文件。 信息来源:http://oooo-h.iteye.com/blog/1435190

    Kernel panic - not syncing: Attempted to kill init 解决办法

    Kernel panic - not syncing: Attempted to kill init 解决办法 开源成就技术;技术成就梦想

    oracle 5699495 Oracle_10_2_ODBC

    oracle 5699495 Oracle_10_2_ODBC 在配置ODBC时报错an unsupported operation was attempted 在配置ODBC时报错an unsupported operation was attempted 打此补丁就好了

    Flappy Bird Clone Source Code For Delphi XE5 Firemonkey On Android And IOS

    I attempted to apply a ratio to my hard coded movement and placement numbers so that it would take the screen size into account but was unable to really get or make a usable ratio for the screen size....

    tomcat-redis-session-manager-tomcat-7

    tomcat-redis-session 共享 异常解决:Race condition encountered: attempted to load session[xxx] which has been created but not yet serialized. java.lang.IllegalStateException: Race condition ...

    64位win7系统下32位odbc配置报错解决方法

    配置oracle 10g odbc数据源的时候提示an unsupported operation was attempted 具体解决方法:下载压缩包,用压缩包内的文件替换本地电脑内的文件,详见readme.txt

    Cloud Computing A Practical Approach

    This book is targeted at a broad audience, but in a nutshell–to anyone who wants to learn more about cloud computing. Cloud computing has become a hot topic in recent years, and people at different ...

    Arduino in Action

    Even if you've never attempted a hardware project, this easy-to-follow book will guide you from your first blinking LED through connecting Arduino to your iPhone. About this Book Arduino in Action ...

    BURNINTEST--硬件检测工具

    - Corrected a Disk test bug where on rare occasions a verification error is incorrectly displayed. This is during the random seeking phase of the "Random data with random seeking" test mode and ...

    cloud computing bible

    attempted to present some element of the topic for a particular audience. In this book, I do not make the assumption that you are a particular type of reader, nor do I assume that you are approaching ...

    mybatis中查询结果为空时不同返回类型对应返回值问题

    resultType 为基本类型,如 int,这时如果查询结果为空,MyBatis 会抛出异常:`org.apache.ibatis.binding.BindingException: Mapper method 'com.fkit.dao.xxDao.getUserById attempted to return null from a ...

    tomcat-redis-session-manager-tomcat-7.jar解决race condition问题

    解决race condition问题,根据git最新源码编译

    centos内核报错

    这种情况下,系统会出现 "kernel panic-not syncing: VFS: Unable to mount root fs on unknown block" 错误信息。 二、解决方案 解决 CentOS 内核报错的方案是修改 grub.conf 文件,将默认启动的内核版本改为旧...

    UPnP_Programming_Guide

    - **4.1.20** `UPNP_E_SOCKET_CONNECT [-204]`: Indicates a connection error on a socket. - **4.1.21** `UPNP_E_OUTOF_SOCKET [-205]`: Signals that there are no more available sockets. - **4.1.22** `UPNP_E...

    Analysis of Single-Phase SPWM Inverter

    After this, it will be attempted to simulate a model circuit on any simulating software e.g. MATLAB and analyse the output waveforms for various values of the elements used in the circuit and hence ...

    2009 达内Unix学习笔记

    [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后...

Global site tag (gtag.js) - Google Analytics