---
title: "(笔记)JSP中Session的工作原理解析"
author: "danielhjd"
date: "2011-03-25T15:51:03Z"
tags: ["JSP", "Myeclipse", "工作", "浏览器"]
categories: ["编程语言"]
slug: "jsp-session"
draft: false
lastmod: "2011-03-25T15:51:03Z"
id: 975360
url: "https://danielhjd.iteye.com/blog/975360"
---

首先在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] &lt;%request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk");%&gt; &lt;% String valuelogin = request.getParameter("ID"); session.setAttribute("name",valuelogin);%&gt; &lt;%=name%&gt; has been in our second Session JSP page! [checked]() \[color=red]（3）Session.jsp\[/color] &lt;%request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk"); String valueSession =(String) session.getAttribute("name");%&gt; &lt;% if (valueSession == null){%&gt; Sorry, you have not been in yet.. please login! &lt;%} else {%&gt; Hey,&lt;%= valueSession%&gt; are in Session now, you are welcome! &lt;%}%&gt; 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（）事件可以设定失效事件