Session 的作用就是它在 Web 服務器上保持用戶的狀態信息供在任何時間從任何頁訪問
Session在xml文件中的配置項
<session-config>
<session-timeout>60</session-timeout>
</session-config>
Tomcat配置文件裏有一個Session的默認配置,在Tomcat下的conf\web.xml文件中
下面舉個例子,簡要說明一下session的使用:
Login.jsp頁面
<%
String command = request.getParameter("command");
if ("login".equals(command)) {
String userId = request.getParameter("userId");
String password = request.getParameter("password");
try {
User user = UserManager.getInstance().login(userId, password);
//將用戶信息設置到session中
session.setAttribute("user_info", user);
//設置session超時,單位是秒,如果配置文件中同樣存在,以這個爲先
//session.setMaxInactiveInterval(60*60);
//重定向到主控頁面
response.sendRedirect(request.getContextPath() + "/main.jsp");
}catch(UserNotFoundException e) {
out.println(e.getMessage());
}catch(PasswordNotCorrentException e) {
out.println(e.getMessage());
}
}
%>
Toolbar.jsp頁面
<%
User user = (User)session.getAttribute("user_info");
%>
<td width="21%">
當前用戶:<%=user.getUserName() %>
</td>
總結:關於Session上面只是簡單的闡述了一下,在Hibernate中也用到了Session,還沒有進行深入的研究,等學到Hibernate中繼續深入學習Session。