This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:include Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:include" Watch "jsp:include" New topic
Author

jsp:include

Jayashree Mohan
Ranch Hand

Joined: Nov 23, 2005
Posts: 37
What is the result displayed on the browser when the First.jsp is accessed.

First.jsp

<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="session" />
<jsp:setProperty name="myBean" property="myProperty" value="FirstValue"/>
<jsp:include page="Second.jsp"/>
<jsp:getProperty name="myBean" property="myProperty"/>


Second.jsp

<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myProperty" value="SecondValue"/>


The mock exam indicated that this would give a "compiler error". Since the jsp action tag was used, there shud be no problem while compiling.I think this shud compile and print the value "SecondValue"?

Am I rite??
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
from the JSP specs-
jsp:useBean section
The id Attribute
The id=�name� attribute/value tuple in a jsp:useBean action has special meaning
to a JSP container, at page translation time and at client request processing time. In
particular:
� the name must be unique within the translation unit, and identifies the particular
element in which it appears to the JSP container and page.
Duplicate id�s found in the same translation unit shall result in a fatal translation
error.


which is probably why you get the error.

additionally,

from the scope section of jsp:useBean

The scope Attribute
The scope=�page|request|session|application� attribute/value tuple is associated
with, and modifies the behavior of the id attribute described above (it has
both translation time and client request processing time semantics). In particular
it describes the namespace, the implicit lifecycle of the object reference
associated with the name, and the APIs used to access this association. For all
scopes, it is illegal to change the instance object so associated, such that its
new runtime type is a subset of the type(s) of the object previously so associated.


Even if it worked, you wouldn't have got "secondjsp" because if the container looked for the property on encountering the getProperty, it would first search Page, Request, Session and finally Application.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Jayashree Mohan
Ranch Hand

Joined: Nov 23, 2005
Posts: 37
Thanks !! That was very informative
Chandrakanth
Ranch Hand

Joined: Aug 16, 2005
Posts: 60
I am successfully getting the out put as : SecondValue

i think here
<jsp:include page="Second.jsp"/>

we are making a dynamic all to Second.jsp....so there will not be a conflict with the names of the id attributes...

because both the jsp files come under diff translation unit.

we get the translation error if this <jsp:include page="Second.jsp"/> would have been a static include <%@ include file="Second.jsp"/>

pls correct me if i am wrong
Vivek Kinra
Ranch Hand

Joined: Mar 11, 2006
Posts: 66
I agree with Chandrakanth
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
yep you guys are right, it works because its a jsp:include and not a <%@ include

but what baffles me is, that it prints "Second.jsp" and not "FirstValue"

the getProperty uses the PageContext.findAttribute() method to find the attribute, and this method looks for the attributes first in the scope Page -> Request -> Session -> Application
therefore it should find the myProperty property in the session scope?
am i confused? need to go through again I guess...damn
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
The answer is compilation error because you don't use fully qualified class name as value for class attribute in <jsp:useBean> standard action. If you used the fully qualified name the output would be firstValue because, as Akshay said, <jsp:getProperty> uses the findAttribute() method to find the attribute.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hey guys.... I am getting SecondValue printed. And the reason, IF I am not wrong is very obvious.

According to my understanding:


First.jsp

<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="session" />
<jsp:setProperty name="myBean" property="myProperty" value="FirstValue"/>
<jsp:include page="Second.jsp"/>
<jsp:getProperty name="myBean" property="myProperty"/>


This code creates a new instance of MyBean in the session scope and sets the value of its property myProperty to "FirstValue". Then It includes the Second.jsp page. This means whatever would be the output of second.jsp would be included in the output of First.jsp. Whenever we include some page, We also send all the objects in the current session scope to the included page.


Second.jsp
<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myProperty" value="SecondValue"/>


Here this is a differnt translation unit hence there is no name conflict. But since this page has got the object of MyBean in session from First.jsp, it does not creates a new Instance and just modifies the value of myProperty to SecondValue.

When we come back to First.jsp and use getProperty, remember that Second.jsp has changed the value of this object which is in session. Hence we get the output as SecondValue instead of FirstValue.

Hope this Helps!!

Regards.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
I hope that when we use "useBean" SCOPE also plays important role in whether to create a new instance or not. Am I Right?

Here, first.jsp talks about session scope and second.jsp talks about application scope and thus two different beans are created (one in each scope).

And when findAttribute finds one in session scope, it has to print "first value". I mean the value of the attribute from session scope takes the precedence.

Can someone justify this please?

Regards,
Mani


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi Mani,

I do agree with you that scope plays a very important role in deciding whether a new instance is to be created or not.


Here, first.jsp talks about session scope and second.jsp talks about application scope and thus two different beans are created (one in each scope).


But I think you are wrong here. nonetheless both the JSP talk about different scopes, TWO instances (beans) will not be created.

Remember that the First JSP creates the Bean in session scope. After that it makes a call to Second.jsp

Although Second.jsp is talking about application scope, but dont forget that IF the <jsp:useBean> can't find an attribute object it makes the new one.

Here before creating the new bean, it would search for the existing attribute....and voila!!! It will get one. Beacuse First.jsp added that attribute (the bean) in the session.

So it will just make the changes to the very same object. Hence we get the output as.....SecondValue.



Hopefully I am not wrong.

Cheers !!!
Ashley Bideau
Ranch Hand

Joined: Apr 22, 2006
Posts: 40
Charles,

Can you please answer this. We need some expert opinion

Thanks
Ashley
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="session" />
<jsp:setProperty name="myBean" property="myProperty" value="FirstValue"/>


The above quoted piece of JSP code, tries to find the attribute "myBean" in session. If it doesn't find it creates one and stores it in session as an attribute named "myBean".

session. setAttribute("myBean", new MyBean() )

After that, it sets the property of the MyBean object stored in pageContext.
((MyBean)pageContext.findAttribute("myBean")).setMyProperty("FirstValue");

<jsp:include page="Second.jsp"/>

This JSP code includes the Second.jsp. Which means it executes its _jspService() method.

Second.jsp
<%@ page language="java" import="com.mypackage.MyBean" %>
<jsp:useBean id="myBean" class="MyBean" scope="application"/>
<jsp:setProperty name="myBean" property="myProperty" value="SecondValue"/>

This JSP code, tries to find the attribute "myBean" in servlet context (application). If it doesn't find it creates one and stores it in servlet context (application) as an attribute named "myBean".

application. setAttribute("myBean", new MyBean() )

After that, it sets the property of the MyBean object stored in pageContext.
((MyBean)pageContext.findAttribute("myBean")).setMyProperty("FirstValue");

pageContext.findAttribute("myBean") searches for the attribute "myBean" in page, request, session , and application scopes in order and returns the value associated or null.

Since page context searches for "myBean" in session scope before application scope it gets the "myBean" object stored in session and overrides the value of property myProperty to "FirstValue".

First.jsp
<jsp:getProperty name="myBean" property="myProperty"/>


This dispays the current value set in myProperty of myBean.
((MyBean)pageContext.findAttribute("myBean")).getMyProperty()

which displays "SecondValue"

Hope this helps.
Ashley Bideau
Ranch Hand

Joined: Apr 22, 2006
Posts: 40
I think out of all the solutions posted this one looks to be most logical. Thanks a lot for clarifying this.

-Ashley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:include
 
Similar Threads
JSP - UseBean
Charles can you please clarify this question
one more Jpilot ques.
jsp:useBean question
Java Beans