JSP基础——分别使用表达式和脚本方式打印九九乘法表

最近在学习慕课网的课程《Java遇见HTML——JSP篇》,简单做些记录。

课程网址为:http://www.imooc.com/learn/166

 

任务描述:分别使用表达式和脚本方式打印九九乘法表

代码如下:

 

[java] view plain copy
 
  1. <%@ page language="java" import="java.util.*"  
  2.     contentType="text/html; charset=UTF-8"%>  
  3. <%  
  4.     String path = request.getContextPath();  
  5.     String basePath = request.getScheme() + "://"  
  6.             + request.getServerName() + ":" + request.getServerPort()  
  7.             + path + "/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12. <head>  
  13. <base href="<%=basePath%>">  
  14.   
  15. <title>My JSP 'index.jsp' starting page</title>  
  16. <meta http-equiv="pragma" content="no-cache">  
  17. <meta http-equiv="cache-control" content="no-cache">  
  18. <meta http-equiv="expires" content="0">  
  19. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  20. <meta http-equiv="description" content="This is my page">  
  21. <!--  
  22.     <link rel="stylesheet" type="text/css" href="styles.css">  
  23.     -->  
  24. </head>  
  25.   
  26. <body>  
  27.     <%!  
  28.     //返回九九乘法表对应的HTML代码,通过表达式来调用,在页面上显示  
  29.     String printMultiTable1() {  
  30.         String s = "";  
  31.         for (int i = 0; i <= 9; i++) {  
  32.             for (int j = 1; j <= i; j++) {  
  33.                 s += i + "*" + j + "=" + (i * j) + "    ";  
  34.             }  
  35.             s += "<br>";  
  36.         }  
  37.         return s;  
  38.     }  
  39.       
  40.     //JSP内置out对象,试用脚本方式调用,打印九九乘法表  
  41.     void printMultiTable2(JspWriter out) throws Exception{  
  42.         String s = "";  
  43.         for (int i = 0; i <= 9; i++) {  
  44.             for (int j = 1; j <= i; j++) {  
  45.                 out.println(i + "*" + j + "=" + (i * j) + "    ");  
  46.             }  
  47.             out.println("<br>");  
  48.         }          
  49.     }  
  50.     %>  
  51.     <h1>九九乘法表</h1>  
  52.     <!-- 表达式的方式输出 -->  
  53.     <%=printMultiTable1()%>   
  54.     <!-- 注意脚本方式应该要以分号结束 -->  
  55.     <% printMultiTable2(out);%>     
  56.        
  57.   
  58. </body>  
  59. </html>  

posted on 2017-06-21 11:42  alex5211314  阅读(182)  评论(0)    收藏  举报

导航