1

Can someone help me in making a array session in JSP? I want to add an item to the array in some part and later on access the whole value of item in my controller. I know how to do it on singular variables but i want to do it in array. Hoping someone can help me. Thank you so much.

I want to do something like

<%
    session.setAttribute( "theName[]", "sample name" );
%>

But I don't know how to do it.

trumanblack1025
  • 471
  • 2
  • 8
  • 19

1 Answers1

0

Similar to request.setAttribute, you can add Object using setAttribute and get it using getAttribute, e.g. JSP code:

List<String> sample = new ArrayList<>();
sample.add("1");
session.setAttribute( "sampleName",  sample);

And get it later in Controller

ArrayList<String> samples =  (ArrayList<String> sample)session.getAttribute("sampleName");
samples.get(0); // Use get/set to get/update array

Also if you meant to use public java.lang.String[] getValueNames() use getAttributeNames() instead

Deprecated. As of Version 2.2, this method is replaced by getAttributeNames()

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233