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产生的变量。
用积极的拼搏迎接雨后的彩虹,相信自己