Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Passing loop variable to drop down menu

Hi,

   I need to pass the loop variable's "i" value to dop down menu. Could anybody plz let me know the correct source code for the following program in JSP.

<select name="day">
 <%
       for(i=0;i<=31;i++){
%>
System.out.println("<option value=' + i + '>" + i + "</option>");
<%          
}
%>
</select>

Moreover in ASP we use Response.Redirect to move to another page. What alternate of it shall b used in JSP? Plz reply immediately .......
0
adnan_rais
Asked:
adnan_rais
  • 2
1 Solution
 
applekannaCommented:
<select name="day">
 <%
       for(i=0;i<=31;i++){
%>
<option value=<%=i%>" <%=i%> "</option> //called JSP expressions

<%          
System.out.println("<option value=' + i + '>" + i + "</option>");

}
%>
</select>

you ca use response.sendRedirect(url)
response.encodeURL
you can also use <jsp:forward> tag too....

HOpe this helps.......
CHeers!
0
 
JNicCommented:
Remember not to use <%System.out.println()%>, - this will print to the console.
If you want the output to appear in the browser, use only <%out.println()%>

Personally I would do like this:

<select name="day">
<%
for (int i=0; i<32; i++){
   out.println("<option value=\""+i+"\">"+i+"</option>";
}
%>
</select>

I find it simpler without all those <%%> flying around ;-)
Regards,

Nic
0
 
adnan_raisAuthor Commented:
Thanx JNic,  n one more thing to ask..... As in ASP we use Month(Now), Day(Now) and Year(Now) to get the current day, month or a year. What would be the alternatives of them used in JSP?
0
 
JNicCommented:
Fx:

<%@page import="java.util.Calendar"%>

<%
Calendar cal = Calendar.getInstance();
int day=cal.get(Calendar.DAY_OF_MONTH);
int month=cal.get(Calendar.MONTH); // note that 0=january, 1=february aso. (really stupid!)
int year=cal.get(Calendar.YEAR);
%>

Today: <%=year+"-"+month+1+"-"+day%> // Just an example print

Regards,

Nic
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now