• 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

jsp:useBean and default scope (head first mock exam question)

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

the below question is from Head First book, mock exam question 4. It's saying that C and D are the correct answers but I
would argue, strictly speaking that none of the below are correct. The reason being that the servlet code stores the 'user' in
request scope but all of the jsp:useBean snippets search in page scope (as page scope is the default scope of jsp:useBean if no scope is explicitly defined).

Any thoughts would be good? Thanks, Ronan.

A Servlet sets up a bean before forwarding to a JSP.
Given:

foo.User user = new foo.User();
user.setFirst(request.getParameter("firstName"));
user.setLast(request.getParameter("lastName"));
user.setStreet(request.getParameter("streetAddress"));
user.setCity(request.getParameter("city"));
user.setState(request.getParameter("state"));
user.setZipCode(request.getParameter("zipCode"));
request.setAttribute("user", user);

What snippet, if placed in a JSP, could replace the Servlet code above? (Choose all that apply.)

A. <jsp:useBean id="user" type="foo.User"/>

B. <jsp:useBean id="user" type="foo.User">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>

C.
<jsp:useBean id="user" class="foo.User">
<jsp:setProperty name="user" property="first" param="firstName"/>
<jsp:setProperty name="user" property="last" param="lastName"/>
<jsp:setProperty name="user" property="street" param="streetAddress"/>
<jsp:setProperty name="user" property="city"/>
<jsp:setProperty name="user" property="state"/>
<jsp:setProperty name="user" property="zipCode"/>
</jsp:useBean>

D. <jsp:useBean id="user" class="foo.User">
<jsp:setProperty name="user" property="*"/>
<jsp:setProperty name="user" property="first" param="firstName"/>
<jsp:setProperty name="user" property="last" param="lastName"/>
<jsp:setProperty name="user" property="street" param="streetAddress"/>
</jsp:useBean>
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ronan,

I agree: the scope should have been put explicitely to "request".

Regards,
Frits
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess this is the correct version of the question.


A Servlet sets up a bean before forwarding to a JSP.
Given:
20. foo.User user = new foo.User();
21. user.setFirst(request.getParameter("firstName"));
22. user.setLast(request.getParameter("lastName"));
23. user.setStreet(request.getParameter("streetAddress"));
24. user.setCity(request.getParameter("city"));
25. user.setState(request.getParameter("state"));
26. user.setZipCode(request.getParameter("zipCode"));
27. request.setAttribute("user", user);

What snippet, if placed in a JSP, could replace the Servlet code above? (Choose all that apply.)
A. <jsp:useBean id="user" type="foo.User"/ scope="request">

B. <jsp:useBean id="user" type="foo.User" scope="request">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>

C. <jsp:useBean id="user" class="foo.User" scope="request">
<jsp:setProperty name="user" property="first" param="firstName"/>
<jsp:setProperty name="user" property="last" param="lastName"/>
<jsp:setProperty name="user" property="street" param="streetAddress"/>
<jsp:setProperty name="user" property="city"/>
<jsp:setProperty name="user" property="state"/>
<jsp:setProperty name="user" property="zipCode"/>
</jsp:useBean>

D. <jsp:useBean id="user" class="foo.User" scope="request">
<jsp:setProperty name="user" property="*"/>
<jsp:setProperty name="user" property="first" param="firstName"/>
<jsp:setProperty name="user" property="last" param="lastName"/>
<jsp:setProperty name="user" property="street" param="streetAddress"/>
</jsp:useBean>



Thanks
Shobhan
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ronan Dowd wrote:Hi all,

the below question is from Head First book, mock exam question 4. It's saying that C and D are the correct answers but I
would argue, strictly speaking that none of the below are correct. The reason being that the servlet code stores the 'user' in
request scope but all of the jsp:useBean snippets search in page scope (as page scope is the default scope of jsp:useBean if no scope is explicitly defined).

Any thoughts would be good? Thanks, Ronan.



Ronan,

Check out the Errata page of this book.

If you search for "page 794" & scroll down to it, this is what the question-submitter & the authors have to say about this question:
The sample code in question, set the attribute in request scope. And equivalent code snippet answers are given as C & D. But those two answers create the attribute in page scope (default scope), but not in request scope. scope = 'request' is missing in those two answers.

Note from the Author or Editor:This is correct. The text [scope="request"] must be added to the open jsp:useBean tag for *all* options (not just C&D, because this is really an invariant to the question).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic