文章詳情頁
JSP生成九九乘法表的簡單實例
瀏覽:190日期:2022-06-07 11:20:57
JSP生成九九乘法表的簡單實例
一 用表達式和腳本方式實現九九乘法表
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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%>" rel="external nofollow" > <title>My JSP "exercise.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" rel="external nofollow" > --> </head> <body> <%! //返回九九乘法表對應的HTML代碼,通過表達式來調用,在頁面上顯示 String printMultiTable1() { String s = ""; for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) {s+=i+"*"+j+"="+(i*j)+" "; } s+="<br>"; //追加換行標簽 } return s; } //JSP內置out對象,使用腳本方式調用,打印九九乘法表 void printMultiTable2(JspWriter out) throws Exception { for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) {out.println(i+"*"+j+"="+(i*j)+" "); } out.println("<br>"); //追加換行標簽 } } %> <h1>九九乘法表</h1> <hr> <%=printMultiTable1()%> <br> <% printMultiTable2(out);%> <br> </body></html>
二 運行效果
三 小知識點
1、pageEncoding是jsp文件本身的編碼。
2、contentType的charset是服務器發給客戶端時的內容編碼。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
標簽:
JSP
上一條:JSP 注釋的詳解及簡單實例下一條:JSP中out對象的實例詳解
相關文章:
排行榜