Application實例:
applicationScope.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>applicationScope.jsp</title>
</head>
<body>
<%
//設置request範圍的倆個對象
application.setAttribute("name", "request範圍的小明");
application.setAttribute("age", 22);
%>
application數據設置完成!!!application數據設置完成!!!application數據設置完成!!!
</body>
</html>
applicationTarget.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>applicationTarget.jsp</title>
</head>
<body>
application設置數據哪個用戶都可以取到!!!
<%
//取值(取出值爲object對象)
String name = (String) application.getAttribute("name");
int age = (Integer) application.getAttribute("age");
%>
name:<%=name %><br>
age:<%=age %>
</body>
</html>
運行結果:
此時,我們更換別的瀏覽器直接訪問applicationTarget.jsp,能訪問到application設置的數據嗎?
分析application執行過程:
可以訪問到,application設置的數據是對所用用戶都可見!!!
知識拓展:
當我們向服務器發送請求,會包含一些頭文件!(頭文件包含瀏覽器的一些信息)
這裏以我們以request範圍爲例:
需要導入java.util.*包,然後在requestTarget.jsp插入如下代碼,即可獲得請求瀏覽器的信息;
<%
Enumeration enu = request.getHeaderNames();
while(enu.hasMoreElements()){
String headerName = (String)enu.nextElement();
String headerValue = request.getHeader(headerName);
%>
<h4><%=headerName %> <%=headerValue %></h4>
<%
}
%>