my dog learned polymorphism*
The moose likes JSP and the fly likes Problem with retreving the Attribute in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with retreving the Attribute in jsp" Watch "Problem with retreving the Attribute in jsp" New topic
Author

Problem with retreving the Attribute in jsp

Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
I am trying to test how a attribute is displayed in the jsp when using a bean

I am using a bean Person.java ->name (as property)

the index page takes a name from the client and sends to the servlet ,the servlet takes that request parameter and stores in a object person and stores the object in the Attribute and sends to the first.jsp page

here is the code

index.jsp




web.xml




Person.java (bean)




Output


My question is why is the standard action and EL not working and displaying the name .......


SCJP 1.5, SCWCD 1.5
Tomasz Szymanski
Greenhorn

Joined: Jan 11, 2010
Posts: 23
Varun Nayudu wrote:
My question is why is the standard action and EL not working and displaying the name .......


Because obviously the bean is placed under "pObj" in the request scope. Under what attribute name do you think the standard action and the EL expression are trying to find your bean?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

At what point does a name get assigned to the property? If you don't set it, of course it will be null.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Tomasz Szymanski wrote:
Varun Nayudu wrote:
My question is why is the standard action and EL not working and displaying the name .......


Because obviously the bean is placed under "pObj" in the request scope. Under what attribute name do you think the standard action and the EL expression are trying to find your bean?


Chance upon this and can't help to ask you question :-

Sorry Tomasz Szymansik, could you please elaborate your comments, especially the part on "obviously the bean is placed under "pObj" in the request scope."

It would be most helpful if you can tell what should be the correct methods....am trying to learn as much as possible from experts in this forum. Thank you.



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

As I already pointed out, the scoped variable does not exist, so of course nothing is displayed.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
I assume you want to compare the access of scoped variables with scriptlets and EL? Some things I see here:
- <jsp:useBean...> looks for a bean named "person". Tomasz Szymanski pointed this out, if you want to use the same object as with the scriptlet, you should write id="pObj". Your EL expression would then access it with ${pObj.name}.

- You use <jsp:useBean..> with the class attribute. This creates a new Person object in the request scope, which attributes will be null of course (since your Person object does not have a default constructor that sets another value). If you want to use an existing object, you should write type="com.beans.Person" instead of class="..".

- I don't think you need <jsp:getProperty..> here (might depend on which JSP/EL version you are using).

- Yet another thing: I *think* you can leave out the <jsp:useBean..> completely. The EL expression is smart enough to look into the different scopes by itself. However I usually do write <jsp:useBean..> (with type attributes) at the beginning of a jsp to document which beans the page expects. Note that <jsp:useBean..> will throw a nullpointer exception if the objects are not found in scope, so this is also a useful way to find mistakes quickly.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

D. Ogranos wrote:You use <jsp:useBean..> with the class attribute. This creates a new Person object in the request scope,

Only if the scoped variable does not already exist. If a person scoped variable exists, the tag will do nothing (except create a useless scripting variable).

If you want to use an existing object, you should write type="com.beans.Person" instead of class="..".

Using type will never cause a new instance to be created, but class can be used, as noted above, to hook up to an existing scoped variable.

I don't think you need <jsp:getProperty..> here (might depend on which JSP/EL version you are using).

getProperty is no longer needed in the JSTL and EL environment.

I *think* you can leave out the <jsp:useBean..> completely.

Correct. useBean is not necessary for the EL to find the scoped variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with retreving the Attribute in jsp
 
Similar Threads
can't able to run <jsp:useBean> in jsp page
unable to set the value of Internal attribute using <c:set>
jsp:usebean | NoClassDefFoundError
<c:set> is not working as expected
Spring exception Neither BindingResult nor plain target object for bean name 'name' a