• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Weird output from jsp:getProperty : pls help

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following set of class and resources. What should be the output?
I was expecting a output "Session" while I am getting "Request" . Could anybody explain why???



Request sent to this servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Person per2 = new Person("Request");
Person per3 = new Person("Session");
request.setAttribute("person", per2);
request.getSession().setAttribute("person", per3);

RequestDispatcher dispatcher = request.getRequestDispatcher("/test.jsp");
dispatcher.forward(request, response);
}



test.jsp

<jsp:useBean id="person" type="com.tridib.vo.Person" scope="session" />
<jsp:getProperty property="name" name="person"/>




Person.java(Bean class)

package com.tridib.vo;

public class Person
{
private String name = "Tridib";


public Person()
{
}

public Person(String name)
{
this.name = name;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}



[ March 29, 2007: Message edited by: Tridib Samanta ]
[ March 29, 2007: Message edited by: Tridib Samanta ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tridib,

request.setAttribute("person",per2);
request.getSession().setAttribute("person",per3);

are you trying with same name "person"..isn't overwritten?

i think useBean getProperty work like Pagecontext.findAttribute()..searches for the named attribute in page,request,session and application in order returns value associated or null;

so if the person which is available in request scope it will come first before session..

correct me if i m wrong?

Thanks,
Seenikkannan.K
[ March 29, 2007: Message edited by: Seenikkannan krishnasamy ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seenikkannan krishnasamy:
request.setAttribute("person",per2);
request.getSession().setAttribute("person",per3);

are you trying with same name "person"..isn't overwritten?



No, because one is set in the request scope and the other in the session scope: the code snipped works on two different attributes.

Originally posted by Seenikkannan krishnasamy:
i think useBean getProperty work like Pagecontext.findAttribute()..searches for the named attribute in page,request,session and application in order returns value associated or null;



I think you are partially right. jsp:useBean works as you described if no scope is specified, but in Tridib's case the jsp code is explicitly specifying scope="session".

I fail to see where the problem is. I'll try it myself once I get home.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is this line:



try specifying a different name. for example in your jsp:useBean tag, use an id of "sessionPerson" and then make the name in your jsp:getProperty tag match that.

I think what is happening is this getProperty tag is looking in the request scope first for an object named "person" and is finding the one you added in the servlet. the getProperty tag then stops instead of looking for the "person" bean you defined with jsp:useBean in the session scope. The bottom line is that jsp:getProperty does not NEED the jsp:useBean because the named reference exists already and will be available in the fully compiled servlet.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Spec (5.3)

The value of the name attribute in jsp:setProperty and jsp:getProperty will
refer to an object that is obtained from the pageContext object through its findAttribute
method.

 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Clifton Eaton:
try specifying a different name. for example in your jsp:useBean tag, use an id of "sessionPerson" and then make the name in your jsp:getProperty tag match that.



The jsp then translates to:

and results in an exception: javax.servlet.ServletException: bean sessionPerson not found within scope


Originally posted by Clifton Eaton:
The bottom line is that jsp:getProperty does not NEED the jsp:useBean because the named reference exists already and will be available in the fully compiled servlet.



It doesn't work either, because the jsp translates to:

which results in the following exception: org.apache.jasper.JasperException: Attempted a bean operation on a null object

And finally, the original jsp translates to:

which outputs "Request" due to the fact that getProperty uses pageContext.findAttribute("person") to get the bean.

I hope this helps somebody else as it did help me understand.
[ March 30, 2007: Message edited by: Sergio Tridente ]
 
Tridib Samanta
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody for your great replies!!

The value of the name attribute in jsp:setProperty and jsp:getProperty will
refer to an object that is obtained from the pageContext object through its findAttribute
method.



Now, I understand why the output is so. But, what is the purpose of "scope" attribute in <jsp:useBean> action, if it can't make the Object available from correct scope?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem here is not with useBean, but with getProperty (it's not a problem actually).
useBean correctly uses the "person" which is in the session.
getProperty finds the "person" in the request first, because the scope is not being set.

The best would be to avoid setting attributes with same names in different scopes, to avoid hair-pulling bugs.
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
getProperty finds the "person" in the request first, because the scope is not being set.



OK. But i have one doubt:
- when using jsp:useBean and jsp:getProperty on the same bean, the resulting translated servlet shows that jsp:getProperty uses the findAttribute method to locate the bean:

- but when using jsp:getProperty without jsp:useBean, then it seems that the jsp:getProperty uses the getAttribute method on page scope to locate the bean:


Why is that? Am I totally wrong here?
[ March 30, 2007: Message edited by: Sergio Tridente ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean id="person" type="com.tridib.vo.Person" scope="session" /> creates an instance of the bean if it dosent exist, or uses the bean if it exists, The scope attribute is used to store this bean i.e person bean.
when we use <jsp:getProperty property="name" name="person"/> we simply are trying to get the name property of person bean which is set previously i.e:
using this:

Person per2 = new Person("Request");
Person per3 = new Person("Session");
request.setAttribute("person", per2);...Storing in request
request.getSession().setAttribute("person", per3);...Storing in session

RequestDispatcher dispatcher = request.getRequestDispatcher("/test.jsp");
dispatcher.forward(request, response);

The getProperty will first look for the property in page , request , session and application scope and it returns value it gets first
: in this case its request scope and hence the Request is returned to the output.

Let me know if this is clear
 
Sergio Tridente
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer Muralidhar. I also thought it shoudl work that way, except that when in the JSP I put only:

i.e.: without using the jps:useBean tag before, the generated servlet code looks like:


and not like expected, what would be:


Can you check out with your Container to see if it reacts the same way as mine. I don't understand why the jsp:getProperty is translated differently in both cases. Or am I doing something wrong?
 
Seenikkannan krishnasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean id="person" type="com.tridib.vo.Person" scope="session"/> creates an instance of the bean if it doesnt exist

i dont think it will create an instance.it will throw an Exception.

Guys correct me if i m wrong

Thanks,
Seenikkannan.K
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic