0

I have a variable declared in one jsp page. This variable is an array. How can I retrieve this array in the next jsp page. The code is likely to be:

<% 
    String[] a=new String[10];
    int i=0;
    while(resultSet.next())//here I'd retrieved the values from the Database
    {
        a[i]=resultSet.getString(1);
        i++;
    }
%>

now i have to retrieve this array a on the next page.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117

3 Answers3

0

Put it in the session, which is an object shared among all pages and servlets of the web application:

session.setAttribute("myarray", a);

And retrieve it with:

String[] bubi = (String[]) session.getAttribute("myarray");

Instead if with

next page

you mean the page that will be included in current page some lines after, you could also use request attributes (not parameters!), setting:

request.setAttribute("myarray", a);

and getting:

String[] bubi = (String[]) request.getAttribute("myarray");
bluish
  • 26,356
  • 27
  • 122
  • 180
0

each jsp file live for itself. You have two options: - You use an object class, that can store that in the current session - You store the array in thehead of the request with rqeuest.setAttribute("myArgumentName", myArrayObject);

Reporter
  • 3,897
  • 5
  • 33
  • 47
0
  • store it in the session
  • output it in a hidden field, comma-separated, submit it, and recreate the array on the next page

I prefer the 2nd approach.

But try to avoid java code in jsp pages. See here how and why

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140