| Author |
How to pass an arrayList from a JSP to a servlet.
|
Maria Smith
Ranch Hand
Joined: Apr 01, 2004
Posts: 40
|
|
How to pass an arrayList from a JSP to a servlet. I tried to use request.setAttribute and tried to forward it to a servlet using <jsp:forward page=/> and it didn't work, because I use <%@ include file= and <jsp:include page= in header.jsp . and request has been already commited. I also tried to use JavaScript function and assigning ArrayList to a hidden field. But Hidden Field can hold a single value. Or is there any other better way to achieve this I am getting the Linked List from a servlet to a jsp. and display the node of linked list as a JavaScript link on the screen. And each link is associated with a list(Which can contain more then one row) When user clicks the link the request is forwarded to a Servlet where I retrieve all the the data associated with the list. Thanks in advance. I really appreciate your help Maria
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
When user clicks the link the request is forwarded to a Servlet ...
No, it is not. When you click a link a new request is created. The current request cannot be forwarded in this manner since it went out of scope when the page was sent to the browser. As you noted, a single hidden field can hold only one value. But, you can create a hidden field for each entry in the list. And if you name each such field with the same name, you can easily get the values back with request.getParameterValues() in your servlet.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Maria Smith
Ranch Hand
Joined: Apr 01, 2004
Posts: 40
|
|
As I mentioned earlier my JSP page contains JavaScript URL which is nothing but Linked List node. and each node contains more then one row. So I tried to use JavaScript array to pass the values back to JavaScript function. Now the problem is regardless of iteration my function always displays the last value in the array (Example if I have 2 rows in my array as for i =0 id =4, grp = HOCKEY sname= John and for i=1 id = 7 grp = SOCKER sname = Phil) when I get back the value in my function it displays ID =7 GRP = SOCKER NAME = Phil instead of this ID =4 GRP = HOCKEY NAME = John ID =7 GRP = SOCKER NAME = Phil and it is because Linked List node (name) is out side of the for loop. I want to get 'n' number of rows back when user clicks the link But i am not sure how to achieve that. Thanks in advance. Here is the code snippet using javascript and hidden field if(iterator.hasNext()) { String name = (String)iterator.next(); LinkedList result = (LinkedList)hMap.get(name); %> <tr> <td width="30%"> <% for(int i=0; i<result.size();i++) { ParentBean parentBean = (ParentBean)result.get(i); ChildBean childBean = (ChildBean)parentBean.getCommObj(); %> <a href="JavaScript:selectProgram(id[<%= i%>] = new Array('<%= childBean.getStudentId()%>'), grp[<%= i%>] = new Array('<%= childBean.getGroupId()%>'), sname[<%= i%>]= new Array('<%= childBean.getName()%> ' );" > <% } %> <b><%=name %></b> <script> var id = new Array(); var grp = new Array(); var sname = new Array(); function selectProgram(id,grp,sname) { for (var i=0; i<id.length; i++) { alert("Length :"+ id.length); alert("ID :"+ id[i]); alert("GRP :"+ grp[i]); alert("NAME:"+ sname[i]); document.test.id.value = id[i]; document.test.groupId.value = grp[i]; document.test.name.value = sname[i]; } self.document.test.submit(); } <form method="POST" name="test" ACTION="/servlet/Test" method="post" > <input name=name type="hidden" value=""> <input name=id type="hidden" value=""> <input name=groupId type="hidden" value="">
|
 |
Maria Smith
Ranch Hand
Joined: Apr 01, 2004
Posts: 40
|
|
Thank you so much for your help. I made some changes and it is working now
|
 |
 |
|
|
subject: How to pass an arrayList from a JSP to a servlet.
|
|
|