This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean and default scope (head first mock exam question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean and default scope (head first mock exam question)" Watch "jsp:useBean and default scope (head first mock exam question)" New topic
Author

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

Ronan Dowd
Ranch Hand

Joined: Jan 21, 2006
Posts: 84
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>


SCJP 1.4 | OCWCD JEE 5
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1647
    
  25

Hi Ronan,

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

Regards,
Frits
Shobhan Ganta
Ranch Hand

Joined: Nov 11, 2010
Posts: 37
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
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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).


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:useBean and default scope (head first mock exam question)
 
Similar Threads
Error in JSP page: org.apache.jasper.JasperException
HTTP ERROR: 500
jspSetProperty Doubt??
Do we consider Escape chars in Exam or not?
Bean or Servlet