`
danielhjd
  • 浏览: 242574 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

(笔记)JSP中Session的工作原理解析

    博客分类:
  • J2EE
阅读更多
首先在MyEclipse创建三个Session_logon.jsp,Session_getparameter.jsp,Session.jsp文件

(1)Session_logon.jsp
<body>
Welcome to our first Session JSP page</br>
<from name = "jsp" method ="post" action="Session_getparameter.jsp">
Login ID: <input type="text" name="ID"></br>
<input type="submit" value ="Submit">
</from>
</body>

(2)Session_getparameter.jsp
<body>
<%request.setCharacterEncoding("gbk");
  response.setCharacterEncoding("gbk");%>
<% String valuelogin = request.getParameter("ID");
    session.setAttribute("name",valuelogin);%>
<%=name%> has been in our second Session JSP page!
<a herf = "Session.jsp"> checked</a>
</body>

(3)Session.jsp
<body>
<%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!
<%}%>
</body>

Key:
request.getParameter("ID");
JSP中用request请求获得 action属性为request所JSP文件的文件本身"ID"所在表单的 value值;<input type="text" name="ID" value="" >

session.setAttribute("name",valuelogin);
JSP中session的setAttribute()的方法将 request的getParameter()方法取回来的值放入到Session中

session.getAttribute("name");
JSP中session的getAttribute方法取回session中的数据。

由这个例子可以看出session的作用域
(1)关闭浏览器session失效
(2)关闭服务器session失效
(3)session中有设定的时间用session的getMaxInactiveInterval()方法可以获取默认 session中的session.timeout()事件可以设定失效事件
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics