I'm sorry that I can't type Chinese because the computer in our company has not been installed with a pinyin input method(The OS language is traditional Chinese) but I'm not authorized to install any software. It's the company's rule.
The question is the difference between "<% %>" and "<%! %>".
I know the code in <%! %> is declaration and no output is allowed in the exclamation point(<%! %>) while any correct java code is allowed in <% %>.
It means only variable and method declaration is allowed in <%! %> and the declaration statement should be complete java statement and it must end with a semicolon.
Example using "<%!":
<%!
public String str="hello";
public void Test()
{
}
%>But we can also declare variable and method in <% %>.
Example using "<%":
<%
public String str="hello";
public void Test()
{
}
%>The question is why we still need a exclamation point(<%!) to write a declaration statement? When do we have to use <%!%> only?Thanks.
The question is the difference between "<% %>" and "<%! %>".
I know the code in <%! %> is declaration and no output is allowed in the exclamation point(<%! %>) while any correct java code is allowed in <% %>.
It means only variable and method declaration is allowed in <%! %> and the declaration statement should be complete java statement and it must end with a semicolon.
Example using "<%!":
<%!
public String str="hello";
public void Test()
{
}
%>But we can also declare variable and method in <% %>.
Example using "<%":
<%
public String str="hello";
public void Test()
{
}
%>The question is why we still need a exclamation point(<%!) to write a declaration statement? When do we have to use <%!%> only?Thanks.
这样的时候,当jsp页面转换成servlet时,会在这个servlet的_jspService()方法中申明一个变量int i = 99;这里的i在方法体里面所以你每次请求jsp页面时,这个变量i都是要重新赋值的<%! int i = 99; %>
这样的时候,当jsp页面转换成servlet时,会在这个servlet的_jspService()方法外申明一个变量int i = 99;也就是我们所说的成员变量(没有全局变量的说法),所以你刷新页面时i的值依然是不变的.用户对同一个servlet的多个请求会产生多个线程,但是如果这个servlet没有实现SingleThreadModel接口,则每个线程都是调用同一个servlet的service方法.
你也可以在Tomcat下的work\Catalina\localhost\项目名 中查看对应jsp转换成servlet的类
<% %> 可以用来声明变量,但也可以用来写程序代码块,比方说out.print(),但是<%!%>就不可以.
I got it.... I finally memorize that, in that case , the method declaration can't be put in <%%> .. method declaration should be put in <%!%> but for the variable, in <%!%> and in <%%> are both ok
<% %> 可以用来声明变量,但也可以用来写程序代码块,比方说out.print(),但是<%!%>就不可以.