用jsp判断年份是润年还是平年

第一步创建一个JSP

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
平年
</body>
</html>


第二步根据表单上的action跳转到另一个JSP中    注意我这里的MyYear是封装类 ,我的封装类在后面

<%
String a=request.getParameter("year"); //请求获得网页里的year值
int year=Integer.parseInt(a); //把year值强转为int型

MyYear years=new MyYear(); //实例化MyYear方法
if(years.isLeap(year)==true){ //判断year值是否为润年
response.sendRedirect("a1.jsp"); //是就跳转到a1.jsp中
}else{ //否则
response.sendRedirect("a2.jsp"); //跳转到a2.jsp
}
%>


创建a1.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
闰年
</body>
</html>


创建a2.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
平年
</body>
</html>


封装类

public boolean isLeap(int year){ //创建一个方法
boolean flag; //设置一个布尔值
if(year%4==0 || year%400==0 && year%100!=0){ //判断是否为润年
flag=true; //是flag就设置为正确
}else{ //否则是错误
flag=false;
}
return flag; //返回一个flag
}

猜你喜欢

转载自www.cnblogs.com/skjy/p/9165360.html