jsp注释

在JSP中支持两种注释的语法操作,一种是显式注释,这种注释是允许客户端看到的,一种是隐式注释,这种注释是客户端无法看到的。

显式注释语法:<!-- 注释内容-->

隐式注释语法:

// 单行

/*   */ 多行

<%-- JSP注释--&>

Scriptlet

jsp中scriptlet比较重要,所有嵌入在HTML代码中的Java程序都必须使用scriptlet标记出来,在JSP中一共有三种scriptlet代码:

第一种:<%%>

第二种:<%! %>

第三种:<%=%>

第一种scriptlet使用<% %>来表示,在此scriptlet中可以定义局部变量,编写语句等,如下:


[html] view plaincopyprint?

  1. <%    

  2.         int x = 20;    

  3.         String info = "lunatictwo";    

  4.         out.println("<h2>x="+x+"</h2>");    

  5.         out.println("<h2>info="+info+"</h2>");    

  6.     %>    



显示结果:



第二种scriptlet使用<%!%>表示,在此scriptlet中可以定义全局变量,方法,类,如下所示:


[html] view plaincopyprint?

  1. <%!  

  2.     public static final String INFO = "lunatictwo";   

  3. %>  

  4. <%!  

  5.     public int add (int x,int y){  

  6.         return x+y;  

  7.     }  

  8. %>  

  9. <%!  

  10.     class Person{  

  11.         private String name;  

  12.         private int age;  

  13.         public Person(String name,int age){  

  14.             this.name = name;  

  15.             this.age = age;  

  16.         }  

  17.         public String toString(){  

  18.             return "name="+this.name+",age="+this.age;  

  19.         }  

  20.     }  

  21. %>  

  22. <%  

  23.     out.println("<h3>INFO="+INFO+"</h3>");  

  24.     out.println("<h3>3+5="+add(3, 5)+"</h3>");  

  25.     out.println("<h3>"+new Person("lunatic",30)+"</h3>");  

  26.  %>  




本程序在<%!%>中定义了全局常量,方法,类,但是因为在<%!%>中不能出现任何其他语句,所以又编写了一个普通的<%%>输出变量,调用方法,输出对象。


第三种:<%=%> 该scriptlet的主要功能是输出一个变量或者具体内容,使用<%=%>的形式完成,有时也把它称为表达式输出。


[html] view plaincopyprint?

  1. <%  

  2.     int temp = 10;  

  3.     String INFO = "lunatictwo";  

  4. %>  

  5. <h3>temp = <%=temp%></h3>  

  6. <h3>String INFO = <%=INFO%></h3>  

  7. <h3>name = <%="优快云 blog" %></h3>  



实际的开发中尽量不要使用out.println();输出,而使用表达式输出。这样可以使html代码和java代码相分离,只输出jsp产生的变量。