0

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Imugi
  • 161
  • 1
  • 3
  • 13
  • 1
    I have edited your question to become more clear and easier to read. Your code was marked as code *and* blockquote (lines that begin with `>`), so I have removed the blockquotes. To get the right indentation within a list, code has to be indented 8 spaces and not just 4, but that's really a detail. I have highlighted inline code and filenames with backticks, removed the ALL CAPS (which can be perceived as rude) and the "thank you", which is not necessary. – Benjamin W. Jan 28 '16 at 07:29

3 Answers3

1

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>
Community
  • 1
  • 1
Frank
  • 2,036
  • 1
  • 20
  • 32
  • doesn't work, for some reason. I just put this snippet in my code but nothing happened. It seems I'm missing something. – Imugi Jan 28 '16 at 11:49
  • The snippet should only show how to iterate and display the values. Of course you have to replace ${myList} with the real property. In your case ${aaa.array_list} which still won't display anything as you do not fill the list in a.java. – Frank Jan 28 '16 at 12:04
1
<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());
}
cody123
  • 2,040
  • 24
  • 29
0

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>
mehere
  • 1,487
  • 5
  • 28
  • 50