JSP状态管理

一:Http协议的无状态性

    无状态是指,当浏览器发送请求给服务器的时候,服务器响应客户端的请求。但是当同一个浏览器再次发送请求给服务器的时候,服务器并不知道它就是刚才那个浏览器

二:保存用户状态的两大机制

    Session和Cookie

三:什么是Cookie?

    Cookie:是web服务器保存在客户端的一系列文本信息

    Cookie的作用:

        1):对特定对象的追踪

        2):保存用户网页浏览记录与习惯

        3):简化登录

四:如何在JSP页面中如何创建与使用Cookie

    1):创建Cookie对象       

Cookie newCookie=new Cookie(String key,Object value);

    2):写入Cookie对象

response.addCookie(newCookie)

    3):读取Cookie对象

Cookie[] cookies=request.getCookies();

    4):常用方法:

        

方法名 说明
void setMaxAge(int expiry) 设置Cookie的有效期,单位为秒
void setValue(String value) 在cookie创建之后,对Cookie赋值
String getName() 获取Cookie的名称
String getValue() 获取Cookie的值
int getMaxAge() 获取Cookie的有效时间,单位为秒

  登录界面:

        

<html>
<head>
    <title>用户登录界面</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    String username="";
    String password="";
    Cookie[] cookies=request.getCookies();
    if(cookies!=null&&cookies.length>0){
        for(Cookie c:cookies){
            if(c.getName().equals("username")){
                username= URLDecoder.decode(c.getValue(),"utf-8");
            }
            if(c.getName().equals("password")){
                password=URLDecoder.decode(c.getValue(),"utf-8");
            }
        }
    }
%>
    <form name="LoginForm" method="post" action="doLogin.jsp">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username" value="<%=username%>"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="text" name="password" value="<%=password%>"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked">保存状态</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="登录"></td>
            </tr>
        </table>
    </form>
</body>
</html>

  cookie处理界面:  

<%@page import="java.net.*" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>登录成功</h1>
    <hr>
    <hr>
    <%
        request.setCharacterEncoding("utf-8");
        //首先判断用户是否点击了c按钮
        String isCookies[] =request.getParameterValues("isUseCookie");
        if(isCookies!=null&&isCookies.length>0){
            //获取表单中的值
            String username=URLEncoder.encode(request.getParameter("username"),"utf-8");
            //使用ERLEncoder来解决在cookie中无法保存中文字符串问题
            String password=URLEncoder.encode(request.getParameter("password"),"utf-8");
            //创建新的cookie对象
            Cookie usernameCookie=new Cookie("username",username);
            Cookie passwordCookie=new Cookie("password",password);
            //设置cookie的最大生命周期
            usernameCookie.setMaxAge(86400);
            passwordCookie.setMaxAge(86400);
            //写入cookie对象
            response.addCookie(usernameCookie);
            response.addCookie(passwordCookie);
        }
        else{//如果没有勾选记住状态选项,则要把里面的内容清空
            Cookie[] cookies=request.getCookies();
            if(cookies!=null&&cookies.length>0){
                for(Cookie c:cookies){
                    if(c.getName().equals("username")||c.getName().equals("password")){
                        c.setMaxAge(0);//设置cookie失效
                        response.addCookie(c);//重新保存cookie
                    }
                }
            }
        }
    %>
    <a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>

  用户信息展示界面:

<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>用户信息</h1>
    <hr>
    <hr>
    <%
        request.setCharacterEncoding("utf-8");
        String username="";
        String password="";
        Cookie[] cookies=request.getCookies();
        if(cookies!=null&&cookies.length>0){
            for(Cookie c:cookies){
                if(c.getName().equals("username")){
                    username= URLDecoder.decode(c.getValue(),"utf-8");
                }
                if(c.getName().equals("password")){
                    password=URLDecoder.decode(c.getValue(),"utf-8");
                }
            }
        }
    %>
    用户名:<%=username%><br>
    密码:<%=password%><br>
</body>
</html>

五:Cookie与session的比较:

    session和cookie的共同点:

        1):都是用来保存状态的

        2):session和cookie都会过期

    session和cookie的区别:

session cookie
是在服务器端保存用户信息 是在客户端保存用户信息
session中保存的是Object类 cookie保存的是String类型
随会话的结束而将其保存的数据销毁 cookie可以长期的保存在客户端
保存重要的信息 保存不重要的用户信息

    

猜你喜欢

转载自blog.csdn.net/phoenix_tgd/article/details/79804001
今日推荐