Java Web筆記:JSP注釋詳解,webjsp
jsp注釋
在JSP中支援兩種注釋的文法操作,一種是顯式注釋,這種注釋是允許用戶端看到的,一種是隱式注釋,這種注釋是用戶端無法看到的。
顯式注釋文法:<!-- 注釋內容-->
隱式注釋文法:
// 單行
/* */ 多行
<%-- JSP注釋--&>
Scriptlet
jsp中scriptlet比較重要,所有嵌入在HTML代碼中的Java程式都必須使用scriptlet標記出來,在JSP中一共有三種scriptlet代碼:
第一種:<%%>
第二種:<%! %>
第三種:<%=%>
第一種scriptlet使用<% %>來表示,在此scriptlet中可以定義局部變數,編寫語句等,如下:
<% int x = 20; String info = "lunatictwo"; out.println("<h2>x="+x+"</h2>"); out.println("<h2>info="+info+"</h2>"); %>
顯示結果:
第二種scriptlet使用<%!%>表示,在此scriptlet中可以定義全域變數,方法,類,如下所示:
<%!public static final String INFO = "lunatictwo";%><%!public int add (int x,int y){return x+y;}%><%!class Person{private String name;private int age;public Person(String name,int age){this.name = name;this.age = age;}public String toString(){return "name="+this.name+",age="+this.age;}}%><%out.println("<h3>INFO="+INFO+"</h3>");out.println("<h3>3+5="+add(3, 5)+"</h3>");out.println("<h3>"+new Person("lunatic",30)+"</h3>"); %>
本程式在<%!%>中定義了全域常量,方法,類,但是因為在<%!%>中不能出現任何其他語句,所以又編寫了一個普通的<%%>輸出變數,調用方法,輸出對象。
第三種:<%=%> 該scriptlet的主要功能是輸出一個變數或者具體內容,使用<%=%>的形式完成,有時也把它稱為運算式輸出。
<%int temp = 10;String INFO = "lunatictwo";%><h3>temp = <%=temp%></h3><h3>String INFO = <%=INFO%></h3><h3>name = <%="CSDN blog" %></h3>
實際的開發中盡量不要使用out.println();輸出,而使用運算式輸出。這樣可以使html代碼和java代碼相分離,只輸出jsp產生的變數。