日期:2014-05-20  浏览次数:20650 次

jsp与servlet之间的传值?
RT,我看了网上很多例子,servlet查询数据库,得到结果集,再传到JSP中显示,我自己做的时候用request,JSP页面总是取不到值啊,不懂怎么回事了。关键代码:
rs=stmt.executeQuery(sql);
List<UseBeanInfo> list=new ArrayList<UseBeanInfo>();
while(rs.next())
{
UseBeanInfo usi=new UseBeanInfo();
usi.setId(rs.getInt("id"));
  usi.setStatus(rs.getInt("status"));
 usi.setName(rs.getString("name"));
 usi.setAddress(rs.getString("address"));
usi.setMobile(rs.getString("mobile"));
list.add(usi);
}
RequestDispatcher rd = req.getRequestDispatcher("/welcome.jsp");  
req.setAttribute("list",list);//存值  
  rd.forward(req,resp);

JSP中

List list=(ArrayList)request.getAttribute("list")
UseBeanInfo usi=new UseBeanInfo();
usi.getString("name");

------解决方案--------------------
jsp中,不是new 对象,要循环读取list中的对象



List<UseBeanInfo> list=(ArrayList)request.getAttribute("list");
for(UseBeanInfo usi : list){
String name = usi.getString("name");
System.out.println(name);
}