Solved

Passing loop variable to drop down menu

Posted on 2003-10-31
4
447 Views
Last Modified: 2009-12-16
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
Comment
Question by:adnan_rais
  • 2
4 Comments
 
LVL 3

Expert Comment

by:applekanna
ID: 9658912
<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
 
LVL 1

Expert Comment

by:JNic
ID: 9659932
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
 

Author Comment

by:adnan_rais
ID: 9662378
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
 
LVL 1

Accepted Solution

by:
JNic earned 20 total points
ID: 9662698
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

16 Experts available now in Live!

Get 1:1 Help Now