aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Weird output from jsp:getProperty : pls help 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Weird output from jsp:getProperty : pls help" Watch "Weird output from jsp:getProperty : pls help" New topic
Author

Weird output from jsp:getProperty : pls help

Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
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 ]

Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Seenikkannan krishnasamy
Greenhorn

Joined: Jan 25, 2006
Posts: 15
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 ]
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Clifton Eaton
Greenhorn

Joined: Mar 26, 2007
Posts: 16
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.


-------------------------------------<br />SCJP<br />SCWCD
Anupama Ponnapalli
Ranch Hand

Joined: Jun 12, 2006
Posts: 66
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

Joined: Mar 22, 2007
Posts: 329

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

Joined: Apr 28, 2004
Posts: 128
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?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

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 ]
Muralidhar Adhikarla
Greenhorn

Joined: Apr 01, 2007
Posts: 14
<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

Joined: Mar 22, 2007
Posts: 329

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

Joined: Jan 25, 2006
Posts: 15
<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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird output from jsp:getProperty : pls help
 
Similar Threads
JSP - cannot resolve symbol
.tld error
getProperty question
Session time out confusion
<jsp:useBean> conditional body