This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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
 
Marshal
Posts: 67313
170
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[].
 
We begin by testing your absorbancy by exposing you to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!