A servlet sets a session-scoped attribute product with an instance of com.example.product an
forward to a JSP. Which two output the name of the product in the response? (Choose two)
A.
<%= product.getName() %>
B.
<jsp:useBean id=”product” class=”com.example.Product” />
<%= product.getName() %>
C.
<jsp:useBean id=”com.example.Product” scope=”page”>
<%= product.getName() %>
</jsp:useBean>
D.
<jsp:useBean id=”product” type=”com.example.Product”
scope=”page” />
<%= product.getName() %>
E.
<jsp:useBean id=”product” type=”com.example.Product”>
<%= product.getName() %>
</jsp:useBean>
I don’t understand. Question ask “session-scoped attribute”, why all options are using scope=”page”.
I agree with you. And if the exam aswers would be wrong? 🙂
…and so the questions
And if it were ‘page’ It would have been B,D. Why C?
B,D
B,D
B and D.
>>> BUT <<<
D should B scope=session.
B would write "Default
D would write the name of the product in the session
B is the only answer.
B searches in all scope. Finally B prints the session value.
D cannot be the answer. Because product is not available in page scope. so new product object needs to be auto instantiated. For auto instantiation class attribute is required. Here class attribute is missing. so the output of D would be some exception.
It wont’ search in all scopes because the default scope is page.
I have not tested it.
A and B both may be the answer.
Supposing scope=”page” is scope=”session”, answer should be C and D.
B is false as default scope value is “page” (is not like an EL).
C is true as the body can contain anything.
the answer is B,D.
Now I realize. It’s B,D.
But it’s as Allan Santos says:
B is a new Product, not the one in session (I was right that B is default scope: session. But as no “product” yet exists in page scope, it creates a new one).
C was completly wrong, as it doesn’t have any class or type attribute.