Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

previously discussed question  RSS feed

Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is how thing work when you stretch the <jsp:getProperty .... /> in various scopes.
The "ID" of the bean is the same "hello" in all the scopes (page|request|session|application)
The Bean in my example is same as what ersin used - Has a String field called "beanScope" instead of "name" used by ersin.
My conclusion: The (s)getProperty searches for the bean named "hello" first in the page then in request then in the session and finally the application scope.
(The page scope is special since using the <jsp:include ...> action will give an error, by design, saying that there is no bean "hello" in the page scope.)
You can test this by changing the scopes of the beans created in the files "two.jsp" and "three.jsp" (or one.jsp for theat matter).
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="application" />
<jsp:setProperty name="hello" property="beanScope" value="Ersin" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<P>
<jsp:include page="two.jsp" /><BR>
<jsp:setProperty name="hello" property="beanScope" value="Satya" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<P>
<jsp:include page="three.jsp" /><BR>
<jsp:setProperty name="hello" property="beanScope" value="Guy" />
Hello, <jsp:getProperty name="hello" property="beanScope"/>!<BR>
This is Request <%=((madhav.bean1.TestBean1)request.getAttribute("hello")).getBeanScope() %><BR>
This is Session <%=((madhav.bean1.TestBean1)session.getAttribute("hello")).getBeanScope() %><BR>
This is Application <%=((madhav.bean1.TestBean1)application.getAttribute("hello")).getBeanScope() %><BR>

<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="request" />
I come from two.jsp<BR>
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="session" />
I come from three.jsp<BR>

The output from Tomcat with this combination is:
Hello, Ersin!
I come from Two.jsp
Hello, Satya!
I come from three.jsp
Hello, Guy!
This is Request Guy
This is Session notSet
This is Application Ersin

can some body explain why the above output is generated.......pleaseeeeeeee
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. that was one of our favourite discussions.
- m
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!