Solved

Passing loop variable to drop down menu

Posted on 2003-10-31
4
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Let's recap what we learned from yesterday's Skyport Systems webinar.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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