1.定义局部变量、输出语句
1 <!doctype html> 2 <html> 3 <head> 4 <title>定义局部变量、输出语句</title> 5 </head> 6 <body> 7 <% 8 int i = 1;//定义局部变量 9 String info = "www.baidu.com"; 10 out.println("<h1>"+i+"</h1>"); //编写语句 11 out.println("<h1>"+info+"</h1>"); 12 %> 13 </body> 14 </html>
2.定义全局变量、方法、类
1 <!doctype html> 2 <html> 3 <head> 4 <title>定义全局变量、方法、类</title> 5 6 </head> 7 <body> 8 <%! 9 public static final String info="www.baidu.com"; 10 %> 11 12 <%! 13 public int add(int i,int j){ 14 return i+j; 15 } 16 %> 17 18 <%! 19 class User{ 20 private String name; 21 private int age; 22 public User(String name,int age){ 23 this.name=name; 24 this.age=age; 25 } 26 public String toString(){ 27 return "name="+name+",aga="+age; 28 } 29 } 30 31 %> 32 33 <% 34 out.println("<h1>"+info+"</h1>"); 35 out.println("<h1>1+3="+add(1,3)+"</h1>"); 36 out.println("<h1>"+new User("liuyang",23)+"</h1>"); 37 %> 38 39 </body> 40 </html>
3.输出语句
1 <!doctype html> 2 <html> 3 <head> 4 <title>定义全局变量、方法、类</title> 5 </head> 6 <body> 7 <% 8 String info = "www.baidu.com"; 9 int i = 1; 10 %> 11 <h1> i = <%=i%> </h1> 12 <h1>info =<%=info%></h1> 13 <h1>name=<%="liuyang"%></h1> 14 </body> 15 </html>
4.注释方法:
<!-- 显示注释 -->
<%-- 隐式注释:JSP 注释 --%>
// 隐式注释:单行注释
/* 隐式注释:多行注释 */
5.JSP 创建表格1 out.println输出
1 <!doctype html> 2 <html> 3 <head> 4 <title>Table_out_print</title> 5 6 </head> 7 <body> 8 <% 9 int rows = 10; 10 int cols = 10; 11 int count = 1; 12 out.println("<table border=‘1‘ width=‘70%‘>"); 13 for(int i=1;i<=rows;i++){ 14 out.println("<tr>"); 15 for(int j=1;j<=cols;j++){ 16 out.println("<td>"+(count)); 17 count++; 18 } 19 out.println("</tr>"); 20 } 21 22 out.println("</table>"); 23 24 %> 25 26 </body> 27 </html>
6.JSP 创建表格2 <%= %>输出
1 <!doctype html> 2 <html> 3 <head> 4 <title>Table_out_jsp</title> 5 </head> 6 <body> 7 <table border=‘1‘ width=‘70%‘>; 8 <% 9 int rows = 10; 10 int cols = 10; 11 int count = 1; 12 for(int i=1;i<=rows;i++){ 13 %> 14 <tr> 15 <% 16 for(int j=1;j<=cols;j++){ 17 %> 18 19 <td> 20 21 <%=(count)%> 22 </td> 23 24 <% 25 count++; 26 } 27 %> 28 29 </tr> 30 <% 31 } 32 %> 33 </table> 34 35 36 37 38 </body> 39 </html>