• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Regarding maintaining hidden variables

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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: 67399
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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[].
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic