• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to pass an arrayList from a JSP to a servlet.

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help.
I made some changes and it is working now
 
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic