第二周JSP作业

1. 显示当前时间,并输出上午(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 'index.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 c = Calendar.getInstance();
        int h = c.get(Calendar.HOUR_OF_DAY);
        int m=c.get(Calendar.MINUTE);
        int s=c.get(Calendar.SECOND);
        if(h>=0&&h<12){
    %>
    上午好!现在时间是:<%=h %>:<%=m %>:<%=s %>
    <%
    }else if(h>=12&&h<18){
    %>
    下午好!现在时间是:<%=h %>:<%=m %>:<%=s %>
    <%
    }else{
     %>
     晚上好!现在时间是:<%=h %>:<%=m %>:<%=s %>
     <%} %>
    
    
</body>
</html>

 

 

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

<%@ 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 'index.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>
  
    <%!int year(int y) {
        int i;
        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
            return i = 1;
        } else {
            return i = 0;
        }
    }%>
    <%
        int y=2023;
        int a=year(y);
        if(a==1){
    %>
    <!-- 字体为红色 -->
    <p style="color: red">2023年是润年!</p>
    <%
    }else{
     %>
     2023年不是润年!
     <%} %>


</body>
</html>

 

 

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

<%@ 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 'index.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>
    <%-- 循环语句 --%>
    <%
        for (int i = 1; i <= 5; i++) {
        int a = i * 100;
    %>
    <hr width="<%=a%>px">
    <%
        }
    %>

</body>
</html>

 

 

4.在第一题上加Java注释,第二题加html注释,第三题加jsp注释

 

 

 

posted @ 2022-03-12 15:46  一土它小木登子  阅读(41)  评论(0编辑  收藏  举报