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()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%!
 7 //Java注释内容
 8 public String tdate(){
 9 Calendar c=Calendar.getInstance();
10 int hour=c.get(Calendar.HOUR);
11 String t = null;
12 if(hour>=0&&hour<=12){
13 t = "上午好";
14 }else if(hour>=13&&hour<=17){
15 t = "下午好";
16 }else if(hour>=18&&hour<=23){
17 t = "晚上好";
18 }
19 return t;
20 }
21  
22  %>
23 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
24 <html>
25   <head>
26     <base href="<%=basePath%>">
27      
28     <title>My JSP 'index.jsp' starting page</title>
29     <meta http-equiv="pragma" content="no-cache">
30     <meta http-equiv="cache-control" content="no-cache">
31     <meta http-equiv="expires" content="0">   
32     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
33     <meta http-equiv="description" content="This is my page">
34     <!--
35     <link rel="stylesheet" type="text/css" href="styles.css">
36     -->
37   </head>
38    
39   <body>
40     欢迎你<br>
41     <%
42     out.print(tdate());
43     %>
44   </body>
45 </html>

 

2. 使用声明方法,在页面调用该方法,判断2023是不是闰年
闰年:能被4整除但不能被100整除或者能被400整除

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%!
 7 public String tdate(int year){
 8  
 9 String t = null;
10 if(year%4==0&&year%100!=0||year%400==0){
11 t = "今年是闰年";
12 }else{
13 t = "今年不是闰年";
14 }
15 return t;
16 }
17  
18  %>
19 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
20 <html>
21   <head>
22     <base href="<%=basePath%>">
23      
24     <title>My JSP 'index.jsp' starting page</title>
25     <meta http-equiv="pragma" content="no-cache">
26     <meta http-equiv="cache-control" content="no-cache">
27     <meta http-equiv="expires" content="0">   
28     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
29     <meta http-equiv="description" content="This is my page">
30     <!--
31     <link rel="stylesheet" type="text/css" href="styles.css">
32     -->
33   </head>
34    
35   <body>
36   <!-- HTML注释内容 -->
37     欢迎你<br>
38     <%
39     out.print("2022年"+tdate(2022));
40     %>
41   </body>
42 </html>

3.表达式+程序段   循环输出5条hr,长度分别为100px-500px

<hr width="<%=i%>"

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>
 9     <base href="<%=basePath%>">
10      
11     <title>My JSP 'index.jsp' starting page</title>
12     <meta http-equiv="pragma" content="no-cache">
13     <meta http-equiv="cache-control" content="no-cache">
14     <meta http-equiv="expires" content="0">   
15     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
16     <meta http-equiv="description" content="This is my page">
17     <!--
18     <link rel="stylesheet" type="text/css" href="styles.css">
19     -->
20   </head>
21    
22   <body>
23   <%
24 for(int i=1;i<=5;i++){
25 %><hr width="<%=i*100%>px" color="red">
26 <% } %>
27   </body>
28 </html>

posted @ 2022-03-18 13:41  唐一南  阅读(49)  评论(0)    收藏  举报