JSP页面中<%! %>和<% %>的区别

JSP声明语句:<%!声明语句%>,通常声明全局变量、常量、方法、类
JSP Scriptlet:<%java代码%>,其中可包含局部变量、java语句
JSP表达式:<%=java 代码%>
显示注释:即HTML注释,可以在客户端显示<!–注释部分-->
隐式注释:即JSP注释,不能在客户端显示<%--注释部分--%>

<!-- 显示注释:声明局部变量、java语句 -->
以下是举得例子,帮助你理解

<%

int result = 1;

out.println(NUM + "+" + result +" 结果 " + sum(NUM,result));

%>

<%-- 隐式注释:定义类、方法、全局变量、常量 --%>

<%!

private static final int NUM = 10 ; //常量

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 ;

}

}

public int sum(int num , int result) { // 方法

result+=num;

return result;

}

%>

<html>

<head>

<title>My JSP 'Jsp_01.jsp' starting page</title>

</head>

<body>

<center>

<h2 style="background-color: red">体会显示注释与隐式注释的区别</h2>

<p>第一步:鼠标右击 ;第二步:选择查看源文件;第三步:体会显示注释与隐式注释</p>

<h2 style="background-color: green"; align="center">JSP 脚本元素的使用</h2>

<P>两数字相加为:<%= sum(NUM,result)%></P>

</center>

</body>

</html>

 

你可能感兴趣的:(jsp)