1

I am wondering how to pass an array or a list from one .jsp page to another. I want to then take the values from this array and assign them to a javascript array. I think I have the source jsp page configured correctly, but was wondering how to get the values in the second .jsp page.

This is my source .jsp file:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Firstjsp</title>
</head>
<body>
<Form Method = "Post" Action = "Mapper.jsp">
<% String locations[] = {"Loan 1", "33.890542", "151.274856", "Address 1","true", "-35404.34"};
for (int i =0; i<locations.length; i++)
{
%>
<Input type = "Hidden" name = "loc" value = "<%= locations[i] %>">
<%
}
%>
</Form>
</body>
</html>
Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
stacktraceyo
  • 1,235
  • 4
  • 16
  • 22

2 Answers2

2

You can get them using HttpServletRequest#getParameterValues(). This returns a string array of all parameter values which have the same parameter name. In your case, you have generated several hidden HTML input elements with the same name loc in the first JSP, so the following in the second JSP (or, preferably, a servlet) should do:

String[] locations = request.getParameterValues("loc");
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

The easiest way i think is to put the variable (it works even as a pointer) as a session variable.This way you can access it everywhere as long as the code is running under the same session.

<% String name = request.getParameter( "username" ); session.setAttribute( "theName", name ); %>

This example also uses request.The difference is that session stands out.For example if you set a variable in session then even if you close the browser and restart it , it will still be there.Think of it as a global variable.Request is best used when you send data from one jsp/servlet to another jsp/servlet.It basically has a lifespan of 1.The moment when you redirect a page it disappears.

You can use session.setParameter(name_of_the_variable,the_variable) and session.getParameter(name_of_the_variable). As a hint that was useful to me,always make sure that you test the returned parameter, in the case above "name" if it's not NULL.If there is no variable on the session with that name, it will return NULL, and most likely crash.Hope this helps!

Dorin Rusu
  • 1,095
  • 2
  • 13
  • 26