wood burning stoves 2.0*
The moose likes JSP and the fly likes Dynamically assign an included bean property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Dynamically assign an included bean property" Watch "Dynamically assign an included bean property" New topic
Author

Dynamically assign an included bean property

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,
Is there any reason why this doesn't work?
<jsp:include page="../includes/adminheader.jsp">
<jsp aram name="path" value="<% out.write(sPath); %>"/>
</jsp:include>
I'm trying to dynamically assign the value of the 'path' parameter. Basically, the value of sPath doesn't come through.
I'm not sure if this is the right way of going about it, and any recommendations are greatly appreciated.
Cheers,
Steve
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Looks like you are getting a little confused between the source code of the JSP (things like <jsp:include> tags) and the generated HTML (things like HTML, plain text, etc.).
What you seem to be trying to do is modify the source code of the JSP at the time it is compiled. To a certain degree this is possible. However, you can't do it using out.write() which is only executed when the JSP has been compiled and is running on the deployed server.
The trick is that JSP often allows you to use the superficially similar <%= expression %> syntax for the kind of source-level change you need. Try the follwoing:

and see if it works.
Of course, if you have JSP 2 (found in Tomcat 5 or Resin 3, for example), you should probably be using the new "EL" syntax instead:

[ January 08, 2004: Message edited by: Frank Carver ]

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Thanks Frank,
I tried the example you gave and it didn't work.
Maybe I should explain a bit better what I'm trying to do.
Basically, the path variable is used to pass the current location into the page header (i.e. sPath = "Books > Technical") which is then output in the header text. I'm basically trying to give the header some text before it loads, but the text is not something that can be hard-coded on the page - it very much depends on the way the page is called.
i.e. this works:
<jsp:include page="../includes/adminheader.jsp">
<jsp aram name="path" value="Books > Technical" />
</jsp:include>
but isn't practical.
In ASP you can simply share variables between included pages - I was using the parameter feature to try to mimic this:
example page:
<%
sPath = "Books > Technical"
%>
<#include admin header page here>
...
example admin header:
<html>
<body>
<%
out.write(sPath)
%>
...
Basically, the sPath variable is shared as if the admin header and example page were in the same application scope.
I hope this makes a bit more sense.
Thanks a lot for your help.
Cheers,
Steve
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Aha.
There is a much simpler way to do what you want. A JSP container provides four "contexts", which can be used to pass any value between servlets and JSPs.
  • "application" context is a single context available to all servlets and JSPs in an application.
  • "session" context is a seoarate context for each session (sequence of calls from the same client),
  • "request" context is a context which exists for the duration of the procesing a single HTTP request.
  • "page" context is a context whch exists only for the duration of the processing of a single JSP page.


  • In your case, the "request" context seems most appropriate, as it will be available to both your "calling" and "included" JSPs when they are both collaborating on responding to a single request.
    In your "calling" JSP, put something like:

    In your "included" JSP put something like:

    If you can do this "under the hood" in ASP, I guess that the ASP server probably places all values in somethlng akin to the "request" context by default.
    Has that helped?
    Steve Wood
    Ranch Hand

    Joined: Jan 08, 2003
    Posts: 137
    That's a much better way of doing it. Thanks a lot for your help - I think I was getting a bit bogged down with a certain line of thinking.
    Thanks again,
    Steve
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Dynamically assign an included bean property