我必须在jsp中显示列表。

时间:2018-05-25 10:10:40

标签: java jsp servlets

我必须在jsp页面中显示我已存储在列表中的数据。我的返回类型是List。因此,每当我调用Registervalues方法时,我的数据都应该打印在jsp页面中。

public static List<registerValues> Registervalues() {
         Connection conn = null;
         PreparedStatement pst = null;
         ResultSet rs = null;
         values.registerValues registerobject=null;
         List<registerValues> list = new ArrayList<registerValues>();
         try {
            conn = getDbConnection();
             pst = conn.prepareStatement("Select name,city,gender,username,password,email,contact_no from registration");

             rs = pst.executeQuery();


             while(rs!=null&&rs.next()) {

                registerobject= new registerValues();
                registerobject.setName(rs.getString("name"));
                registerobject.setCity(rs.getString("city"));
                registerobject.setName(rs.getString("gender"));
                registerobject.setUsername(rs.getString("username"));
                registerobject.setPassword(rs.getString("password"));
                registerobject.setEmail(rs.getString("email"));
                registerobject.setContact_no(rs.getString("contact_no"));

                list.add(registerobject);  //my data is stored over here.


                System.out.println("REGISTRATION values present");

             }

         }catch(Exception e) {
             e.printStackTrace();
             System.out.println(e);

         }
        return list; // i am returning list, as it contains all the data.
 }

1 个答案:

答案 0 :(得分:0)

你可以在jsp的foreach循环中设置该列表并打印列表项,如下所示

   <c:forEach items="${list}" var="listItem">
                      <c:set var="first" value="${list[0]}"/>
                      <c:set var="second" value="${list[1]}"/>
                      <c:set var="third" value="${list[2]}"/>
                      <c:set var="four" value="${list[3]}"/>
                      <c:set var="five" value="${list[4]}"/>
                      <c:set var="six" value="${list[5]}"/>
 </c:forEach>