Retrieving the value of a java array in jsp

时间:2015-07-24 12:48:32

标签: java arrays jsp jstl

I have a java method that returns a String array. Also there is a jsp page which has an array variable. How do I assign this jsp variable with the value returned by the java method?

I tried,

<c:set var="list" value="<%= engine.getList(); %> "/>

where engine is the java object. But this is not working. No values are getting stored.

Java Code:

public String[] getCatEntId() {
    ArrayList<String> catEntIdArrList = new ArrayList<String>();
    Statement stmnt = null;
    String itemUrl = null;
    int i = 0;

    String sql = "SELECT * FROM ITEM WHERE ITEMID LIKE '%it%'";

    try {
        stmnt = getMySqlDbConn().createStatement();
        ResultSet rs = stmnt.executeQuery(sql);

        while (rs.next()) {
            itemUrl = rs.getString(1);
            String catalogId = itemUrl.substring((itemUrl.indexOf("productId=") + 10), (itemUrl.indexOf("productId=") + 15));
            System.out.println("Catalog Id : " + catalogId);
            catEntIdArrList.add(catalogId);
            i++;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } 

    return catEntIdArrList.toArray(new String[catEntIdArrList.size()]);
}

Any help would be appreciated!!!

1 个答案:

答案 0 :(得分:0)

使用JSTL Tag ::

java代码:

public List print(){

    ArrayList<String> l = new ArrayList<String>();
    l.add("a");
    l.add("b");

 return l;

}

JSP Code ::

  <c:forEach items="${list}" var="item">
     ${item}<br>
  </c:forEach>