How to retrieve the value of a variable in a jsp page to another
I have a variable declared in one jsp page. This variable is an array. How can I retrieve t开发者_如何学Chis 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.
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");
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);
- 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
精彩评论