Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:get and setProperty doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Can we use jsp:getProperty and jsp:setProperty without using a jsp:useBean?
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope.. The reason is very simple, we always use <jsp:setProperty> standard action tag to set the property values of certain JavaBean. So without specifying JavaBean's name, it wouldn't be possible to set the desired values of JavaBean's properties and that's why name and property attributes are mandatory in <jsp:setProperty> tag. The same is applicable for <jsp:getProperty> standard action tag. Before using these two tags, we need to declare and instantiate the JavaBean class by using <jsp:useBean .../> tag in our jsp's. e.g. <jsp:useBean id="myBean" type="classtype" class="xxx.ClassName" scope="session" />. id attribute of <jsp:useBean.../> is the object-name which we use in name attribute of <jsp:setProperty> & <jsp:getProperty> tags to specify the JavaBean name.


Hope this helps!!!

Thanks,
Mahesh

--------------------
SCJP 1.4 | SCWCD 1.4 | SCBCD 1.3 | SCEA Part I - In Progress
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from David Bridgewater's book that we can!
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How...? Could you please explain it...?
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How...? Could you please explain it...?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what David Bridgewater's book says,

Actually, the truth is that you can use <jsp:setProperty> and <jsp:getProperty> without a previous <jsp:useBean>. All <jsp:setProperty> and <jsp:getProperty> do is to use PageContext.findAttribute()�so if an attribute of the right name exists�set up, perhaps, in a previous servlet�these standard actions will fi nd it. However, it�s good practice to
include <jsp:useBean> before these actions in the same JSP page. After all,
it won�t replace beans of the same name that you have set up by other means,
and it will create beans of the right name that don�t exist already. Furthermore, if your <jsp:setProperty> and <jsp:getProperty> standard actions try to access an attribute that doesn�t exist, they
are liable to die a horrible death with HTTP 500 errors returned to the requester.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what section JSP.5.3 of the specification says:


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.
The object named by the name must have been �introduced� to the JSP processor using either the jsp:useBean action or a custom action with an
associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.

Note � A consequence of the previous paragraph is that objects that are stored in, say, the session by a front component are not automatically visible to jsp:setProperty and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible.


I have tested it in tomcat and a transaltion error is thrown.
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for more details on this topic guys!!!

Mahesh
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergio,

Thanks for bringing the specs here...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic