• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL access operators doubt in HF

 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mock exam question in Chap 8: (using scriptless JSP)

Which about El access operators are true: (choose all that apply)

A) Anywhere the dot operator is used [] operator could be used instead
B) anywhere the [] operator is used the dot operator could be used instead
c) If the dot operator is used to access a bean property and the property doesn't exist an exception is thrown
d) There are some situations where dot operator must be used and others where the [] operator must be used

My answers were A & C

But the HFSJ errata link says:

[432] Question 17, Option C;
option C is invalid and should not be checked.

But trying an example proves otherwise, that is an exception is thrown! Has anybody tried this and did not get an exception?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please show us your sample ?
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, this is a long reply, but you asked for it

I have a html that has submit button. When I click it, a controller servlet is invoked. The servlet sets a name property in a bean (with a single userName property) and sets the bean as a request attribute and dispatches the request to a jsp. The jsp tries printing different stuff. Here are the Bean, Servlet, JSP, html & DD respectively:

PersonBean.java

package com.example;
import java.util.*;
public class PersonBean
{
String name;
public String getUserName()
{
return name;
}
public void setUserName(String name)
{
this.name=name;
}

}

PersonBeanServlet.java

package com.example;
import java.util.*;
public class PersonBean
{
String name;
public String getUserName()
{
return name;
}
public void setUserName(String name)
{
this.name=name;
}

}

PersonBeanPage.jsp

The name of the person is : ${person1.userName} or ${person1["userName"]}<br>
The address of the person is: ${person1[address]}<br>
The address of the person is: ${person1["address"]} or ${person1.address} <br>
The attribute that is not present is: ${foo}

FirstPage.html

<html>
<body>
<form method="post" action="Person.do">
<input name="submit" type="submit>
</form>
</body></html>

Web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>MyTest</servlet-name>
<servlet-class>com.example.PersonBeanServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyTest</servlet-name>
<url-pattern>/Person.do</url-pattern>
</servlet-mapping>
</web-app>

With the PersonBeanPage.jsp, I tried several outputs commenting the third line gives the output as follows:

The name of the person is : Joe or Joe
The address of the person is:
The attribute that is not present is:

Removing the comment results in the following exception:

javax.servlet.ServletException: Unable to find a value for "address" in object of class "com.example.PersonBean" using operator "[]"
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.PersonBeanPage_jsp._jspService(org.apache.jsp.PersonBeanPage_jsp:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.example.PersonBeanServlet.doPost(PersonBeanServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

javax.servlet.jsp.el.ELException: Unable to find a value for "address" in object of class "com.example.PersonBean" using operator "[]"
org.apache.commons.el.Logger.logError(Logger.java:481)
org.apache.commons.el.Logger.logError(Logger.java:498)
org.apache.commons.el.Logger.logError(Logger.java:611)
org.apache.commons.el.ArraySuffix.evaluate(ArraySuffix.java:340)
org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:920)
org.apache.jsp.PersonBeanPage_jsp._jspService(org.apache.jsp.PersonBeanPage_jsp:51)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.example.PersonBeanServlet.doPost(PersonBeanServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,

The name of the person is : ${person1.userName} or ${person1["userName"]}<br>
The address of the person is: ${person1[address]}<br>
The address of the person is: ${person1["address"]} or ${person1.address} <br>
The attribute that is not present is: ${foo}


Where does your doubt lie ?

1. Line one will get the user name (note that you should name your member variable "userName", not "name")

2. Line two, a scoped parameter called "address" is used to access a bean property. The "address" variable does not exist. This is equivalent to ${person1[null]}, and is ignored. Try to add :
<c:set var="address" value="userName"/>, and you'll get the user name.

3. Line three will call getAddress of the person1 bean. It does not exist, so will generate an error.
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:

3. Line three will call getAddress of the person1 bean. It does not exist, so will generate an error.


It is the line 3 that I am relating to the option c of the question. Since line 3 results in an exception, I conclude that the answers should be A & C.

But the errata link is contradicting my output. Don't they mean that the statement is false by saying that option c is invalid and that it should not be checked?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you are right.

There would be no error if the bean did not exist,
but if it exists it will throw an error.

In the spec, it says :
JSP.2.3.4 Operators "[]" and "."
� Otherwise (a JavaBeans object), coerce value-b to String
- If value-b is a readable property of value-a, as per the JavaBeans specification:
� If getter throws an exception: error
� Otherwise: return result of getter call-
- Otherwise: error.

C should be correct as you said.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
option C should be correct


page 447 (key points and gotchas- last point) it says EL expression by itself will NOT cause an exception if the property does not exist.so even though $(fooBean.notAProperty) won't cause an exception by itself(it just returns null) etc



According to page 395 EL is null friendly.


This will make us to think that option c should NOT be checked. Because the EL expression for


he address of the person is: ${person1["address"]} or ${person1.address} <br>


will return null and since EL is null friendly any expection thats produced will result in just blank space as output.

But what we are getting is an exception. Hence page 447 is wrong.


Otherwise (a JavaBeans object), coerce value-b to String
If value-b is a readable property of value-a, as per the JavaBeans specification:
� If getter throws an exception: error
� Otherwise: return result of getter call
Otherwise: error.


Read the last point.


Otherwise: error.


Thats what we get as output.

Hope my explanation justifies option c is correct.
[ February 07, 2006: Message edited by: Vishnu Prakash ]
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I just wanted to be sure that I am not wrong. Now I know what to answer if a question like this appears on the final exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic