jsp第二周作业
1. 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 25 </head> 26 27 <body> 28 <% 29 //Java注释 30 //Calendar 提供了一个类方法 getInstance,以获得此类型的一个通用的对象。 31 //Calendar 的 getInstance 方法返回一个 Calendar 对象。 32 Calendar c = Calendar.getInstance(); 33 int year = c.get(Calendar.YEAR);//获取年 34 int month = c.get(Calendar.MONTH) + 1;//获取月份 35 int date = c.get(Calendar.DATE);//获取天数 36 int hour = c.get(Calendar.HOUR_OF_DAY);//获取小时 37 int minute = c.get(Calendar.MINUTE);//获取分钟 38 int second = c.get(Calendar.SECOND);//获取秒 39 String str = ""; 40 if (hour >= 0 && hour <= 12) { 41 str = "上午好!"; 42 } else if (hour > 12 && hour <= 17) { 43 str = "中午好!"; 44 } else if (hour >= 13 && hour <= 18) { 45 str = "下午好!"; 46 } else if (hour > 18 && hour <= 23) { 47 str = "晚上好!"; 48 } 49 %> 50 <table border="1" align="center"> 51 52 <tr align="center"> 53 <td>现在时间为:<%=year%>-<%=month%>-<%=date%> <%=hour%>:<%=minute%>:<%=second%></td> 54 </tr> 55 <tr align="center"> 56 <td><%=str%></td> 57 </tr> 58 59 </table> 60 61 </body> 62 63 </html>
2. 使用声明方法,在页面调用该方法,判断2023是不是闰年
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <!--HTML注释语句--> <body> <h1>判断闰年</h1> <%! String panduan(int a){ if((a%4==0&&a%100!=0)||a%400==0){ return "闰年"; }else{ return "平年"; } } %> <% int a=2023; out.print("2023为"+panduan(a)); %> </body> </html>
3.表达式+程序段 循环输出5条hr,长度分别为100px-500px
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>My JSP 'index.jsp' starting page</title> 6 </head> 7 8 <body> 9 <%--jsp注释 --%> 10 <% 11 for (int i = 1; i <= 5; i++) { 12 %> 13 <hr width="<%=i * 100%>px" size="<%=i%>" color="blue"> 14 <% 15 } 16 %> 17 <br /> 18 19 20 </body> 21 </html>