• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output??

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will the following jsp page print for the name of the Account if the page is access through a url containing name=crazy as the query string? (Enthuware Mock)

(The code for Account.java is given in exhibit. )





1. Nothing. No name will be printed.
2. Bobby
3. crazy
4. None of the above is correct.

I guess the given correct option(in bold) is absolutely wrong.

here <%=account.getName()%> would have been valid and print the shown output, if account is a scripting variable. Here account is name of the attribute AND not exposed as a scripting variable to call the method of the Account bean.

Please correct me if I am getting it wrong.

Thanks
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Here account is name of the attribute AND not exposed as a scripting variable to call the method of the Account bean.


After all , finally all JSPs are converted to servlets and it would be easy if you imagine the translated code and all those jsp tags and scriptlets are translated available inside the same method (_jspservice).

Since "account" bean already exists in page scope and hence account name is fetched from already set object which is "bobby". Note here set property is not called as bean exists in page scope.

 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since "account" bean already exists in page scope and hence account name is fetched from already set object which is "bobby". Note here set property is not called as bean exists in page scope.


"account" is a variable refer to the bean object, which has not been created , because its already there in the page scope.

<%
pageContext.setAttribute("account", new com.enthuware.jwebplus.Account(1, "Bobby", 1000.0));
%>

In the above line account is a named attribute to com.enthuware.jwebplus.Account.

<%=account.getName()%> this line would be valid only if account is refer as id of <jsp:useBean>.

is anybody has to say something about it??

please help
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Have you tried to run the code and checked the output ?
As explained earlier , the attribute "account" set in pageContext is reffered in <jsp:useBean id="account" ..> as id="account" matches attribute name "account' and the same is used in the scriplet to fetch the getName() ? whats your doubt on this ?
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:

Have you tried to run the code and checked the output ?
As explained earlier , the attribute "account" set in pageContext is reffered in <jsp:useBean id="account" ..> as id="account" matches attribute name "account' and the same is used in the scriplet to fetch the getName() ? whats your doubt on this ?


dear, "id=account" is not invoked becouse there is already a bean Account availabe in pageConetxt
1. <%
pageContext.setAttribute("account", new com.enthuware.jwebplus.Account(1, "Bobby", 1000.0));
%>

this line set an attributr of Account in the pageContext scope. Right??

2. <jsp:useBean id="account" class="com.enthuware.jwebplus.Account">
<jsp:setProperty name="account" property="name" param="name"/>
</jsp:useBean>

This line is not executed becuase container already finds an attribute Account in the pagecontext scope. Since it never create id=account , we can not this to get the property of Account . <%=account.getName()%> not works!!

3. to get the desire output (Bobby) we first need to get the attribute from the pageContext and then we can male the call to print its properties.
something like this
Account account=(Account)pageContext.getAttribute(account);
<%=account.getName()%> will print Bobby.


I guess I am more clear this time.. Is anybosy please help me out to resolve this issue??


Thanks in Advance
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

2. <jsp:useBean id="account" class="com.enthuware.jwebplus.Account">
<jsp:setProperty name="account" property="name" param="name"/>
</jsp:useBean>
This line is not executed becuase container already finds an attribute Account in the pagecontext scope. Since it never create id=account , we can not this to get the property of Account . <%=account.getName()%> not works!!


Thats sounds interesting !! Please get me a copy from where you learnt
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poonam the jsp:usebean will get executed. Only the body of the jsp:usebean tag will not be executed as there is already an object in the page context. So the page will run normally as far as I know but I haven't tried it...
 
Poonam Agarwal
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an impression, that if container got the bean which is already created , it will never try to execute the <jsp:useBean> tag itself.
now i got clear that it will execute the <jsp:useBean> tag but bot the body. it ignore it.

Sorry to asked a really dumb question , but i got very confuse.

Thanks Ankit to get it clear. Thanks again
 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a different doubt but related to the same topic.

lets consider the jsp code which Poonam has mentioned



Here is my doubt. Doubt#1) I think implementation of this snippet line no. 11-13 will be vendor dependent. So when we say that this code
will be translated as what Poonam has mentioned (and as most of us understand) will it be translated in a similar way by all app severs ??

Poonam Agarwal wrote:

3. to get the desire output (Bobby) we first need to get the attribute from the pageContext and then we can male the call to print its properties.
something like this
Account account=(Account)pageContext.getAttribute(account);
<%=account.getName()%> will print Bobby.




I know that HFSJ translated it that way but they also mention at every point that implementation is vendor dependent.
They mostly give tomcat implementation.

Doubt#2) Also id attribute in jsp:useBean is the name of the attribute to which an object is bound in a particular scope. I think it doesn't mean that the (container generated) variable used to refer to that object will also take the name as "account". In HFSJ, they have used the same variable but nowhere they say that if id="X" then variable will take same name as id attribute in generated java code or in this case



Infact I have seen mostly generated-variables to start with underscore so it cud well be



As such
will not work. Answer here could be 4 as well. That's the reason jsp:useBean is used with jsp:getProperty

I know such logic of vendor-dependence could make every question confusing but I have just started preparing so if this is "the exam logic" I am fine with it.
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

As per the JSP spec,

The attribute id="beanInstanceName"

A variable that identifies the Bean in the scope you specify. You can use the variable name in expressions or scriptlets in the JSP file.

The name is case sensitive and must conform to the naming conventions of the scripting language used in the JSP page. If you use the Java programming language, the conventions in the Java Language Specification. If the Bean has already been created by another <jsp:useBean> element, the value of id must match the value of id used in the original <jsp:useBean> element.

So, the vendor should implement this, and we can use that name in scriptlets.

Kathir
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, for the aclaration!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic