在t1.jsp 中,設置一個表單,可以輸入年份,提交到另外一個action進行計算,
如果算出來是閏年,那么就跳轉到a1.jsp(顯示閏年),如果是平年就跳轉到a2.jsp(顯示平年)。
要求:需要把計算是否閏年的算法,封裝到一個工具類MyYear中isLeap方法中。
t1.jsp中
<form action="action.jsp" method="post">
請輸入年份
<input type="Text" name="Year">
<input type="submit" name="submit">
</form>
工具類MyYear中
private int year;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public boolean isyear(){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}else{
return false;
}
}
action.jsp中
MyYear my=new MyYear();
String year=request.getParameter("Year");
int y=Integer.parseInt(year);
my.setYear(y);
session.setAttribute("year",year);
if(my.isyear()){
response.sendRedirect("a2.jsp");
}else{
response.sendRedirect("a1.jsp");
}
a1.jsp中
${year}是平年
a2.jsp中
${year}是閏年
