JSP生成九九乘法表的簡單實例
jsp生成九九乘法表的簡單實例
一 用表達式和腳本方式實現(xiàn)九九乘法表
<%@ 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代碼,通過表達式來調(diào)用,在頁面上顯示 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內(nèi)置out對象,使用腳本方式調(diào)用,打印九九乘法表 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是服務(wù)器發(fā)給客戶端時的內(nèi)容編碼。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法