Solved

Passing loop variable to drop down menu

Posted on 2003-10-31
4
450 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

17 Experts available now in Live!

Get 1:1 Help Now