1 创建及获取客户端会话
session.setAttribute(String name,Object obj);
session.getAttribute(String name)
返回值是Object类型。
2 移除指定的绑定对象
session.removeAttribute(String name)
3 销毁session
session.invalidate()
4 会话超时管理
session.setMaxInactiveInterval(10000)
单位为秒
例子:
(1)index.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<form id=
"form1"
name=
"form1"
method=
"post"
action=
"session.jsp"
>
<div align=
"center"
>
<table width=
"23%"
border=
"0"
>
<tr>
<td width=
"36%"
><div align=
"center"
>您的名字是:</div></td>
<td width=
"64%"
>
<
label
>
<div align=
"center"
>
<input type=
"text"
name=
"name"
/>
</div>
</
label
>
</td>
</tr>
<tr>
<td colspan=
"2"
>
<
label
>
<div align=
"center"
>
<input type=
"submit"
name=
"Submit"
value=
"提交"
/>
</div>
</
label
>
</td>
</tr>
</table>
</div>
</form>
|
(2)session.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<%
String
name = request.getParameter(
"name"
);
//获取用户填写的用户名
session.setAttribute(
"name"
,name);
//将用户名保存在session对象中
%>
<div align=
"center"
>
<form id=
"form1"
name=
"form1"
method=
"post"
action=
"result.jsp"
>
<table width=
"28%"
border=
"0"
>
<tr>
<td>您的名字是:</td>
<td><%=name%></td>
</tr>
<tr>
<td>您最喜欢去的地方是:</td>
<td><
label
>
<input type=
"text"
name=
"address"
/>
</
label
></td>
</tr>
<tr>
<td colspan=
"2"
><
label
>
<div align=
"center"
>
<input type=
"submit"
name=
"Submit"
value=
"提交"
/>
</div>
</
label
></td>
</tr>
</table>
</form>
|
(3)result.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<%
String
name = (
String
)session.getAttribute(
"name"
);
//获取保存在session范围内的对象
String
solution = request.getParameter(
"address"
);
//获取用户输入的最想去的地方
%>
<form id=
"form1"
name=
"form1"
method=
"post"
action=
""
>
<table width=
"28%"
border=
"0"
>
<tr>
<td colspan=
"2"
><div align=
"center"
><strong>显示答案</strong></div> </td>
</tr>
<tr>
<td width=
"49%"
><div align=
"left"
>您的名字是:</div></td>
<td width=
"51%"
><
label
>
<div align=
"left"
><%=name%></div> <!-- 将用户输入的用户名在页面中显示 -->
</
label
></td>
</tr>
<tr>
<td><
label
>
<div align=
"left"
>您最喜欢去的地方是:</div>
</
label
></td>
<td><div align=
"left"
><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 -->
</tr>
</table>
</form>
|
本文转自stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1341497
,如需转载请自行联系原作者