• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question regarding EL and standard action from HF Servlets and JSP

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the servlet put the bean in a scope, it will be visible.
 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this statement is not enough to put the bean in request scope ?

request.setAttribute("person",p);
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Check your syntax.


 
Mishaal Khan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question has been moved to which forum?I checked book reviews forum but its not there.

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The forum is prominently displayed on the page.

Have you checked the book's errata to see if these errors have already been reported?
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic