java httpsession 类型_JavaWeb——HttpSession常用方法示例

这篇博客探讨了如何利用HttpSession接口的方法,如getId()、getCreationTime()和getLastAccessedTime()来管理用户的登录状态。通过setMaxInactiveInterval()设置会话超时,isNew()检查会话是否新建,以及getAttribute()和setAttribute()处理用户信息。在login.jsp中获取用户名,hello.jsp中设置用户名,而logout.jsp中通过调用invalidate()注销用户。内容涉及到Web开发中的会话管理策略。

HttpSession接口中方法

getId()

getCreationTime()

getLastAccessedTime()

setMaxInactiveInterval()

getMaxInactiveInterval()

isNew():如果客户端请求消息中返回了一个与Servlet程序当前获得的HttpSession对象的会话标识号相同,则认为这个HttpSession对象不是新建的

invalidate()

getServletContext()

setAttribute()

getAttribute()

removeAttribute()

getAttributeNames()

b8f423d256d86626fd0bcae15a88c303.png

e8875c649ffa0fee5daed0c4063d98f8.png

28546dccbb9c9a13a1c8aed0f0f95f77.png

login.jsp

Created by IntelliJ IDEA.

User: dell

Date: 2019/7/10

Time: 15:52

To change this template use File | Settings | File Templates.

--%>

Title

SessionID:

IsNew:

MaxInactive:

CreateTime:

LastAssessTime:

Object username = session.getAttribute("username");

if (username ==null){

username ="";

}

%>

username:

hello.jsp

Created by IntelliJ IDEA.

User: dell

Date: 2019/7/10

Time: 15:56

To change this template use File | Settings | File Templates.

--%>

Title

SessionID:

IsNew:

MaxInactive:

CreateTime:

LastAssessTime:

Hello:

session.setAttribute("username",request.getParameter("username"));

%>

重新登录

注销

logout.jsp

Created by IntelliJ IDEA.

User: dell

Date: 2019/7/10

Time: 16:07

To change this template use File | Settings | File Templates.

--%>

Title

SessionID:

IsNew:

MaxInactive:

CreateTime:

LastAssessTime:

BYE:

重新登录

session.invalidate();

%>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值