user904976
user904976

Reputation:

Receiving ArrayList from servlet to jsp page

I want to receive ArrayList from Servlet to a JSP page.

Here is what i am doing in Servlet

Stored the arraylist in schoolarraylist

request.setAttribute("Arraylist", schoolarraylist);

In JSP(CreateStudent) i am trying to access arraylist

<%ArrayList<SchoolBean> get= ( ArrayList<SchoolBean> )         

<%request.getAttribute("schoolarraylist"); %>

What i want to do is load the list of schools from database into a drop down box to be displayed while creating student in this JSP.

This is the code i wrote between the tag

<%for (SchoolBean c : get) {%>

<option value="<%=c.getSchoolname()%>"> <%=c.getSchoolname()%>
</option>
<%} %>

Upvotes: 2

Views: 9651

Answers (1)

Martin Wilson
Martin Wilson

Reputation: 3386

setAttribute(java.lang.String name, java.lang.Object o) stores an object called name in the request. So you are storing an object called "Arraylist".

getAttribute(java.lang.String name) retrieve an object called name from the request. So you are trying to retrieve an object called "schoolarraylist".

So, change your code that sets an attribute with the same name as you are using to retrieve it, e.g.:

request.setAttribute("schoolarraylist", schoolarraylist);

It doesn't matter what you call the attribute but be consistent in the name you use when setting and getting it.

BTW, you should consider using a tag library, such as JSLT. For example, if you have stored your list in the request as an attribute called "schoolarraylist", you could do something like this:

<c:forEach var="school" items="${schoolarraylist}">
    <option value="<c:out value='${school.schoolname}'/>"> <c:out value='${school.schoolname}'/>
    </option>
</c:forEach>

Upvotes: 4

Related Questions