servlet  传值的代码如下:                LinkedList<basicinformation> a=new LinkedList<basicinformation>();//创建链表
ListIterator<basicinformation> aIter=a.listIterator();
basicinformation c=new basicinformation(); basicinformation info=new basicinformation();
while(rsl.next())//从数据库读取信息后填写到a中
{
info.setName(rsl.getString(1));
info.setFullname(rsl.getString(2));
info.setAddress(rsl.getString(3));
info.setTelephone(rsl.getString(4));
info.setContact(rsl.getString(5));
a.add(info);
}
request.setAttribute("contactinfo", a);
                request.setAttribute("aIter",aIter);
                request.setAttribute("c", c);
RequestDispatcher dispatcher=request.getRequestDispatcher("showdata.jsp");
dispatcher.forward(request, response);
JSP的取值代码如下:
 LinkedList a=(LinkedList)request.getAttribute("contactinfo");
 ListIterator aIter=(LinkedList)request.getAttribute("aIter");basicinformation的代码如下:
public class basicinformation { private String name;
private String fullname;
private String address;
private String telephone;
private String contactname; public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}

public void setFullname(String fullname)
{
this.fullname=fullname;
}
public String getFullname()
{
return fullname;
}

public void setAddress(String address)
{
this.address=address;
}
public String getAddress()
{
return address;
}

public void setTelephone(String telephone)
{
this.telephone=telephone;
}
public String getTelephone()
{
return telephone;
}

public void setContact(String contactname)
{
this.contactname=contactname;
}
    public String getContact()
    {
     return contactname;
    }}
但是我发现a.size()=0就是取得的链表里面没有数据。这是怎么一回事啊。值到底该怎么传怎么取?我的问题出在那里?
如果我想要让servlet将链表传给JSP让JSP以表格的形式输出元素,该怎么搞?

解决方案 »

  1.   

     request.setAttribute("list",a);jsp页面
    ${list.xxxx}
    ${list.xxxx}
    ${list.xxxx}
    ${list.xxxx}
    ${list.xxxx}
    ${list.xxxx}还有你确信你跳转的页面就是那个showdata.jsp吗??我记得我都会加/的,不知道你这样行不行
      

  2.   

    额,那个${list.xxxx}是什么。。我是初学者不太懂饿showdata.jsp可以的。
      

  3.   

    先确认servlet里的a有没有数据
      

  4.   

    request.setAttribute("contactinfo", a);
    之前 打印一下 a.size() ;
    确定是否a中有数据 ;
      

  5.   

    [code]
    while(rsl.next())//从数据库读取信息后填写到a中
    {
    basicinformation info=new basicinformation();  // 这句添加到循环内,不然你的记录后天的都是重复的。。循环出来的每个对象是独立的。。需要重新实例化。。
    info.setName(rsl.getString(1));
    info.setFullname(rsl.getString(2));
    info.setAddress(rsl.getString(3));
    info.setTelephone(rsl.getString(4));
    info.setContact(rsl.getString(5));
    a.add(info);
    }
    [/code]
      

  6.   


    while(rsl.next())//从数据库读取信息后填写到a中
    {
    basicinformation info=new basicinformation(); // 这句加到循环里面来。不然取出来的数据后面都是重复的,每个新的对象必须重新实例化。。
    info.setName(rsl.getString(1));
    info.setFullname(rsl.getString(2));
    info.setAddress(rsl.getString(3));
    info.setTelephone(rsl.getString(4));
    info.setContact(rsl.getString(5));
    a.add(info);
    }