JSP第一次

1.编写JSP页面,计算1-100之间的所有素数之和

 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 'index.jsp' starting page</title>
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 </head>
24 
25 <body>
26     <%
27         int i, j, sum = 0;
28         for (i = 1; i < 100; i++) {
29             for (j = 2; j < i; j++) {
30                 if (i % j == 0) {
31                     break;
32                 }
33             }
34             if (j == i) {
35                 sum += i;
36             }
37         }
38     %>
39     1-100之间的素数之和为:
40     <%=sum%>
41 </body>
42 </html>

 

 

2.编写JSP页面,计算1-100之间的所有偶数之和

<%@ 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 i, sum = 0;
        for (i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
            }
        }
    %>
    1-100之间的偶数之和为:
    <%=sum%>
</body>
</html>

 

posted @ 2021-03-03 22:31  潘宇航  阅读(40)  评论(0编辑  收藏  举报