By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

memory questions in Java

P: n/a
I'm passing some arrays (created by checkboxes) between servlets and
JSPs. The code works, but it just doesn't look right. I'm worried
that I will run into some memory problems at some later point.

Here is what I'm doing: The servlet InitVisit initializes an array
(all checkboxes unchecked) and forwards it to a JSP

String ordered[] = {"",""}; // I actually have 11 checkboxes !!!
request.setAttribute("ORDERED",ordered};

visit.jsp receives this array:

<% String ordered[] = {"",""}; %>
<% ordered = (String[])request.getAttribute("ORDERED); %>

visit.jsp then displays a sequence of checkboxes (i = 0,...10):

<input type=checkbox name="ORDERED" value=i>
or
<input type=checkbox checked name="ORDERED" value=i>

(but at this point all checkboxes are unchecked)

The user selects any number of checkboxes (up to 11) and posts to a
servlet visitUpdate.

visitUpdate allocates memory again:

String ordered[] = new String[12];
ordered = request.getParameterValues("ORDERED");

After processing the data, visitUpdate forwards its request back to
the same old JSP visit.jsp. So, again we have

<% String ordered[] = {"",""}; %>
<% ordered = (String[])request.getAttribute("ORDERED); %>

visit.jsp displays a sequence of checkboxes (i = 0,...10):

<input type=checkbox name="ORDERED" value=i>
or
<input type=checkbox checked name="ORDERED" value=i>

(At this point the ORDERED attribute might have an array of size 11 if
all checkboxes had been checked previously)

The code works, but somehow it doesn't look right. in visit.jsp I
allocate an array of size 2 and populate with 0 to 11 elements. Then,
in visitUpdate I do just the opposite and allocate an array of size 12
to populate with 0 - 11 elements: What do the commands
request.getParameterValues and request.getAttribute do with my memory
allocations?

Anke
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Anke Barton wrote:
The code works, but somehow it doesn't look right. in visit.jsp I
allocate an array of size 2 and populate with 0 to 11 elements. Then,
in visitUpdate I do just the opposite and allocate an array of size 12
to populate with 0 - 11 elements: What do the commands
request.getParameterValues and request.getAttribute do with my memory
allocations?


In a word, nothing.

To be a little clearer, the memory allocations you are doing are wasted.
The methods will create their own memory space, or use memory from
instance variables, etc.

To be even clearer, remember that a variable holding an array is really
a reference to the instance of the class representing the array. It
works like any other object reference. Consider:

// Make a list to play with
List list = createList();
// Make an array the same size as the list
Object array[] = new Object[list.size()];
// Let's fill the list with the array
array = list.toArray();
// Oops the array I allocated is gone now because
// the toArray() method made a new one.

// The correct way (also ultra-paranoid):
array = list.toArray(array);

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.