Forums Register Login

Question regarding EL and standard action from HF Servlets and JSP

+Pie Number of slices to send: Send
[size=12]I have 2 questions regarding EL and standard action from chapter 8 and 9 of HF Servlets and JSP.

QUESTION 1:
==========

In chapter 8 on page 399,it says :

"EL handles null values gracefully" and there is an example that says, "there is
an attribute named "bar" but does not have a property or key named "foo". "
so if there is an expression like

${bar[foo]} ,it will print null or nothing.I assume because foo is an unquoted
string so it will look for an attribute name foo to replace the property with foo's value.
But foo does not exists, so it will return null.

On page 457 last line of the bullet point says:

"Remember that the EL expression ${bean.notAProperty} will throw an exception."

My question is the difference of output of 2 scenerios above are because of
usage of operators (.) and [] ?


QUESTION 2:
===========

there is a question on page 358 in chapter 8 regarding <jsp:useBean> standard action
that what will be the output?

JSP code :

<jsp:useBean id="person" type="foo.Employee" request="scope">
jsp:setPropery name="person" property="name" value="Fred" />
</jsp:useBean>


Servlet code:

foo.Person p = new foo.Employee();
p.setName("Evan");
request.setAttribute("person",p);

Answere to this question on page 420 says:

FAILS at request time.The "person" attribute is stored at request scope,
so the <jsp:useBean> tag won't work since it specifies only a type.The
Container knows that if you have only a type specified,there must be an
existing bean attribute of that name and scope.

I am confused as it seems Servlet has done all the work i.e
created a bean and set it in attribute at request scope.All requirement
seems to be fullfilled then what is wrong here?

On page 356,last four line says:

if you use type without class,you better make certain that bean is already
stored as an attribute ,at the scope and with the id you put in the tag.

Thanks.
+Pie Number of slices to send: Send
1. There's no difference between . and []. The book may be in error. What happened when you tried it?

2. When type is used, the bean is assumed to already exist.
+Pie Number of slices to send: Send
I did not tried it yet ,just trying to making sense of it all. So if its error in book which statement is true about question 1:

${bar[foo]} will return null (if foo doesn't exists) ?
{bean.notAProperty} will throw an exception ?

About question 2, "person" bean already exists in servlet,and servlet then forward the request to JSP,then
why it will give error?

+Pie Number of slices to send: Send
1. This will best be answered by trying it yourself.

2. The JSP could care less what is declared in the servlet; it only "sees" scoped variables. If the scoped variable exists, there will be no problem.
+Pie Number of slices to send: Send
I will try the example for question 1 by myself.

I am sorry I couldn't understand your reply for question 2. Why JSP is not able to "see" "person" attribute
set by servlet ?

thanks.
+Pie Number of slices to send: Send
If the servlet put the bean in a scope, it will be visible.
+Pie Number of slices to send: Send
this statement is not enough to put the bean in request scope ?

request.setAttribute("person",p);
+Pie Number of slices to send: Send
Yes.

As this is a discussion of the veracity of the book more than anything else, it's been moved to the appropriate forum for discussion books.
+Pie Number of slices to send: Send
If request.setAttribute is not enough to set the bean in request scope than what else need to specify?
I always set the bean using this statment and get them in JSP using request.getAttribute.
what else is required in this example to have it available in JSP,please advice!

thanks.
+Pie Number of slices to send: Send
 

Mishaal Khan wrote:If request.setAttribute is not enough to set the bean in request scope than what else need to specify?


As I already said, it is enough.

+Pie Number of slices to send: Send
So this JSP code :

JSP code :

<jsp:useBean id="person" type="foo.Employee" request="scope">
jsp:setPropery name="person" property="name" value="Fred" />
</jsp:useBean>

should work fine if servlet has set :

request.setAttribute("person",p);

But the answere in book says:

FAILS at request time.The "person" attribute is stored at request scope,
so the <jsp:useBean> tag won't work since it specifies only a type.The
Container knows that if you have only a type specified,there must be an
existing bean attribute of that name and scope.

Confusing ???

+Pie Number of slices to send: Send
 

Mishaal Khan wrote:
<jsp:useBean id="person" type="foo.Employee" request="scope">


Check your syntax.


+Pie Number of slices to send: Send
Sorry its a typo mistake by me , in book its actually is :

<jsp:useBean id="person" type="foo.Employee" scope="request">

+Pie Number of slices to send: Send
My question has been moved to which forum?I checked book reviews forum but its not there.

Thanks.
+Pie Number of slices to send: Send
The forum is prominently displayed on the page.

Have you checked the book's errata to see if these errors have already been reported?
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2269 times.
Similar Threads
Head First Servlets and JSP:SCWCD Question
HFSJ Chapter 8 useBean question
Head First Book Questions
Question to Kathy
use of type in <jsp:usebean>
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:22:26.