• jsp 会话


    session和cookie的区别:
    1、session是保存在服务器端,cookie是保存在客户端。
    2、session可以保存任何对象,cookie只能保存字符串对象。
    3、session更安全,cookie不安全,不能存储敏感数据,对保存的数据进行加密处理。
    4、session默认过期时间30分钟,cookie默认过期时间关闭浏览器后。
    5、session适合保存客户状态,cookie适合保存持久化的数据状态。

    1.Session对象

    session机制是一种服务器机制,在服务器保存信息,当程序接收到客户端的请求时,服务器首先会检查这个客户端是否已经创建了Session.

    session保存数据:

    session.setAttribute(String key,Object value)

    session中读取数据:

    (Object)session.getAttribute(String key)

    方法                 返回值类型                                            说明                          
    setAttribute(String key,Object value) void 以key-vlaue的形式保存对象值
    getAttribute(String key) Object 通过key获取对象值
    getId() String 获取sessionID
    Invalidate void 设置session对象失效
    setMaxInactiveInterval(int interval) void 设置session的有效期
    removeAttribute(String key) void 移除session中的属性

    注册页面

        if(username.equals("admin")){
                //不允许注册,返回注册页面
                request.setAttribute("mess", "注册失败,请使用其他用户名");
                //response.sendRedirect("login.jsp");//返回注册页面没有任何提示信息(实现页面跳转)
                request.getRequestDispatcher("login.jsp").forward(request, response);//返回注册页面并提示信息
            }else{
                //注册成功
                session.setAttribute("user", username);//session保存数据
                response.sendRedirect("newsDetail.jsp");
            }

    注册成功 后跳转到的页面

            <%     Object o=session.getAttribute("user");
                if(o==null){
                    //显示用户名密码,可在此登陆(java代码和HTML代码结合使用情况)
                %>    
                <label>用户名</label><input type="text" name="uname" /><label>密码</label><input type="text" name="upassword" /><button>登录</button>
                <%
                }else{
                    //显示“欢迎你,xxx”
                    out.print("欢迎你,"+o.toString());
                }
            %>

     session有效期的设置

    if(username.equals("admin")){
                //不允许注册,返回注册页面
                request.setAttribute("mess", "注册失败,请使用其他用户名");
                //response.sendRedirect("login.jsp");//返回注册页面没有任何提示信息(实现页面跳转)
                request.getRequestDispatcher("login.jsp").forward(request, response);//返回注册页面并提示信息
            }else{
                //注册成功
                session.setAttribute("user", username);//session保存数据
                //session.setMaxInactiveInterval(10);//设置有效期为10秒
                response.sendRedirect("newsDetail.jsp");
            }
        <%     Object o=session.getAttribute("user");
                if(o==null){
                    //显示用户名密码,可在此登陆(java代码和HTML代码结合使用情况)
                %>    
                <label>用户名</label><input type="text" name="uname" /><label>密码</label><input type="text" name="upassword" /><button>登录</button>
                <%
                }else{
                    //显示“欢迎你,xxx”
                    out.print("欢迎你,"+o.toString());
                    
                    %>
                    &nbsp;<a href="userLogout.jsp">注销</a>//创建注销链接
                    <% 
                }
            
            %>

    tomcat中wed.xml设置失效(这里10表示10分钟)

       </welcome-file-list>
            <session-config>
                <session-timeout>10</session-timeout>
            </session-config>
    </web-app>

     Cookie:

    Cookie由服务器端 生成,发送给客户端浏览器,浏览器会将其保存成某个目录下的文本文件。

    Cookie的应用

    1.创建Cookie对象

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

    CookieName为Cookie对象的名称(自己定义);

    String key:Cookie的名称

    value:Cookie所包含的值

    2.写入Cookie

    Cookie创建后,需要将其添加到页面中,调用response对象的public void addCookie(Cookie cookie)方法写入。

    response.addCookie(CookieName);

    例子

      //注册成功后
    Cookie cookie=new Cookie("user",username); response.addCookie(cookie);    cookie.setMaxAge(60*60);//设置保存时间单位秒
    response.sendRedirect(
    "userLoginOk.jsp"); }

    3.读取Cookie
    Cookie[] cookies=request.getCookie();

    例子

    <%    
        //读取Cookie页面
        Cookie[] cookies=request.getCookies();
        String user="";
        for(int i=0;i<cookies.length;i++){
            if(cookies[i].getName().equals("user")){
                user=cookies[i].getValue();
            }
        }
    %>
    <label>用户名</label><input type="text" name="uname" value="<%=user%>"/>
    <%

     当在首页点击“注销”按钮后,用户名会自动显示在“用户名”文本框中。

    application

    application对象类似于全家变量,每个Web项目都有application对象,可以在整个Web项目中共享数据.

    setAttribute(String key,Object value) 返回类型void,可以以key-value形式保存对象值

    getAttribute(String key) Object类型,通过key获取对象值

    统计网站访问人数:

    <%
        Object count=application.getAttribute("count");
        if(count==null){
            //未访问过页面,设置页面访问次数为1
            application.setAttribute("count", new Integer(1));
        }else{
            //已经访问过了,次数+1
            Integer i=(Integer)count;
            application.setAttribute("count", i.intValue()+1);
        }
        Integer icount=(Integer)application.getAttribute("count");
        out.print("访问次数"+icount.intValue()+"次");
    %>
  • 相关阅读:
    根据输入的个数,打印每行做多输出3个的for循环
    在启动页面后面再加载一个广告页,可以定制动画等
    frame.size.height无法直接赋值问题
    iOS开发远程推送
    iOS——UIKeyboardWillShowNotification 监听键盘高度变化
    iOS 浅谈本地通知 UILocalNotification
    iOS中assign、copy 、retain等关键字的含义
    GCD
    xocde快速定位崩溃代码
    关于xcode打包app
  • 原文地址:https://www.cnblogs.com/lgxstudy/p/4248476.html
Copyright © 2020-2023  润新知