Java Web筆記:JSP注釋詳解,webjsp

來源:互聯網
上載者:User

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產生的變數。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.