1

I am developing one sample web application using JSP and Servlets, in this application i have set some object in the Servlets, i can retrieve that value in JSP by using request.getAttribute("Object"). Here i want to iterate that array of value in JSP. How can i achieve this any one help me.

My servlet code:

ArrayList<Performer> Performerobj=new ArrayList<Performer>();
ResultSet rst = stm1.executeQuery("some query"); 
while (rst.next()) 
{   
     Performer obj=new Performer();
     obj.setProject(projectname);
     obj.setCount(rst.getString("COUNT"));
     obj.setDate(rst.getString("DATE"));
     obj.setEmpid(rst.getString("empid"));
     Performerobj.add(obj);
}
request.setAttribute("Performer", Performerobj);

Performer.java

public class Performer {


private String project;
private String empid;
private String date;
private String count;

public String getProject() {
    return project;
}
public void setProject(String project) {
    this.project = project;
}
/*setter and getter...... for all*/

Perform.jsp

    <% List<Performer>obj1=List<Performer>)request.getAttribute("Performerobj"); %>
<script>
       var obj=<%=obj1%>   
for(obj object : list)
{
  /*IS it correct way or how can i iterate*/
}
</script>
2

1 Answer 1

2

You can do that if you transform the ArrayList object into a JSON using a library like Jackson:

<% List<Performer>obj1 = (List<Performer>) request.getAttribute("Performerobj"); %>
<script>
var obj=<%=new ObjectMapper().writeValueAsString(obj1)%>;
for(obj object : list)
{
  /*IS it correct way or how can i iterate*/
}
</script>

Another option is to use JSTL:

<c:forEach var="performer" items="${Performerobj}">
    <c:out value="${performer.project}"/>
</c:forEach>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.