• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

jsp:include

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks !! That was very informative
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Chandrakanth
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charles,

Can you please answer this. We need some expert opinion

Thanks
Ashley
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<%@ 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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think out of all the solutions posted this one looks to be most logical. Thanks a lot for clarifying this.

-Ashley
 
reply
    Bookmark Topic Watch Topic
  • New Topic