Session

来源:互联网 发布:浅野忠信知乎 编辑:程序博客网 时间:2024/04/30 09:21

Session的作用就是它在 Web服务器上保持用户的状态信息供在任何时间从任何页访问

Sessionxml文件中的配置项

    <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。