Solved

Passing loop variable to drop down menu

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Adding Computers to AD groups through an SCCM Task Sequence
In this increasingly digital world, security hacks are no longer just a threat, but a reality. As we've witnessed with Target's big identity hack 2013, Heartbleed in 2015, and now Cloudbleed, companies and their leaders need to prepare for the unthi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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