JSP | 基础 | 两种方式循环输出
用循环连续输出三个你好,字体从小变大
第一种:
1 <body> 2 <%! 3 //通过表达式方式调用实现 4 String HelloJSP1(){ 5 String str = ""; 6 for(int i = 5; i >= 1; i--){ 7 str += "<h" + i+">" +"你好~" +"<"+"/h"+i+">" ; 8 } 9 10 return str; 11 } 12 %> 13 14 <%=HelloJSP1() %> 15 16 </body>
第二种:
1 <body> 2 <%! 3 4 // JSP内置out对象 通过调用脚本方式实现 5 void HelloJSP2(JspWriter out) throws Exception { 6 7 for(int i = 5; i >= 1; i--){ 8 out.println( "<h" + i+">" +"你好~" +"<"+"/h"+i+">" ) ; 9 } 10 } 11 %> 12 13 14 <%HelloJSP2(out); %> 15 </body>
阶段小项目:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = ""; for(int i = 5; i >= 1; i--){ str += "<h" + i+">" +"你好~" +"<"+"/h"+i+">" ; } return str; } // JSP内置out对象 通过调用脚本方式实现 void HelloJSP2(JspWriter out) throws Exception { for(int i = 5; i >= 1; i--){ out.println( "<h" + i+">" +"你好~" +"<"+"/h"+i+">" ) ; } } %> <%=HelloJSP1() %> <hr> <%HelloJSP2(out); %> </body> </html>