Session
Session相當于一個服務器端的保管箱,服務器發送Sessionid給客戶端,當客戶端返回數據時,將會通過sessionid來判斷客戶端的唯一性。
Session常用方法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session</title>
</head>
<body>
session的唯一表示符<%=
session.getId()
%><br />
session的創建時間<%=
new java.util.Date(session.getCreationTime()).toString()
%><br />
session的最后訪問時間<%=
new java.util.Date(session.getLastAccessedTime()).toString()//使用Data對象將時間變為Data類型字符串
%><br />
session的失效時間<%=
session.getMaxInactiveInterval()
%><br />
</body>
</html>
在/WEB-INF/web.xml中 可以設置session失效時間,時間以分鐘為單位,設置session-timeout,更改默認失效時間。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
15
</session-timeout>
</session-config>
</web-app>
PS :Mac下有一個很好用的API查詢軟件Dash
Session會話小實例 登陸退出的會話功能
功能簡介:四個頁面 login.jsp do_login.jsp logout.jsp welcome.jsp.
login收集表單數據 傳給do_login 進行處理,而后驗證通過后,轉入welcome頁面。
logout行為觸發后清空session對象,而后轉入welcome歡迎新用戶注冊登錄。
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="/SessionDemo/do_login.jsp" method="post">
userName:<input type="text" name="userName" />
password:<input type="password" name="password"/>
<input type="submit" value="submit" />
</form>
</body>
</html>
do_login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (userName != null && password != null) {
session.setAttribute("userName", userName);//在session中添加一個鍵值對
response.setHeader("refresh", "2;URL=/SessionDemo/welcome.jsp");//設置刷新 跳轉到welcome頁面
}
%>
welcome.jsp session.isNew()方法判斷是否時新創建的
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>welcome</title>
</head>
<body>
<%
if (session.getAttribute("userName") != null) {%>
歡迎:<%=session.getAttribute("userName")%>
<a href="/SessionDemo/logout.jsp">注銷</a>
<br />
<%}else{%>
請先登陸
<a href="/SessionDemo/login.jsp">登錄</a>
<%}%>
<%if(session.isNew()){ //判斷是否是新創建的%>
歡迎新用戶!
<%}else{%>
歡迎老用戶!
<%}%>
</body>
</html>
logout.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
session.invalidate();//登出時直接干掉整個session對象,清除的非常徹底
response.setHeader("refresh", "2;URL=/SessionDemo/welcome.jsp");//響應到welcome
%>
JSP內置對象 Application
Application代表的是目前的應用程序,存在于服務器的系統內存中,一旦應用啟動就會創建一個Application對象。Application生命周期更長,為用戶使用全局信息提供了方便。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>application</title>
</head>
<body>
服務器信息<%=
application.getServerInfo()
%><br/>
應用名稱<%=
application.getServletContextName()
%><br/>
主機名稱<%=
application.getVirtualServerName()
%><br/>
</body>
</html>
Application對象是如何共享信息的
頁面訪問計數器
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>counter</title>
</head>
<body>
<%
Object obj = application.getAttribute("counter");//獲取鍵值對
if (obj == null) {//看鍵值對是否為空,為空則第一次訪問,不為空 則計數累計
application.setAttribute("counter", new Integer(1));//保存鍵值對到application中
out.println("頁面被訪問了1次");
}else{
int counterValue = Integer.parseInt(obj.toString());//創建計數標記值
//若不為空,則將計數加一輸出
++counterValue;
out.println("該頁面被訪問了" + counterValue + "次<br />");
application.setAttribute("counter",counterValue);//最后將變化后的值保存到application對象中
}
//java中會自動將int類型的值轉化成一個Integer對象 進行一個裝箱操作
%>
</body>
</html>
內置對象Config
在配置文件和servlet中更常用,在jsp中較少直接用內置Config對象。