• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Session Attributes without Scriptlets  RSS feed

 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In my JSP I want to get an object out of a JavaBean and do some processing with it. But I need to cast this object down to MyObject in order to work with it. E.g.:

<%
MyForm form = (MyForm)session.getAttribute("myForm")).
MyObject myObj = (MyObject)form.getMyObject();
// do some processing on myObj...
%>

How do I do this without using scriptlets? I realise this is a basic question so could somebody please either point me to the correct JSTL libraries to use or provide me with an example?

Thanks very much.

P

[ November 03, 2005: Message edited by: Peter Loew ]
[ November 03, 2005: Message edited by: Bear Bibeault ]
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your container is JSP2.0 use this EL expression:

${sessionScope.myForm.myObject}

to represents your object (no need to cast).
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alec,

But my container boasts the JSP 1.2 Spec. Is there a way to do this with JSP 1.2?

P
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about expression:

<%=((MyObject) ( (MyForm)session.getAttribute("myForm")).getMyobject())%>

This is technically a scripting element. But I doubt if it is possible to achieve scriptless page without using EL in JSP 2.0.
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is exactly what I'm trying to get away from. A number of other pages in this enterprise application do something similar. I thought I'd try and enforce a non-scriptlet policy but I see that might not be possible!

I think I'll end up using that approach. Thanks for your help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the EL under JSP 1.2, but only within the context of JSTL 1.0 tags.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While a worthy goal, it's very difficult to achieve completely scriptless JSP pages under JSP 1.2 since the EL is limited to JSTL tags. Under JSP 2.0, where the container rather than the JSTL is reponsible for EL evaluation, it finally becomes possible.

Probably the biggest brick wall that you hit under JSP 1.2 is that you cannot use the EL as attributes of your own custom tags, and thus must resort to scriptlet expressions to pass dynamic values.

But I think you are on the right track. I'd recommend trying to minimize your reliance on scripting even under JSP 1.2 (though not to the point of creating complex contortions that are actually worse than scripting) in preparation for a future move to JSP 2.0.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alec Lee:
What about expression:

<%=((MyObject) ( (MyForm)session.getAttribute("myForm")).getMyobject())%>

This is technically a scripting element. But I doubt if it is possible to achieve scriptless page without using EL in JSP 2.0.



Should the JSP developer need to know that this value is the result of a call to the toString method of some object that is currently being stored in session?

Why not just provide a scoped bean that exposes simple simple getters for these properties.
If you wrap this kind of logic up in beans that handle the casting and dereferencing, you will be following a more pure form of MVC.
Let the JSP be nothing but a view tier component.
 
Peter Loew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
But I think you are on the right track. I'd recommend trying to minimize your reliance on scripting even under JSP 1.2 (though not to the point of creating complex contortions that are actually worse than scripting) in preparation for a future move to JSP 2.0.


I agree with you. Some applications we have here at work are completely over-engineered; The design is much more complex than the actual purpose, i.e. what it's meant to do. I am generally against increasing the complexity of an application to achieve something trivial.

Though Ben's idea about putting this logic in a bean is a good one, since it's fairly simple to achieve.

P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!