How to change a java include file string with a java variable?

Posted on 2008-11-11
Last Modified: 2013-11-23
How do you change the include file string with a Java variable?

For example:

<%@ include file="../flash_menus/menu_16.jsp" %> I want to make the number 16 dynamic, replace it with a variable. I don't understand the syntax to do it. Nothing has worked so far.

Here is my include file below and the variable I want to put in it.
<!-- Include file -->

<%@ include file="../flash_menus/menu_16.jsp" %>


<!-- Variable -->

<% ResponseBean.getTopBarMainID(0) %>

Open in new window

Question by:the_hero
    LVL 15

    Assisted Solution


    Try :

    <% String file="menu_";
          String path = "../flash_menus/";
          String location = path+file+"16.jsp";

    <jsp:include page="<%=location%>" flush="true"/>

    Reminded that per the JSP include sytanx you can use either the file-name in
    quotes directly or you can give it as a variable within scriptlet tags but not as a combination of both like

    <jsp:include page="../flash_menus/menu_<%=location%>.jsp" flush="true"/>
    LVL 1

    Author Comment

    Thank you very much. I utilized your code and it works great. However, I need to be able to dynamically change "16". SO I tried adding my databean there and when I do it won't show my include file.

    Here is what I did to the code. Let me know how to correctly write this so I can change 16 also.

          String path = "../flash_menus/";
    	  String file="menu_";
    	  String ID = ResponseBean.getTopBarMainWidth(0);
          String location = path+file+ID+".jsp";
    <jsp:include page="<%=location%>" flush="true"/>

    Open in new window

    LVL 1

    Accepted Solution

    Okay so the above code I displayed works. I just had a little error in my include file. Thank you for your help!

    Here is the final solution thanks to the help of quincydude:
          String path = "../flash_menus/menu_";
    	  String file= ResponseBean.getTopBarID(0);
    	  String end =".jsp";
          String location = path+file+end;
    <jsp:include page="<%=location%>" flush="true"/>

    Open in new window


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now