File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to get values of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get values of object " Watch "How to get values of object " New topic
Author

How to get values of object

rushikesh kale
Ranch Hand

Joined: Jan 25, 2005
Posts: 41

Hi Friends,

I am not able to get values of a bean object on my jsp page which are set in servlet.(Trying to use MVC approch first time )

In my servlet code I am setting attributes of my bean object as follows:
my servlet code:


Well, In my jsp page I can get that value using following tag:


This is working fine, But I want to use this no_of_acct attribute in a if loop, How can i do that?

I tried this code :


but getting error while doing this.

Can anybody tell me the proper way of getting this bean object value for above code
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Take a look at the JSTL tags.

Here's a tutorial.

Good luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Vassili's advice is good.
We have a sample JSP in our JSP FAQ that builds a table with a nested set of JSTL's c:forEach tags.

JSTL/EL and the older scriptlet syntax should never be mixed on the same page.
[ March 28, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
rushikesh kale
Ranch Hand

Joined: Jan 25, 2005
Posts: 41

Thanks ben and vassili for reply!

I know that, how to use this JSTL tag, In my project use of JSTL custom tag is not allowed.

I developed my code, by following concept of simpleMVC and simpleTable code from bens Simple Servlet Example ,
But in my project use of custom tag is not allowed.

So is there any other logic to get the object values in jsp page, which are set in servlet page.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First. JSTL and custom tags aren't necessarily the same thing.

Second, it's most unfortunate that you can't use JSTL.
It really has made a huge improvement to the uniformity of JSP code.

Third: Not following Java bean naming conventions it going to give all kinds of problems down the road (avoid underscore separation in property method names). This will be especially problematic if you plan on using EL which follows the bean naming conventions very closely.

Lastly, yes, you can do anything with scriptlets that you can do with JSTL.

The following code has several issues:


The <%= symbol is the start of is the start of a JSP expression (just prints the value to the page) and can't be used inside an 'if' condition.

You'll also need to cast your object to its proper subclass after retrieving it from request scope.

The following should get you closer (assuming your object is named AccountInfo):

[ March 28, 2008: Message edited by: Ben Souther ]
rushikesh kale
Ranch Hand

Joined: Jan 25, 2005
Posts: 41

It works!
I was trying this from past few hrs ,
I admit that,since i am very new to jsp and particularly MVC approch, i skipped some convention.

But your tutorials gave me proper way to implement mvc approch and also saved lot of time.

Thanks once again for all!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working.

I'll stress this again because it could save you a lot of trouble later.
Make a point of following bean naming conventions.
This is always a good practice when coding Java but it's especially important when preparing objects to be passed to JSPs.

If you have a field named: "firstName", in an object named Person it's accessor (getter) method should be named "getFirstName" not "get_first_name".

EL follows this naming convention and will automatically shift the case when looking for property methods.

In EL the expression ${person.firstName} will generate code that adds the 'get' and capitalizes the first letter "person.getFirstName".
If you use another convention (like underscore separators) your JSP's EL code will not be able to find the right method.
 
jQuery in Action, 2nd edition
 
subject: How to get values of object