jQuery in Action, 2nd edition*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes include directive confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "include directive confusion" Watch "include directive confusion" New topic
Author

include directive confusion

Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Dear All,
Please look at result.jsp


And following is Header.jspf


This works fine. And note that 'jsp:useBean' is declared after 'Header.jspf' is included. Also note that in the translated result_jsp.java file, 'person' variable is declared after included code of 'Header.jspf' .

If i remove 'jsp:useBean' line then translator complaints as -
General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3.

So i conclude that it requires to introduce bean with name 'person' before the jsp:getProperty code.
But if i add 'jsp:useBean' after include directive, it works fine. How it is possible?
I was expecting translation time error.

Regards







Regards, Sharmila.
We will have a future if we save Nature!
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Sharmila.

Can you please post, in which container/server you are using and the code of 'result_jsp.java' file?
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Thanks Chinmaya,
Using tomcat 5.5.28. Servlet container 2.4, Jsp 2.0 and java 1.5
Regards
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Thanks for reply. Have you set 'person' attribute in any scopes like application, session etc. ?
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Thanks Chinmaya for your reply,
No there is no 'person' in any scope. And i call result.jsp as -
http://localhost:8080/Beer-v1/result.jsp

Code responsible for searching 'person' in the jsp:getProperty tag in Header.jspf in result.jsp is as -
org.apache.jasper.runtime.JspRuntimeLibrary.toString((((foo.Employee)_jspx_page_context.findAttribute("person")).getName()))

Then how come result.java is not created if i do not add jsp:useBean into result.jsp. And browser
displays error as -
"General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3"

Regards
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Then how come result.java is not created if i do not add jsp:useBean into result.jsp. And browser
displays error as -
"General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3"


The specification says,
The <jsp:getProperty> action places the value of a bean instance property, converted
to a String, into the implicit out object, from which the value can be displayed
as output. The bean instance must be defined as indicated in the name attribute before this point in the page (usually via a jsp:useBean action).


Sharmila wroteThis works fine. And note that 'jsp:useBean' is declared after 'Header.jspf' is included. Also note that in the translated result_jsp.java file, 'person' variable is declared after included code of 'Header.jspf' .

Can you please post, what is the result, when accessing 'result.jsp'?
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Dear Chinmaya,
Please look at the .java code bellow-
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Sharmila.

In result_jsp.jsp,
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "name", "bugs bunny", null, null, false);
presents means, there is presents in 'result.jsp'.

What happens, when you are trying to access this through browser, calling http://localhost:8080/Beer-v1/result.jsp? Are you getting any exception or got 'bugs bunny'?
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Thanks Chinmaya,
Can we start new topic? This is become very big. And by mistake i have given you wrong translated .java file.
Please let me start new topic and i will put there all things you need upfront.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Can we start new topic? This is become very big. And by mistake i have given you wrong translated .java file.
Please let me start new topic and i will put there all things you need upfront.

Sharmila, I cann't answer this question. Our forum leader 'Christophe Verré' will decide.


Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Dear Chinmaya,
I do not know exact rules. But looks like we cannot start new topic on same subject. So let me put things here only.
I access the following code with "http://localhost:8080/Beer-v1/result.jsp" This code does not create .java file and displays error on browser as -
General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3
So i think it says the bean 'person' need to introduce before included code is run.
Now i add jsp:useBean to the result.jsp as -
This time .java file is created and prints 'General exception null'
Note that i have added jsp:useBean added after include directive. My question is how come translator knows that jsp:useBean is available. I was expecting
General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3
because it is added after include directive.
The generated result_jsp.java with added jsp:useBean is as -


Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Note that i have added jsp:useBean added after include directive. My question is how come translator knows that jsp:useBean is available. I was expecting

General exception jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3

because it is added after include directive.


During translation, jsp engine first sees the include directive, of result.jsp and translates it into and sees
<jsp:useBean id="person" class="foo.Employee" scope="request"/>
translates it into
Now translation is over and compiles it. After loading, instantiating, initialization, it is eligible to be a servlet. When a request comes to the 'result.jsp', first it sees the generated byte codes of the
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((foo.Employee)_jspx_page_context.findAttribute("person")).getName())));
and it checks is there 'person' attribute present in all scopes ordering from application, session, request, page scopes and if it finds gets the 'Employee' object, and cast it with 'foo.Employee' and calls the 'getName()' on it. Here there is no 'person' attribute available. So it returns 'null'. Calling on 'null' produces 'NullPointerException'. Since our 'person' attribute was set into 'request' by our jsp:useBean after 'jsp:getProperty', here 'person' attribute was not available.

We can use jsp:getProperty without using jsp:useBean. All we have is set the specified attribute in any one of the scopes(application, session, request, page).
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Dear Chinmaya,
Got the point-
We can use jsp:getProperty without using jsp:useBean. All we have is set the specified attribute in any one of the scopes(application, session, request, page).

Thanks for your help
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

This is working but I don't think that the JSP spec says that. The following is from the specs
The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.
The object named by the name must have been “introduced” to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.

Note – A consequence of the previous paragraph is that objects that are stored in, say, the session by a front component are not automatically visible to jsp:setProperty and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible.


In my tomcat 6, the line
actually translates to
So it works if there is an attribute named person in the Page scope and not in any other scope...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Thanks Ankit and Chinmaya,
Understood the problem i was facing.


Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Hi guys,

I'm bringing this thread of discussions back to live
I read this topic many times and tried lot of different tests but I still don't understand some aspects. Here are some of my tests and their results:

1)
- I've set an attribute in request scope (this is done by a servlet before forwarding the request to the JSP
- In JSP I put in this order:
+ getProperty
+ useBean with scope set to request
- Result : Everything worked fine and the name of the person was displayed

2)
- No attribute set in any scope in servlet
- In JSP I put in this order:
+ getProperty
+ useBean with scope set to request
- Result: Null pointer exception

3)
- No attribute set in any scope in servlet
- In JSP I put only
+ getProperty
- Result rg.apache.jasper.JasperException: jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3

4)
- I've set an attribute in request scope (this is done by a servlet before forwarding the request to the JSP
- In JSP I put only
+ getProperty
- Result rg.apache.jasper.JasperException: jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3

From the above results i conclude that:
- In neighter cases I can use only getProperty without a useBean . See 3) 4)
- I don't understand why first experiment worked properly ( point 1) as the object named by the name had been “introduced” to the JSP processor using the
jsp:useBean after I used jsp:getProperty . This is in contradiction with what specs says. How does the container know that there is an object named
"person" because it was "introduced" later?Does it use somethink like a "forward reference" and manage to "sees" the object ?


Chinmaya Chowdary wrote:
We can use jsp:getProperty without using jsp:useBean. All we have is set the specified attribute in any one of the scopes(application, session, request, page).


Can you please give an example to confirm your statement?

thanks a lot!


SCJP 6.0 (88%)
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi, Dragos.

Suppose if we set the name of the 'Person' and store the 'Person' object in request scope and forward it to the 'result.jsp' like,

And if our 'result.jsp' contains, then we will get the name of the person as 'Dragos'. Here we are using only jsp:getProperty without using jsp:useBean.
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Hi Chinmaya,

Thanks for your explanations but I already tried this and don't work for me.
As I said in my previous post:

4)
- I've set an attribute in request scope (this is done by a servlet before forwarding the request to the JSP
- In JSP I put only
+ getProperty
- Result is org.apache.jasper.JasperException: jsp:getProperty for bean with name 'person'. Name was not previously introduced as per JSP.5.3

Could be container dependent?

Thank you
 
wood burning stoves
 
subject: include directive confusion