Page 1 of 1

About Question enthuware.jwpv6.2.944 :

Posted: Sat Jun 23, 2012 3:05 am
by ETS User
This answer on this question is wrong. It will not compile because variable account is not defined.

Code: Select all

<html>
    <body>
    <h1>Simple Bean Usage</h1>
    <% 
        pageContext.setAttribute("account", new com.enthuware.jwebplus.Account(1, "Bobby", 1000.0));
    %>
    
    Account Name is : 
    <jsp:useBean id="account" class="com.enthuware.jwebplus.Account">
        <jsp:setProperty name="account" property="name" param="name"/>
    </jsp:useBean>       
    <%=account.getName()%>
    </body>
</html>

Re: About Question enthuware.jwpv6.2.944 :

Posted: Sat Jun 23, 2012 6:12 am
by admin
It works fine. useBean defines the variable.

HTH,
Paul.

Re: About Question enthuware.jwpv6.2.944 :

Posted: Sat Jun 23, 2012 6:49 am
by ETS User
Indeed you are right. I wasn't aware that the variable becomes available after the useBean.

That is good to know because it also means that the following will not compile, due to
a duplicate variable "account".

Code: Select all

<%
Account account = new Account();
%>
<jsp:useBean id="account" class="Account"></jsp:useBean>
Thanks!

Re: About Question enthuware.jwpv6.2.944 :

Posted: Wed Apr 13, 2016 6:58 pm
by himaiMinh
But when I change the pageContext into request , it prints out "Crazy".

Code: Select all

 <%request.setAttribute("account", new com.nullhaus.Account(1,"Bobby", 1000.0));%>
          Account Name is :     <jsp:useBean id="account" class="com.nullhaus.Account">      
                <jsp:setProperty name="account" property="name" param="name"/>    
              </jsp:useBean>           
              <%=account.getName()%> 
  

It seems to me that the account instance has not been created after request.setAttribute(...)