求教,如何在JSP取服务器的年份
求教,如何在JSP中取服务器的年份,并赋值到一个下拉框中并以该年份的前两个年份展现在JSP页面上,请问该如何做到呢?
同事说这样就可以取到年份 <%
int year = Calendar.YEAR;
%> 但如何赋值到select标签呢?
------解决方案--------------------<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
<select id="txtYear" style="width:100px;">
<%
Integer year = Integer.parseInt(new java.text.SimpleDateFormat("yyyy").format(new java.util.Date()));
for (int i = 9; i >= 0; i--) {
%>
<option value="<%=year - i %>"><%=year - i %></option>
<%
}
%>
</select>
</body>
</html>
经过本地测试,木有问题
------解决方案--------------------
<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
<select id="txtYear" style="width:100px;">
<%
Integer year = Integer.parseInt(new java.text.SimpleDateFormat("yyyy").format(new java.util.Date()));
for (int i = 9; i >= 0; i--) {
if (i == 0) {
%>
<option value="<%=year - i %>" selected="selected"><%=year - i %></option>
<%
} else {
%>
<option value="<%=year - i %>"><%=year - i %></option>
<%
}
}
%>
</select>
</body>
</html>