• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

JSTL No Beans Please! HOW TO read through a 'Custom Class' object using JSTL

No Beans please - 1) Can you send a 'Class' object to session scope from a servlet -  
                           2) then retrieve the session object from a JSP using JSTL (items etc):

Ok here it is: Take this class: Elements (from a servlet)

package com.Data;
public void class  Elements
{
String sFirstElement;
String sSecondElement;
Int iThirdElement;
}
Create an instanceof the class:  Elements theElements = new Elements();
fill it:
theElements.sFirstElement="thefirstvalue";
theElements.sSecondElement="thesecondvalue";
theElements.iThirdElement=3;
session it:
session.SetAttribute("SessionElements", the elements);

```````Now from a JSP Page
1) using 'JSTL'  read the values of each field in the custom class
I have not been able to do this with JSTL yet
....I get error messages such as 'can't find value of sFirstElement'
....I think this might have something to do with my custom class -
(note I can CAST the custom class the old way - but would prefer JSTL instead of CASTING:
<%Elements oldWayElements = (Elements) session.getAttribute('SessionElements)%>

thanks -

 




0
paygo
Asked:
paygo
  • 6
  • 5
  • 3
2 Solutions
 
objectsCommented:
You can use the following to access directly

${sessionScope.SessionElements.sFirstElement}

or to set a variable use:

<c:set var="elements" value="${sessionScope.SessionElements}"/>
0
 
paygoAuthor Commented:
thanks - objects here;s the error message

Unable to find a value for &quot;sFirstElement&quot; in object of class
0
 
paygoAuthor Commented:
looks like it doesn't recognize the custon class object for some reason.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
paygoAuthor Commented:
does making the field public cause a problem:  public String sFirstElement  
0
 
koppchaCommented:
you can try that... but in the link i have sent you in your earler question
i think they are using <c:out depending on getter methods
0
 
objectsCommented:
what version jsp/jstl are you running?
0
 
objectsCommented:
> i think they are using <c:out depending on getter methods

godd point :)
0
 
paygoAuthor Commented:
does the custom class need to be serializable or extend httpservlet  ?
0
 
objectsCommented:
no, what koppcha is saying is that you need to add getter methods for the vaues you want to access
0
 
koppchaCommented:
I checked that link they are working because of getter method in the bean but they are not accessing the elements in the class directly and i do not see nay harm in trying with getter methods rather than accessing the variables directly
0
 
paygoAuthor Commented:
Hope you guys got the points - think I messed up the split .

Here's the answer: I'm am more familiar with MS .NET which is more object oriented than JAVA. - true everyone!

If you are going to use JSTL then you need the 'getter' method in the Custom Object(koppcha) example:
(I guess JSTL is proprietary - and looks for the 'getter' and 'setter' methods as part of the object)

package com.Data;
public void class  Elements
{
String sFirstElement;
String sSecondElement;
Int iThirdElement;

//<Add This>
public String getCategory()
{
 return category;
}
}

~~~~~~~~~Now Part B~~~~~~~~
If you want simple object code from your JSP then you can use this to retrieve the same session object.

<%Elements oldWayElements = (Elements) session.getAttribute('SessionElements)%>

.........Comment.......... too bad JSTL can't get around this.  I'll bet this changes at some in the future.

Thanks for the help.....off and running again!
0
 
koppchaCommented:
Thanks for the points and many thanks for updating us.
0
 
objectsCommented:
> too bad JSTL can't get around this.

It can, I posted earlier how to create variable
0
 
paygoAuthor Commented:
Hi Objects, I tried this too -
Could not get this to work  without the 'getter' method in the "custom class"
class is not a regular array object.
- give it a try and post the code if it works! thx

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now