session对象的方法以及使用(补充request作用域以及session共享)

版权声明:@ly https://blog.csdn.net/lytwy123/article/details/84111725

1.session的方法

a.
getId():获取sessionId
boolean isNew():判断是否是新用户(第一次访问)
void invalidate():使session失效(退出登录,注销)

setAttribute()
getAttribute()

void setMaxInactiveInterval(秒):设置最大有效 非活动时间
比如你登录京东,但是你不做任何操作比如你去上厕所,页面停留不动,然后通过该方法可以保存你的登录状态时长,比如,设置30分钟,那么超过30分钟你就需要重新登录。
int getMaxInactiveInterval():
获取最大有效 非活动时间


2.实例测试方法

做一个登录来测试session的setAttribute,与getAttribute方法
以及setMaxInactiveInterval方法:

用户登录后检查用户名密码对应,对应完成后通过session的setAttribute方法获得登录成功的用户名密码,你登录不成功就返回登录页面,登录成功后重定向到欢迎页面判断得到的用户名是否为null,不为null则打印ession保存的用户名,否则跳到登录页面。并且设置登录作用时长为10s,注意设置的是非活动时间,即你不动网页是非活动时间,即你不动网页10ssession中的用户名密码就会消失.
login.jsp->check.jsp->welcome.jsp
login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
        <form action="check.jsp" method = "post">
        用&nbsp;户&nbsp;名:<input type = "text" name = "uname"><br/>
        &nbsp;密&nbsp;&nbsp;码:<input type = "password" name = "pwd"><br/>
        <input type = "reset" value = "重置">
        &nbsp;&nbsp;&nbsp;
        <input type = "submit" value = "登录"><br/>
        </form>
</body>
</html>

check.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
        <%
            request.setCharacterEncoding("UTF-8");
            String name = request.getParameter("uname");
            String password = request.getParameter("pwd");
            if((name.equals("zs")) && (password.equals("12345")))
            {
                session.setAttribute("uname", name);
                session.setAttribute("pwd", password);
        //        response.sendRedirect("success.jsp");
                request.getRequestDispatcher("welcome.jsp").forward(request, response);
            }else
            {
                response.sendRedirect("login.jsp");
            }
        %>
</body>
</html>

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
        欢迎您:
        <%
            String name = (String)session.getAttribute("uname");
            //判断拿到的用户名是否是空,空则跳转回去登录
            if(name!=null){
                out.print(name);
            }else{
                response.sendRedirect("login.jsp");
            }

        %>
</body>
</html>

实现10s不动网页则登录失效
将check改为如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
        <%
            request.setCharacterEncoding("UTF-8");
            String name = request.getParameter("uname");
            String password = request.getParameter("pwd");
            if((name.equals("zs")) && (password.equals("12345")))
            {
                session.setAttribute("uname", name);
                session.setAttribute("pwd", password);
                session.setMaxInactiveInterval(10);
                response.sendRedirect("welcome.jsp");
        //        request.getRequestDispatcher("welcome.jsp").forward(request, response);
            }else
            {
                response.sendRedirect("login.jsp");
            }
        %>
</body>
</html>

你不要动网页在刷新就会让你重新登录


3.request的作用域

当你获得了用户名zs你在刷新页面(不要按刷新按钮或者F5刷新,直接回车刷新)应该是还会有zs的用户名显示但是并不是而是抛出空指针异常

为什么会这样?因为request对象只发送一次请求,你如果刷新就相当于再次发送请求,一定要注意request的作用域。这时候只要将请求转发改为重定向即可。

" class="reference-link">

4.session的共享

就是session保存了你的用户名密码,在同一个浏览器访问同一个页面,只要你登录了,都不需要再次登录了。
如果换一个浏览器则需要重新登录了。
就比如你在火狐浏览器登录京东,然后你在火狐继续打开多个京东页面都是显示登录状态。
这就是session共享于同一浏览器。
当测试该功能时新建多个jsp即可,然后jsp中直接打印用户名。前提把之前设置网页不动10s登录失效代码屏蔽,当你在其他浏览器输入则会获取null用户名,这就是session的共享。

大家可以关注关注,一起学习。

猜你喜欢

转载自blog.csdn.net/lytwy123/article/details/84111725