Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get values of object

 
rushikesh kale
Ranch Hand
Posts: 41
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the JSTL tags.

Here's a tutorial.

Good luck ...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
rushikesh kale
Ranch Hand
Posts: 41
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic