IntelliJ Java IDE
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes previously discussed question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "previously discussed question" Watch "previously discussed question" New topic
Author

previously discussed question

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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).
one.jsp
<HTML>
<head><Title>ersin</title></head>
<body>
<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>
<P>
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>
</body></html>

two.jsp
=========
<jsp:useBean id="hello" class="madhav.bean1.TestBean1" scope="request" />
I come from two.jsp<BR>
three.jsp
==========
<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

Joined: Jun 03, 2000
Posts: 5040
Yup. that was one of our favourite discussions.
- m


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
 
subject: previously discussed question
 
Threads others viewed
jsp:getProperty calling bean from specific scope
jsp:useBean question
HFSJ 2ed - about <c:set>
doubt on getProperty in various scopes
Creating a bean in session scope
developer file tools