• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding maintaining hidden variables

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know if we want to maintain some fields we use hidden attribute. What if we want to store a list object in hidden. Say i declare

// In the servlet code
ArrayList myList = new ArrayList();
myList.add("one");
myList.add("two");
myList.add("three");

request.setAttributye("myList", myList);


// In the jsp page i write
ArrayList myList = (ArrayList) request.getAttribute("myList");

Now how should i maintain myList in hidden fields. Can i do it or is there any other alternative for performin this task.

Thanks and regards
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hidden fields would be a poor choice as they can only maintain strings. You'd need to flatten the List and then re-create it.

What have you got against maintaining such constructs in the session?
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In our application we are not supposed to use a session. So we have to use only request scope in our application. Can we store the list as string array. If yes can you show me a snippet of code . I have written a piece of snippet below.

<jsp:forEach var = "index" item = "${requestScope.myList}">
<input type = "hidden" name ="myList" value = "${index}"/>
</jsp:forEach>

Is the above snippet right. Add comments please.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not use request.setAttribute(String, Object) method to store your list object in request context?
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Krithika Srinath:
why not use request.setAttribute(String, Object) method to store your list object in request context?



It will work only when you forward/include the same request to some other servlet/jsp.
But when you render a response and still wants to maintain this values in request you will have to use hidden only.
[ October 03, 2007: Message edited by: sudhir nim ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PradeepBhatt Prasad:
In our application we are not supposed to use a session.



Out of curiosity, why?



Originally posted by PradeepBhatt Prasad:

So we have to use only request scope in our application. Can we store the list as string array. If yes can you show me a snippet of code . I have written a piece of snippet below.

<jsp:forEach var = "index" item = "${requestScope.myList}">
<input type = "hidden" name ="myList" value = "${index}"/>
</jsp:forEach>

Is the above snippet right. Add comments please.



What you're doing looks fine.
If you use the same name for multiple form input fields (hidden or otherwise) you can retrieve them with request.getParameterValues.
This method will return an instance of String[].
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic