aspose file tools*
The moose likes Bunkhouse Porch and the fly likes Question regarding EL and standard action from HF Servlets and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Books » Bunkhouse Porch
Bookmark "Question regarding EL and standard action from HF Servlets and JSP" Watch "Question regarding EL and standard action from HF Servlets and JSP" New topic
Author

Question regarding EL and standard action from HF Servlets and JSP

Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
[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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
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?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.
Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

If the servlet put the bean in a scope, it will be visible.
Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
this statement is not enough to put the bean in request scope ?

request.setAttribute("person",p);
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.
Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.

Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
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 ???

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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

Check your syntax.


Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
Sorry its a typo mistake by me , in book its actually is :

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

Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
My question has been moved to which forum?I checked book reviews forum but its not there.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

The forum is prominently displayed on the page.

Have you checked the book's errata to see if these errors have already been reported?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding EL and standard action from HF Servlets and JSP