Results 1 to 3 of 3
  1. #1
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    149
    Rep Power
    0

    Default array passing dynamically

    Hi

    Can anyone help me how to set and get an array attribute values??

    Eg:

    for(int i=0;i<array.length;i++)
    {
    session.setattribute(array[i],array[i]);
    }

    now i would like to get this array value in forwarded jsp???

    thanks

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    OK create a class with name Constants,create in it the array which will be public and will have all the names for session attributes.
    Java Code:
    package ourbeans;
    public class Constants { 
    
       public static final String[] array = {"username","password"}; 
       
    
       ...
    after that in jsp file you create the session variables:

    Java Code:
    <jsp:useBean   id="constants"  class="ourbeans.Constants"    />
    
    for(int i=0;i<constants.array.length;i++)
    {
          session.setattribute(array[i],array[i]);
    }
    In this way you can get attributes from the session:
    Java Code:
    session.getAttribute(cosntants.array[0]);

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    session.getAttribute(cosntants.array[0]);
    A better coding style is to use constants to get at fixed position elements in an array. When you add a new element between two existing ones, there will be a simple coding change in one place, vs looking through your code to change all the hard coded indexes.
    Java Code:
       ...
    final int NameIdx = 0;  // index to name
     ...
    session.getAttribute(cosntants.array[NameIdx]);

Similar Threads

  1. Replies: 5
    Last Post: 10-04-2008, 11:13 AM
  2. passing data dynamically
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 09:43 AM
  3. passing dynamically generated values
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-20-2008, 03:21 AM
  4. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 09:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •