Dynamic variables in a jsp page


I have been this problem for a while now:

I have been doing this for now,

<c:set var="flag1" value="0"/>	
<c:set var="flag2" value="0"/>
<c:set var="flag3" value="0"/>
<c:set var="flag4" value="0"/>
<c:set var="flag5" value="0"/>
<c:set var="flag6" value="0"/>
<c:set var="flag7" value="0"/>
<c:set var="flag8" value="0"/>
<c:set var="flag9" value="0"/>
<c:set var="flag10" value="0"/>
<c:set var="flag11" value="0"/>
<c:set var="flag12" value="0"/>

What I want is a simple way to get these 12 variables declared . Something like this

<c:forEach var="flagVar" start="1" end="12">
 <c:set var="flag${flagVar}" value="0"/>

But this also does’nt work though. How can I get this done please. Urgent help needed.:confused:[/LEFT][/LEFT]

The way you’re trying should work, although I haven’t actually tested it. But you can do it like this:

for (int i=0; i<12; i++) {
  pageContext.setAttribute("flag" + i, new Integer(0));

What are you actually trying to do though? Seems like a strange requirement but if you give us some more info maybe there’s a better solution.

Sounds like using Map is better option for this one

<jsp:useBean id=“exampleMap” class=“java.util.HashMap”/>
<c:forEach var=“flagVar” start=“1” end=“12”>
<c:set target=“${exampleMap}” property=“${flagVar }” value=“0”/>

Haven’t tested but I’ll go w/ Map or List if I were you.

@ rozner

I basically have a complex jsp with a lot of Database retrievals and using them dynamically. I have been using a set of flags which helps me to use the data to generate dynamic elements. I knew that the way I was declaring them was not optimum in anyway.

Thanks for the Solution. Please let me know if there is any better solution for this.

Suppose that I want selected flags to be assigned a new value at a later stage on the same jsp, then how do I do that following this ‘Map’ approach? Something like this

<c:set var="flag4" value="1"/>
<c:set var="flag5" value="1"/>
<c:set var="flag6" value="1"/>

Note that I want only three of those 12 flags to be assigned this new value.

Thank You:)

<c:set target=“${exampleMap}” property=“flag4” value=“1”/>

Just FYI, there is no such thing as dynamic variable in Java. If you’ve seen rozner’s code

for (int i=0; i<12; i++) {
pageContext.setAttribute(“flag” + i, new Integer(0));

pageContext is the variable and it’s sort of like Map by setting through “Key” and “Value”. Just want to clear up this for you. So this is the reason why I suggest using Map.

Also, I get a feeling you’re doing too much logic in the JSP. I suggest you move to Servlet. In pure form, JSP should only be used for displaying data and not do any logic.

It’s not a question, but I think i have the same problem here… Except that it’s a jsp page without servlets…

Here it goes:

<select name="slc_ville" class="form_element" id="slc_ville" onChange="searchfilter()" style=" width:150px">
	<option value="">All</option>
		java.sql.Connection con5;
		con5 = DriverManager.getConnection(url, user, password);
		Statement statement5=con5.createStatement();	
		String query5="select DISTINCT c.event_no_ville , v.ville, p.province_fr, p.province_en, p.abreviation FROM cmsSWI_contents c, cmsGeo_villes v, cmsGeo_provinces p WHERE v.no = c.event_no_ville AND v.no_province = p.no AND c.actif='1' AND c.date_fin >= CURDATE() AND c.heure_fin>= CURTIME() ORDER BY v.ville asc";
		ResultSet resultset5=statement5.executeQuery(query5);
		ResultSetMetaData rsm5 = resultset5.getMetaData();
		int ville_compteur;
		ville_compteur = 0;
		String selected_ville;
		while (resultset5.next())
			<option value="<%=resultset5.getInt(1)%>" ><%=resultset5.getString(2)%> <%=eval('slc_v'+ville_compteur)%> </option>

The problem is within my option tag. The eval part is not working. All I need is ‘slc_v’+ville_compteur to be interpreted as a variable, wich is defined earlier in the script.

I know it’s an easy thing to do in php or other languages, but I searched the web for hour and I can’t find anything.

Is there an equivalent for the eval() in JSP ??

Thanks everybody

There is sort of an equivalent of eval which could accomplish that but it’s not exactly the same and would be slightly complicated.

A better solution would be to use a Map. Then you can reference the Map keys with “slc_v” + ville_compteur instead of having variables.

Here’s some info on Maps

That’s a good tip, it does the job…

Good forum!