Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in object retrival from application scope

 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
file 1 : jsp1.jsp
<jsp:useBean id="objRef" class="java.lang.String" scope="application" />

<%
objRef = "hello";
out.println(objRef);
%>

file 2 : jsp2.jsp

<jsp:useBean id="objRef" class="java.lang.String" scope="application" />

<%
out.println("bean value"+objRef);
%>

why jsp2.jsp is not printing the bean value hello.

Thanks in Advance.
 
Harshit Patel
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not printing "hello" because String class is final.

'jsp:useBean id="objRef" class="java.lang.String" scope="application"' creates an attribute in the application scope.

but in the scriptlet, you are doing objRef="hello"; this means that in your file1.jsp, the local variable (referenced by scriptlet) points to a new string "hello", but the original attribute is still an empty string.
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to call argumented constructor using useBean?
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - but if the object were not a string you could set the properties using the jsp:setProperty tags nested in the jsp:useBean tag.

In the given example, you'd just have to do this:

<%
objRef = "hello";
application.setAttribute("objRef", objRef);
%>

and the second jsp would print the value.
[ March 12, 2007: Message edited by: Anupama Ponnapalli ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic