JSP第二周作业

1.显示当前时间,并输出上午(0~12)好,下午好(12~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>
<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 x = "";   //用字符串输出内容
        if (hour >= 0 && hour <= 12) {    //用选择语句进行判断
            x = "上午好!";
        } else if (hour >= 13 && hour <= 17) {
            x = "下午好!";
        } else if (hour >= 18 && hour <= 23) {
            x = "晚上好!";
        }
    %>
    <p>
        当前时间为:<%=year%>/<%=month%>/<%=date%>
        <%=hour%>:<%=minute%><%=second%><br>
    </p>
    <p><%=x%></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注释 -->
<%!boolean a(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }%>
</head>
<body>
    <%
        boolean b = a(2023);
        out.print(b);
    %>
</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="5" color="green">
    <%
        }
    %>
</body>
</html>

 

posted @ 2022-03-09 14:40  青鸢°  阅读(35)  评论(0编辑  收藏  举报