File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL access operators doubt in HF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL access operators doubt in HF" Watch "EL access operators doubt in HF" New topic
Author

EL access operators doubt in HF

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
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?


Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Could you please show us your sample ?


[My Blog]
All roads lead to JavaRanch
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 04, 2004
Posts: 89
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 15, 2004
Posts: 1026
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 ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EL access operators doubt in HF