JSP第二次作业

1.p39   实验2 显示当前时间,并输出上午(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>
12 <base href="<%=basePath%>">
13 
14 <title>My JSP 'two.jsp' starting page</title>
15 
16 <meta http-equiv="pragma" content="no-cache">
17 <meta http-equiv="cache-control" content="no-cache">
18 <meta http-equiv="expires" content="0">
19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20 <meta http-equiv="description" content="This is my page">
21 <!--
22     <link rel="stylesheet" type="text/css" href="styles.css">
23     -->
24 
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%>&nbsp;&nbsp;<%=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是不是闰年

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%!public boolean nian(int year) {
 3         if (year % 4 == 0 || year % 100 != 0 && year % 400 == 0) {
 4             return true;
 5         }
 6         return false;
 7     }%>
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10 <head>
11 <title>My JSP 'MyJsp.jsp' starting page</title>
12 </head>
13 <!-- HTML注释 -->
14 <body>
15     <%
16         boolean b = nian(2023);
17         out.print(b);
18     %>
19 </body>
20 
21 </html>

 

 

3.表达式+程序段 循环输出5条hr,长度分别为100px-500px
<hr width="<%=i%>"

 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>

 

posted @ 2022-03-09 17:37  MXT16  阅读(15)  评论(0编辑  收藏  举报