wood burning stoves 2.0*
The moose likes JSP and the fly likes Setting a List in the request or session scope using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Setting a List in the request or session scope using JSTL" Watch "Setting a List in the request or session scope using JSTL" New topic
Author

Setting a List in the request or session scope using JSTL

Hemant Kawale
Greenhorn

Joined: Feb 07, 2006
Posts: 6
Can you set an arraylist or an array to the session or request, via JSTL in a JSP page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Sure. Assuming that xyz is a scoped variable that is an array or List (or really any other type of object):



or



Or are you really asking if you can create and populate such elements from within the JSP page? In which case, I'd ask why you'd do this in a JSP rather than in the servlet controller.
[ February 07, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Hemant Kawale
Greenhorn

Joined: Feb 07, 2006
Posts: 6
Originally posted by Hemant Kawale:


or



Or are you really asking if you can create and populate such elements from within the JSP page? In which case, I'd ask why you'd do this in a JSP rather than in the servlet controller.

[ February 07, 2006: Message edited by: Bear Bibeault ]<hr></blockquote>[/QB]





I have to convert some pages jsp pages that have declared and populated a arraylist in scriptlets.
I have to remove these scriptlets and use Jstl instead.

So is there any way of replacing this :
<%
ArrayList A = new ArrayList();

A.add("Error Message:");
A.add("Session Not Obtained");
A.add("Solution");
request.setAttribute("e",A);
%>
with JSTl tags
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

The best approach would be to factor that code out into the controller and establish them in the appropriate contexts before forwarding to the JSP.
Hemant Kawale
Greenhorn

Joined: Feb 07, 2006
Posts: 6
I cannot change anything else as this would affect the action class, that is taking this out of the request as an arraylist.



So is there any direct replacement for the mentioned code....???

Like <c:set var=A[0] value="something />
<c:set var=A[1] value="something />

and the setting the <c:set var=${A} scope="request />
???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Why can't you move it into the action? It's really the right thing to do.

I've never tried to create and populate a List on a JSP page. It's quite possible to do for JavaBeans and even Maps with a combination of <jsp:useBean> and <c:set>.

But Lists... not so sure...

If it's something you have to do on the page (I would not), perhaps you can create a List or array in a JavaBean wrapper so that you can manipulate it via the EL and JSTL, or you could just resort to some custom actions to do it.

But really, factoring it into the controller/action is the best practice...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I cannot change anything else as this would affect the action class, that is taking this out of the request as an arraylist.


As Bear said, you should do this kind of stuff outside JSP. There must be a way to set this in the controller forwarding to this page.

I don't think you can do it using JSTL.


[My Blog]
All roads lead to JavaRanch
Hemant Kawale
Greenhorn

Joined: Feb 07, 2006
Posts: 6
So i guess there is no way of setting a list into the session or request scope via JSTL.

Ok. thanks a lot for the help guys.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Yes, you can set anything you want into a scoped variable. The issue is the code necessary to create and populate such constructs.

At this point I'd be asking what you are trying to accomplish by refactoring the pages away from scriplets. The whole purpose of using the JSTL and EL on scriptless pages is to refactor the pages to pure view elements.

If you are unwilling to move inappropriate processing off the page and are just going through the motions of converting scriplet processing to JSTL equivalents (not always possible, as you have seen), what's the point of the exercise? What are you trying to gain by the conversion?
Hemant Kawale
Greenhorn

Joined: Feb 07, 2006
Posts: 6
Its just that somebody wrote a bad piece of code and now i have to deal with converting it into JSTL.

Cant affored to refactor it..cus thaen it would affect the entire application.

I know its bad programming . . .
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Do not replace a smelly piece of code by another one
Doing this with JTSL won't make it less smelly.

I hate this kind of job
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Sounds like you've been given a job to do without the tools or responsibility to do it right. That's a tough place to be.

Well, as I said before, you could punt back to beans or custom actions if need be.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Agreed with the general consensus: you can't populate an array/list using JSTL.
The best place to do it would be an action.

If the code HAS to be on the page you can
- leave it as scriptlet code
- write a custom tag to handle it

I'd see the custom tag working something like this:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting a List in the request or session scope using JSTL