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
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;
Class.forName("org.gjt.mm.mysql.Driver");
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())
{
ville_compteur++;
%>
<option value="<%=resultset5.getInt(1)%>" ><%=resultset5.getString(2)%> <%=eval('slc_v'+ville_compteur)%> </option>
<%
}
con5.close();
%>
</select>
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.