JSP第二周作业
1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% Calendar c=Calendar.getInstance(); int year=c.get(Calendar.YEAR);//获取年 int month=c.get(Calendar.MONTH)+1;//获取月 int date=c.get(Calendar.DATE);//获取日 int hour=c.get(Calendar.HOUR_OF_DAY);//获取时 int minute=c.get(Calendar.MINUTE);//获取分 int second=c.get(Calendar.SECOND);//获取秒 String a="";//通过字符串输出内容 if(hour>=0&&hour<=12){//使用条件语句 a="上午好!"; }else if(hour>=13&&hour<=17){ a="中午好!"; }else if(hour>=18&&hour<=23){ a="晚上好!"; } %> <p>现在的时间为:<br></p> <p><%=year%>/<%=month%>/<%=date%><br></p> <p><%=hour%>:<%=minute%>:<%=second%><br></p> <p><%=a%></p> </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> <title>My JSP 'index.jsp' starting page</title> <!-- HTML注释 --> </head> <body> <%!boolean m(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } else { return false; } }%> <% boolean n = m(2023); out.print(n); %> </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> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% for (int i = 1; i <= 5; i++) { %> <%--JSP注释 --%> <hr width="<%=i * 100%>px" size="15" color="red"> <% } %> </body> </html>