You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Marc Hughes <ma...@bookpool.com> on 2004/03/02 18:09:34 UTC

scope & order of initialization of variabes & includes

I have the following situation:

Page1.jsp:

<jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
<%   bandbg = "ok";   %>
First page: <%= bandbg %>
<jsp:include page="page2.jsp"  />


Page2.jsp:
<jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
<BR>Second page: <%= bandbg %>


Viewing page1.jsp I get the output:
First page: ok
Second page:

I thought I should get the output:
First page: ok
Second page: ok

I've tried with application & session scope too.

I'm assuming I'm just misunderstanding how things work.  Maybe tomcat 
starts processing includes before it processes the page or something?  
Any comments?

Thanks
-Marc

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: scope & order of initialization of variabes & includes

Posted by Marc Hughes <ma...@bookpool.com>.
Ah.. figured it out...
<%  
  bandbg = "ok";  
%>

is like

<%  
bandbg =  new String("ok");  
%>

I wasn't setting a string, I was creating a new one... doh.

Thanks anyways!
-Marc


Marc Hughes wrote:

> I have the following situation:
>
> Page1.jsp:
>
> <jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
> <%   bandbg = "ok";   %>
> First page: <%= bandbg %>
> <jsp:include page="page2.jsp"  />
>
>
> Page2.jsp:
> <jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
> <BR>Second page: <%= bandbg %>
>
>
> Viewing page1.jsp I get the output:
> First page: ok
> Second page:
>
> I thought I should get the output:
> First page: ok
> Second page: ok
>
> I've tried with application & session scope too.
>
> I'm assuming I'm just misunderstanding how things work.  Maybe tomcat 
> starts processing includes before it processes the page or something?  
> Any comments?
>
> Thanks
> -Marc
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: scope & order of initialization of variabes & includes

Posted by Asim Alp <de...@educationalnetworks.net>.
I think you need to remove <jsp:useBean ... part from page2.

Asim

On Mar 2, 2004, at 12:09 PM, Marc Hughes wrote:

> I have the following situation:
>
> Page1.jsp:
>
> <jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
> <%   bandbg = "ok";   %>
> First page: <%= bandbg %>
> <jsp:include page="page2.jsp"  />
>
>
> Page2.jsp:
> <jsp:useBean id="bandbg" scope="request" class="java.lang.String" />
> <BR>Second page: <%= bandbg %>
>
>
> Viewing page1.jsp I get the output:
> First page: ok
> Second page:
>
> I thought I should get the output:
> First page: ok
> Second page: ok
>
> I've tried with application & session scope too.
>
> I'm assuming I'm just misunderstanding how things work.  Maybe tomcat 
> starts processing includes before it processes the page or something?  
> Any comments?
>
> Thanks
> -Marc
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: scope & order of initialization of variabes & includes

Posted by Christopher Schultz <ch...@comcast.net>.
Marc,

> I thought I should get the output:
> First page: ok
> Second page: ok

I think this is what's happening. (You can probably verify this by 
looking at the java source code generated by Tomcat from your JSP):

When you do a <jsp:useBean> and declare where the bean lives, the 
translator essentially converts that to this line of code:

String bandBg = (String)request.getAttribute("bandBg");

Then, later, you are using a scriptlet to do this:

bandBg = "ok";

In the other JSP, the same code executes:

String bandBg = (String)request.getAttribute("bandBg");

However, you never poked 'ok' back into the request, so it's still 
either null or blank or whatever it was when you started.

Setting the local reference bandBg in your Page1.jsp doesn't do anything 
outside of it. You'd have to do something like this in your scriptlet to 
make it work:

<% request.setAttribute("bandBg", "ok"); %>

If you're going to use <jsp:useBean>, you probably shouldn't screw 
around with those variables inside of scriptlets. Try to stick with the 
bean tags only. That generally means that you'll have to write your own 
bean instead of using a string (so it can have a 'setValue' method or 
something).

Hope that helps.

-chris