• 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

Help: Wrox Profession SCWCD - don't understand

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is taken from P.225 of Professional SCWCD Certification by Wrox:

<jsp:useBean id="user" class="com.wrox.scwcd.chapter10.UserBean" />
<jsp:setProperty name="user" property="firstName" value="Wrox" />
...
...
... make JSP pages more readable and to aid maintainability:
<%
UserBean user = (UserBean) page.findAttribute("user");
if( user != null) {
user.setFirstName("Wrox");
}
%>

Ok, my question is, the implicit object page doesn't have the method findAttribute(). Should it be pageContext.findAttribute("user") instead?
Kelvin
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, according to that same book, the "page" implicit variable is of type Object, so you are correct, it wouldn't have a findAttribute() method. I suppose however, that the page implementation class *could* define a findAttribute() method, in which case you could call it using the page variable, if you cast it to the appropriate type.
I think that this is a typo though. The PageContext object *does* have findAttribute() method, so it looks very much like a mistake in the book to me.
 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can cast it to a type that has a findAttribute method.
The only class that has findAttribute() is PageContext and JspContext, but the page object should be of type "Servlet".
So I guess this is a mistake on the book.
 
Phil Rhodes
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I guess this is a mistake on the book.
Sure seems like it...
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kelvin,

Originally posted by Kelvin Hung:
The following code is taken from P.225 of Professional SCWCD Certification by Wrox:

<jsp:useBean id="user" class="com.wrox.scwcd.chapter10.UserBean" />
<jsp:setProperty name="user" property="firstName" value="Wrox" />
...
...
... make JSP pages more readable and to aid maintainability:
<%
UserBean user = (UserBean) page.findAttribute("user");
if( user != null) {
user.setFirstName("Wrox");
}
%>

Ok, my question is, the implicit object page doesn't have the method findAttribute(). Should it be pageContext.findAttribute("user") instead?


I think the truth is far from what the book said. I tested with one JSP page, on JavaBean Class, and I checked the translated java class for the JSP page:
JSP page is test.jsp:

<jsp irective.page isELIgnored="false" />

<jsp irective.include file="header.jspf" />

<br/>
<jsp:useBean id="gr" class="com.hover.Htest"/>
<jsp:setProperty name="gr" property="greeting" value="The art of jsp"/>

${gr.greeting}

<%@include file="footer.jspf" %>


the JavaBean is:


package com.hover;
import java.io.Serializable;
public class Htest implements Serializable {
private String greeting;

public void setGreeting(String g){
greeting = g;
}
public String getGreeting(){
return greeting;
}
}


Now mostly insteresting, the following is relative part from what is generated for test.jsp:


synchronized (pageContext) {
gr = (com.hover.Htest) pageContext.getAttribute("gr", PageContext.PAGE_SCOPE);
if (gr == null){
gr = new com.hover.Htest();
pageContext.setAttribute("gr", gr, PageContext.PAGE_SCOPE);
}
}


As we see:
- "pageContext" is used to retrieve the JavaBean
- Another parameter must specified for pageContext.getAttribute is the scope of the JavaBean, "PageContext.PAGE_SCOPE". The reason for page scope is we didn't specify scope in <jsp:useBean>, and the default scope value is "page".
- if the JavaBean has not existed, it will be created at this phase

Hope this is helpful.
( btw, kelvin, do I know you before? )
[ January 17, 2004: Message edited by: hover cheng ]
 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hover,
Thank for testing the code. So I guess the book is right except the page object should be pageContext object. findAttribute() and getAttribute() are pretty similar.
I will send you a PM.
Kelvin
 
reply
    Bookmark Topic Watch Topic
  • New Topic