源码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Declaration</title>
</head>
<body>
<%!
//全局变量
int accessCount = 0;
%>
<%
//局部变量
int accessCount2 = 0;
%>
<h2>AccessCount:
<br>Overall Variable:<%= ++accessCount %>
<br>Local Variable:<%= ++accessCount2 %>
</h2>>
</body>
</html>
测试结果:访问同一页面,每刷新一次,accessCount增1,accessCount2不变(每次出现一个新的局部变量)。