using a variable within a JSP include directive

Discussion in 'Java' started by Falk Köppe, May 17, 2009.

  1. Falk Köppe

    Falk Köppe Guest

    Hello,

    I have a problem using a variable within a JSP include directive. The
    following code does not work for me:

    <%
    String page = "examplepage.jsp";
    %>

    <@ include file="<%= page %>" %>
    or
    <@ include file="${page}" %>
    or
    <@ include file=page %>

    Is it possible to use a variable within a JSP include directive? I
    know that I could use a variable within a JSP include action
    (<jsp:include page="<%= page %>" /> or <jsp:include page="${page}" />)
    but the content of my page does not change very often, so it should be
    included at compile time.

    Cheers
    Falk Köppe
     
    Falk Köppe, May 17, 2009
    #1
    1. Advertising

  2. In article
    <>,
    Falk Köppe <> wrote:

    > Hello,
    >
    > I have a problem using a variable within a JSP include directive. The
    > following code does not work for me:
    >
    > <%
    > String page = "examplepage.jsp";
    > %>
    >
    > <@ include file="<%= page %>" %>
    > or
    > <@ include file="${page}" %>
    > or
    > <@ include file=page %>
    >
    > Is it possible to use a variable within a JSP include directive?


    No, the value of the variable "page" is established when the compiled
    servlet is executed. In contrast, the include directive is applied at
    the time the JSP page is translated into a servlet, before the String
    containing the page name exits.

    > I know that I could use a variable within a JSP include action
    > (<jsp:include page="<%= page %>" /> or <jsp:include page="${page}"
    > />) but the content of my page does not change very often, so it
    > should be included at compile time.


    Out of curiosity, why not just use the include directive:

    <%@ include file="examplepage.jsp" %>

    [Note corrected JSP directive syntax.]

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, May 17, 2009
    #2
    1. Advertising

  3. Falk Köppe

    Falk Köppe Guest

    Thank you for your answer.

    > No, the value of the variable "page" is established when the compiled
    > servlet is executed. In contrast, the include directive is applied at
    > the time the JSP page is translated into a servlet, before the String
    > containing the page name exits.


    You are totally right that the variable is first established, after
    the include directive is evaluated. I did not think about this.

    > Out of curiosity, why not just use the include directive:
    >
    > <%@ include file="examplepage.jsp" %>


    Actually my specific example was, that I tried to introduce a include-
    folder variable, so I can easily switch to another directory if I have
    to.

    <%@ include file=${include_location}examplepage.jsp %>

    If this include-folder variable would be used in all include variables
    I could easily rename or move the folder without changing all include
    directives. But you are again absolutely right, that I will now only
    work with relative paths, leaving the name and the relative location
    of the include folder intact.

    Thank you again for pointing me to the sequence of statement
    evaluation, which I just not thought of.

    Greetings
    Falk Köppe
     
    Falk Köppe, May 17, 2009
    #3
  4. Falk Köppe

    Mark Space Guest

    Falk Köppe wrote:

    >
    > <%@ include file=${include_location}examplepage.jsp %>
    >
    > If this include-folder variable would be used in all include variables
    > I could easily rename or move the folder without changing all include
    > directives. But you are again absolutely right, that I will now only
    > work with relative paths, leaving the name and the relative location
    > of the include folder intact.


    One thing you can do is treat your include_location like a object file
    and have it built at during the compile/deploy step.

    Given:
    include_a/example_stuff.jsp
    include_b/example_stuff.jsp

    Have your build script decide which file to copy to the eventual location:

    include_location/example_stuff.jsp // determined at build time
    // from _a or _b
     
    Mark Space, May 17, 2009
    #4
  5. Falk Köppe

    Wojtek Guest

    Mark Space wrote :
    > Falk Köppe wrote:
    >
    >>
    >> <%@ include file=${include_location}examplepage.jsp %>
    >>
    >> If this include-folder variable would be used in all include variables
    >> I could easily rename or move the folder without changing all include
    >> directives. But you are again absolutely right, that I will now only
    >> work with relative paths, leaving the name and the relative location
    >> of the include folder intact.

    >
    > One thing you can do is treat your include_location like a object file and
    > have it built at during the compile/deploy step.
    >
    > Given:
    > include_a/example_stuff.jsp
    > include_b/example_stuff.jsp
    >
    > Have your build script decide which file to copy to the eventual location:
    >
    > include_location/example_stuff.jsp // determined at build time
    > // from _a or _b


    Which would still not allow a different file to included during
    runtime, only compile time.

    I'll bet the OP is coming from PHP which is interpreted and therefore
    you can include different files during runtime.

    jsp OTOH is compiled once, then the resulting class is cached and
    reused many times.


    --
    Wojtek :)
     
    Wojtek, May 19, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Farooq Khan

    using Include directive in ASP .NET

    Farooq Khan, Oct 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,274
    Kevin Spencer
    Oct 28, 2003
  2. Replies:
    0
    Views:
    4,386
  3. Anthony Judd

    Variable scope with Include Directive ???

    Anthony Judd, Jul 3, 2003, in forum: ASP General
    Replies:
    2
    Views:
    129
    Matthew Lock
    Jul 7, 2003
  4. *** HAWK

    Problem using 'include directive' in ASP

    *** HAWK, Nov 21, 2003, in forum: ASP General
    Replies:
    4
    Views:
    157
    *** HAWK
    Nov 23, 2003
  5. Replies:
    10
    Views:
    267
    Sherm Pendley
    May 25, 2005
Loading...

Share This Page