• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding maintaining hidden variables  RSS feed

 
Pradeep Kumar
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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66152
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
Krithika Srinath
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?
 
sudhir nim
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 ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 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[].
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!