开发者

How to show a collection in a jsp page

开发者 https://www.devze.com 2023-02-15 12:21 出处:网络
My servlet class and jsp is given below. 开发者_JAVA百科ContactManager.java public class ContactManager extends HttpServlet {

My servlet class and jsp is given below.

开发者_JAVA百科ContactManager.java

  public class ContactManager extends HttpServlet {
List<ContactDetails> contactsList = new ArrayList<ContactDetails>();


public List<ContactDetails> getContactsList() {
    return contactsList;
}

public void setContactsList(List<ContactDetails> contactsList) {
    this.contactsList = contactsList;
}

/**
 * 
 */
private static final long serialVersionUID = 7999998491377176969L;

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    AssetDao assetDao = new AssetDao();
    contactsList = assetDao.loadAllContacts();

    response.setContentType("text/html");


    String nextJSP = "/searchResults.jsp";
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(nextJSP);
    dispatcher.forward(request, response);

}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

}

ContactDetails.java

   public class ContactDetails {

private String contactName;

public String getContactName() {
    return contactName;
}

public void setContactName(String contactName) {
    this.contactName = contactName;
}

}

searchResults.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>

<jsp:useBean class="com.ey.asset.servlet.ContactManager"
scope="page"  id="contactManager">
   </jsp:useBean>

      <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>

  <html>
    <head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contacts manager</title>

  </head>
  <body>
   Contacts List

   <br><br>

 <table width="40%" cellpadding="5" bordercolor="#000066" 
  bgcolor="#FFFFFF" border="1"   cellspacing="0">
    <tr> 


     <c:forEach var="conatctInfo" items="${contactManager.contactsList}">
         <td><div align="center"><b> <c:out  value="${conatctInfo.contactName}"/>
             </b></div></td>
             </c:forEach>

 </tr>
 </table>

</body>
 </html>

the list size is 2.. but its not showing in the serachResult.jsp

Please Help


You're creating a brand new instance of the servlet as a jsp:useBean. This makes no sense. It is not the same instance as the servlet instance which is been running.

In servlet, get rid of

List<ContactDetails> contactsList = new ArrayList<ContactDetails>();


public List<ContactDetails> getContactsList() {
    return contactsList;
}

public void setContactsList(List<ContactDetails> contactsList) {
    this.contactsList = contactsList;
}

and replace

contactsList = assetDao.loadAllContacts();
response.setContentType("text/html");

by

List<ContactDetails> contactsList = assetDao.loadAllContacts();
request.setAttribute("contactsList", contactsList); // This sets ${contactsList}

In JSP, get rid of

<jsp:useBean class="com.ey.asset.servlet.ContactManager"
scope="page"  id="contactManager">
   </jsp:useBean>

and replace

<c:forEach var="conatctInfo" items="${contactManager.contactsList}">

by

<c:forEach var="conatctInfo" items="${contactsList}">

(and fix the typo)

0

精彩评论

暂无评论...
验证码 换一张
取 消