i don't know how to discuss it clearly but i'll try.. my problem is when my index page loads, the current value of my variable year is null.. so it displays the word null.. ahm, only if i click the submit button that's the time that a value is inserted in that variable.. i have values on my select tag.. from 2008 - 2007.. that's where data should come from..

so when i get parameter from the select tag, once it initially loads, null value is generated.. hope it's a li'l bit clearer.. i'm just a newbie.. that's why..

<html>
<head>
<title></title>
</head>
<body>
<%
java.util.Date today = new java.util.Date();
String s = DateFormat.getDateInstance(DateFormat.LONG).format(today);
out.print(s);
%>
<table border="0" cellspacing="0" cellpadding="5">
<tr>
<td>
<form method = "post" action = "MonthlyTrend.jsp">
<select name = xyear>
<%
String year = request.getParameter("xyear");
for (int i=(1900 + today.getYear()); i >= 2005; i--) {
if(i == today.getYear())
out.println("<option value=" + i + " selected = \"selected\">" + i + "</option>"+i);
else
out.println("<option value=" + i + ">" + i + "</option>"+i);
}
%>
</select>
<input type = "Submit" value = "GO">
</form>
</td>
</tr>
</table>
<br><b><%= year %> Monthly Test MCBJ Trend</b></font><br>
</body>
</html>

Change

String year = request.getParameter("xyear");

to

String year = request.getParameter("xyear");
if (year == null) {
  year = today.getYear();
}

But you also really need to change "today" to be Calendar and use get(Calendar.YEAR) instead of getYear(), as the getYear() method of Date is deprecated for a reason. See the API for Calendar.

Edit: Also, you really should not be using scriptlets in a JSP. What you're doing here can be handled by a simple bean and a couple of standard JSTL tags.

Change the code in red with the following.

java.util.Date today = new java.util.Date();
String s = DateFormat.getDateInstance(DateFormat.LONG).format(today);

Change to: String s = java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG).format(today); Java does not know where to find DateFormat. It is not in the default lang namespace but in java.text namespace.

I have run your code in my editor. I failed when i run it with your code, but worked when i run
it with my code.

This article has been dead for over six months. Start a new discussion instead.