日期:2014-05-17  浏览次数:20631 次

为什么会输出null?? servlet利用javabean向jsp传递数据
javabean端的代码:

package com.servlet;
public class rsBean {
private String name;
public String getName() {
System.out.println(name);
return name;
}
public void setName(String name) {
this.name = name;
}

}

servlet端的代码:
 rs=selection.selectAll(conn,sql); 
  try {
if(rs.next())
{
String s="商品ID: "+rs.getString(2);
System.out.println(s);
rsBean rb=new rsBean();
session.setAttribute("rb", rb);
rb.setName(s);

}


jsp端代码
<textarea name="content" rows="20" cols="40">
 
  <jsp:useBean id="rb" class="com.servlet.rsBean" type="com.servlet.rsBean" />
  <%
  String name=((com.servlet.rsBean) pageContext.getAttribute("rb")).getName();
  out.println(name);
  %>
   
   
  </textarea><br/> 


结果:在textarea中显示的是null??求解为什么??

------解决方案--------------------
Java code

if(rs.next())
{
String s="商品ID: "+rs.getString(2);
System.out.println(s);
rsBean rb=new rsBean();
session.setAttribute("rb", rb);
rb.setName(s);

}

------解决方案--------------------
楼上正解,顺序写的不对。不过,楼主的代码好乱啊