JSP第二周练习
1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)
<%@ 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> <base href="<%=basePath%>"> <title>My JSP 'showTime.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 现在是<% Calendar calendar = Calendar.getInstance(); //获取时间类 java注释 int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) +1; int day = calendar.get(Calendar.DATE); /* int hour2 = calendar.get(Calendar.HOUR); */ int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); String date = year +"年" +month +"月" +day +"日" +hour +"时" +minute +"分" +second +"秒"; out.println(date); %><br><% if(0 <=hour && hour <=12) out.print("早上好!"); else if(13 <=hour && hour <=17) out.print("中午好!"); else out.print("晚上好!"); /* out.print("hour = " + hour); out.print("hour2 = " + hour2); */ %> </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+"/"; %> <%-- 声明方法是 "<%!" 开头 JSP注释 --%> <%! public boolean leap(int year){ boolean leap = false; if((year %4 ==0 && year %100 !=0)||year %400==0) leap = true; return leap; } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'leap2023.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%-- 等待输入 JSP注释 --%> <% if(leap(2023)) out.print("2023年是闰年!"); else out.print("2023年不是闰年!"); %> </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> <base href="<%=basePath%>"> <title>My JSP 'forHr.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- 注意hr中java代码的用法 "=i" HTML注释 --> <% for(int i =100 ;i <=500 ;i +=100){%> <hr width="<%=i%>" align="left"> <%} %> </body> </html>
4.在第一题上加Java注释,第二题加html注释,第三题加jsp注释
<%@ 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> <base href="<%=basePath%>"> <title>My JSP 'annotation.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% // java注释 %> <%-- JSP注释 --%> <!-- HTML注释 --> < % // java注释 % > <br> < %-- JSP注释 --% > <br> < !-- HTML注释 -- > <br> </body> </html>