(笔记)JSP中Session的工作原理解析
首先在MyEclipse创建三个Session_logon.jsp,Session_getparameter.jsp,Session.jsp文件 [color=red](1)Session_logon.jsp[/color] Welcome to our first Session JSP page
Login ID:
[color=red](2)Session_getparameter.jsp[/color] <%request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk");%> <% String valuelogin = request.getParameter("ID"); session.setAttribute("name",valuelogin);%> <%=name%> has been in our second Session JSP page! checked [color=red](3)Session.jsp[/color] <%request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk"); String valueSession =(String) session.getAttribute("name");%> <% if (valueSession == null){%> Sorry, you have not been in yet.. please login! <%} else {%> Hey,<%= valueSession%> are in Session now, you are welcome! <%}%> Key: [color=red]request.getParameter("ID"); [/color] JSP中用request请求获得 action属性为request所JSP文件的文件本身"ID"所在表单的 value值; [color=red]session.setAttribute("name",valuelogin);[/color] JSP中session的setAttribute()的方法将 request的getParameter()方法取回来的值放入到Session中 [color=red]session.getAttribute("name");[/color] JSP中session的getAttribute方法取回session中的数据。 由这个例子可以看出session的作用域 (1)关闭浏览器session失效 (2)关闭服务器session失效 (3)session中有设定的时间用session的getMaxInactiveInterval()方法可以获取默认 session中的session.timeout()事件可以设定失效事件