首先使用JSP聲明聲明一個函數用於獲得九九乘法表的內容html
<%! String printMultiTable() { String s = ""; for (int i = 1; i < 10; i ++) { for (int j = 1; j <= i; j ++) s += " " + i + " * " + j + " = " + (i * j) + " "; s += "<br />"; } return s; } %>
而後在JSP腳本中使用它:java
<%=printMultiTable() %>
完整的代碼:jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%! String printMultiTable() { String s = ""; for (int i = 1; i < 10; i ++) { for (int j = 1; j <= i; j ++) s += " " + i + " * " + j + " = " + (i * j) + " "; s += "<br />"; } return s; } %> 九九乘法表:<br /> <%=printMultiTable() %> </body> </html>
效果:ide