JSP第二周作业
1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
<%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> </body> <%
//这就是java注释
Date date = new Date(); int year = date.getYear() + 1900; int month = date.getMonth() + 1; int day = date.getDay(); int hour = date.getHours(); String tips; if (hour >=0 && hour<=12) { tips = "上午好"; }else if (hour > 12 && hour <18) { tips = "下午好"; }else{ tips = "晚上好"; } %> <h3>当前时间是<%=year%>年<%=month%>月星期<%=day%>,<%=hour%>时</h3> <h3><%=tips%>!</h3> </html>
2. 使用声明方法,在页面调用该方法,判断2023是不是闰年
<%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> </body> <%! public static String yearsIf(int year){ String tips; if (year%400 ==0 && year %4 ==0) { tips = year + "是闰年"; }else{ tips = year + "不是闰年"; } return tips; } %> <!--这就是HTML注释--> <h3><%=yearsIf(2023)%></h3> </html>
3.表达式+程序段 循环输出5条hr,长度分别为100px-500px
<hr width="<%=i%>"
<%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h2>Hello World!</h2> </body> <% /* * 这就是jsp注释 * */ for (int i = 100; i <= 500; i = i+100) { %> <hr width="<%=i%>" color="red"> <% } %> </html>