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

本文通过三个JSP页面的交互演示了如何使用Session进行登录验证和信息传递。首先在登录页面输入用户名,然后通过Session保存并传递该信息到后续页面进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在MyEclipse创建三个Session_logon.jsp,Session_getparameter.jsp,Session.jsp文件

[color=red](1)Session_logon.jsp[/color]
<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>

[color=red](2)Session_getparameter.jsp[/color]
<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>

[color=red](3)Session.jsp[/color]
<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:
[color=red]request.getParameter("ID"); [/color]
JSP中用request请求获得 action属性为request所JSP文件的文件本身"ID"所在表单的 value值;<input type="text" name="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()事件可以设定失效事件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值