Imugi
Imugi

Reputation: 161

How to get values from array in jsp

I want to show the data from an array using JSP.

I have three files:

  1. index.jsp:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World! </h1>
            <form name="Input Name Form" action="response.jsp"/>
            <p> Enter your name:</p>
               <input type="text" name="name"/>  
               <input type="submit"   value="ok" />
        </form>
    
        </body>
    </html>
    
  2. response.jsp:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1> <br>
            <jsp:useBean id="aaa" scope="page" class="A.a" />
            <jsp:setProperty name="aaa" property="name" value="<%= request.getParameter("name")%>" />
            <jsp:getProperty name="aaa" property="name" />
    
        </body>
    </html>
    
  3. a.java:

    public class a {
        public a ()
        {}
        private String name;
    ArrayList() array_list = new ArrayList();
    
    
        public String getName() {
            return name;
        }
    
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        //some magic to fill array_list with values
    
        }
    }
    

My question is:

What statement should I use in jsp to get values from array_list in a.java?

I know that there is statement

<c:forEach> </c:forEach>

but I am not sure how to use it.

Upvotes: 0

Views: 7678

Answers (3)

Frank
Frank

Reputation: 2066

A similar question has been asked here: Iterate ArrayList in JSP

Long story short:

<c:forEach items="${aaa.array_list}" var="item">
    ${item}
</c:forEach>

Upvotes: 1

mehere
mehere

Reputation: 1556

use JSTL.

Try this out:

Have this at top of your JSP:

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

And code for displaying data

<c:forEach begin="0" end="${fn:length(array_list) - 1}" var="index">
   <tr>
      <td><c:out value="${array_list[index]}"/></td>
   </tr>
</c:forEach>

Upvotes: 0

cody123
cody123

Reputation: 2080

<c:forEach items="${dataDetail}" var="data" varStatus="item">
    <c:out value="${data.id}"/>
</c:forEach>    

Here "dataDetail" is name of the key where you have set your list in controller.

(session or request ).setAttribute("dataDetail",---List of Data of type Class Data---);

Above code is similar to

for(Data data : dataDetail){
   System.out.println(data.getId());
}

Upvotes: 1

Related Questions