JSP第二周作业
1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
1 <%@page import="java.text.SimpleDateFormat"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>My JSP 'index.jsp' starting page</title> 8 </head> 9 10 <body> 11 当前时间为: 12 <% 13 Date d = new Date();//获取系统时间 14 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间格式 15 out.print(sdf.format(d)+"</br>");//输出系统时间 16 int h = d.getHours();//得到系统小时数 17 if(h>=0&&h<=12){//判断时间范围并输出相应的问候语 18 out.print("上午好"); 19 }else if(h>=13&&h<=17){ 20 out.print("下午好"); 21 }else{ 22 out.print("晚上好"); 23 } 24 %> 25 </body> 26 </html>
2. 使用声明方法,在页面调用该方法,判断2023是不是闰年
1 -<%@page import="java.text.SimpleDateFormat"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <!-- 判断是否是闰年 --> 4 <%! 5 boolean isRun(int year){ 6 if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){ 7 return true; 8 }else{ 9 return false; 10 } 11 } 12 %> 13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 14 <html> 15 <head> 16 <title>My JSP 'index.jsp' starting page</title> 17 </head> 18 19 <body> 20 2023 21 <!-- 调用isRun()方法判断 --> 22 <% 23 if(isRun(2023)){ 24 out.print("是闰年"); 25 }else{ 26 out.print("不是闰年"); 27 } 28 %> 29 30 </body> 31 </html>
3.表达式+程序段 循环输出5条hr,长度分别为100px-500px
<hr width="<%=i%>"
1 <%@page import="java.text.SimpleDateFormat"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <%-- 设置一个全局变量 --%> 4 <%! int i = 0; %> 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 <head> 8 <title>My JSP 'index.jsp' starting page</title> 9 </head> 10 11 <body> 12 <%-- 循环五次 --%> 13 <% 14 for(i = 100; i <= 500; i += 100){ 15 %> 16 <%-- 设置每次线宽值为i --%> 17 <hr width="<%=i%>px"/> 18 <%} %> 19 </body> 20 </html>