0

This is Java class:

package com.example;

import java.util.ArrayList;

public class sample {

    public void print() {

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

    }
}

This is my Jsp page :

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

<%@ page import="com.example.sample"%>
<%@page import="java.util.*"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
    sample s = new sample();
%>


</body>
</html>

I dont know how to call print merhod and how Print list in jsp page so that i print data in jsp page list please help me how i will Implement i am learning this.

user2837886
  • 21
  • 1
  • 1
  • 2

2 Answers2

3

First return a list from print method

public List<String> print() {

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

     return l;

    }

Don't use scriplets, Use JSTL

<%
    sample s = new sample(); //not recommended.Pass this object from servlet
    List<String> list = s.print();
%>

with that print where ever you want to print in jsp

With JSTL

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

With scriplets (not recommended);

<% for (int i=0;i<list.size();i++)
          {

              out.println(list.get(i));

          } %>

With help of iterator;

 <%  Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {    
        out.println(<iterator.next());
    }
 %>

Side note:Not tested ,and make sure that you have imported all used classes in JSP.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0
<%
Class c = new Class(); 
List<String> list = c.print();
for (c : list) {
  //print list here
}
%>
Chirag Kathiriya
  • 427
  • 5
  • 14