JSP中判断闰年平年
首先在src的包中添加一个新的类,封装一个实体类Time
public class Time {
private int date;
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public boolean isteap(){
//非满百年及%100!=0且%4=0
//整除四百%400=0
if(date%100!=0&&date%4==0||date%400==0){
return true;
}else{
return false;
}
}
然后创建一个t..jsp文件,在body中打如下代码:
<form action="1.jsp" method="post">
请输入年份:<input type="text" name="txttime">
<input type="submit" value="提交">
</form>
然后再创建一个1.jsp文件在body里面打如下代码:
<%
String y_str=request.getParameter("txttime");
int y=Integer.parseInt(y_str);
Time time=new Time();
time.setDate(y);
boolean leap=time.isteap();
if(leap){
request.setAttribute("result", "闰年");
}else{
request.setAttribute("result", "平年");
}
request.getRequestDispatcher("a.jsp").forward(request, response);
%>
对了,记得在1.jsp文件前面先导入实体Time类:
<%@ page import="com.nf.Time" %>