memory questions in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anke Barton

    memory questions in Java

    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.setAttr ibute("ORDERED" ,ordered};

    visit.jsp receives this array:

    <% String ordered[] = {"",""}; %>
    <% ordered = (String[])request.getAtt ribute("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.getPara meterValues("OR DERED");

    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.getAtt ribute("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.getPara meterValues and request.getAttr ibute do with my memory
    allocations?

    Anke
  • Raymond DeCampo

    #2
    Re: memory questions in Java

    Anke Barton wrote:[color=blue]
    > 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.getPara meterValues and request.getAttr ibute do with my memory
    > allocations?[/color]

    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(ar ray);

    HTH,
    Ray

    --
    XML is the programmer's duct tape.

    Comment

    Working...