第二次作业
1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'zuoye002_1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% Calendar y = Calendar.getInstance();//显示当前时间 int year = y.get(Calendar.YEAR); int month = y.get(Calendar.MONTH) + 1; int date = y.get(Calendar.DAY_OF_MONTH); int hour = y.get(Calendar.HOUR_OF_DAY); int minute = y.get(Calendar.MINUTE); int second = y.get(Calendar.SECOND); %> 用户在<%=year%>/<%=month%>/<%=date%><br> <%=hour%>:<%=minute%>:<%=second%>访问了网页. <% if (hour <= 12) { out.print("早上好!"); } else if (hour > 12 && hour <= 17) { out.print("下午好!"); } else if (hour > 17 && hour <= 23) { out.print("晚上好!"); } %> </body> </html>
2. 使用声明方法,在页面调用该方法,判断2023是不是闰年
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'zuoye002_2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%! boolean b(int year){ if(year%4==0&&year%100!=0||year%400==0){ return true; }else{ return false; } } %>
<!-- 显示是否为闰年 -->
<% boolean f=b(2023); out.print(f); %> </body> </html>
3.表达式+程序段 循环输出5条hr,长度分别为100px-500px
<hr width="<%=i%>"
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'zuoye002_3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body>
<%-- 递增横线 --%>
<% for (int i = 1; i <= 5; i++) { %> <hr width="<%=i*100%>px"> <% out.print("<br>"); %> <% } %> </body> </html>