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

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

LVL 1
Shaye LarsenAsked:
Who is Participating?
 
Shaye LarsenConnect With a Mentor Author Commented:
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

0
 
quincydudeConnect With a Mentor Commented:

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"/>
0
 
Shaye LarsenAuthor Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.