笔记-JavaWeb学习之旅12

发布时间:2019-06-11 06:00:01发布者:Mr.Zhang阅读(351)

会话技术

Cookie:客户端会话技术,将数据保存到客户端

package com.data.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/CookieDemo1")
public class CookieDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建Cookie对象
        Cookie cookie = new Cookie("msg","hello");
        //发送Cookie
        response.addCookie(cookie);
        //当访问CookieDemo1的Servlet的时候,会创建一个Cookie对象
        //然后把绑定的数据发送给浏览器
    }
}
package com.data.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/CookieDemo2")
public class CookieDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取Cookie
        Cookie[] cs = request.getCookies();
        if( cs != null){
            //遍历cs,拿到每一个cookie对象
            for(Cookie c : cs){
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name+":"+value);
            }
        }

    }
}
输出结果
msg:hello
Idea-a0bb733:23efa255-9ce7-42d9-8d0b-9853348226b8

会话技术原理分析

img

img

JSP

概念:Java Server Pages : java服务器端页面,可以理解为一个特殊的页面,其中既可以定义Html标签,又可以定义java代码

JSP的脚本:JSP定义代码的方式

1.<% 代码 %>: 定义的代码,在service方法中。

2.<%! 代码 %>:定义的java代码,在jsp转换后的java类的成员文字

3.<%+ 代码 %>:定义的java代码,会输出到页面上

会话技术_Session

概念:在服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession

package com.data.session;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/sessionDemo1")
public class SessionDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取HttpSession对象
        HttpSession session = req.getSession();
        //当客户端关闭后,服务器不关闭,两次获取的session不是同一个对象
        //如果需要相同,可以创建Cookie对象,设置id值,因为session的实现依赖于cookie,只要id值不变就是同一个对象
        Cookie cookie = new Cookie("JSESSIONID",session.getId());//JSESSIONID=226E48B3AD9F203B2BCBFCA2E6EA5D71
        cookie.setMaxAge(60*60);//一个小时内关闭客户端都是同一个对象,因为他们的ID值相同
        resp.addCookie(cookie);
        //在服务器端存储数据
        session.setAttribute("msg","hello,Session");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
package com.data.session;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/sessionDemo2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //创建对象
        HttpSession session = req.getSession();
        //通过键获取值
        Object msg = session.getAttribute("msg");
        System.out.println(msg);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

session原理:Session的实现依赖于Cookie的,服务在一次会话范围内,多次获取的Session对象是同一个。

session细节

客户端关闭后,服务器不关闭,两次获取session不是同一个对象,如需相同,可以创建Cookie,设置属性。

客户端不关闭,服务器关闭后,两次获取的session不是同一个对象,

session在服务器被关闭时销毁,在调用invalidata()时也会销毁,默认失效时间是30分钟

session特点

session用于存储异常会话的多次请求数据,存在服务器端,session可以存储任意类型,任意大小的数据

session与cookie的区别:

session存储数据在服务器端,cookie在客户端

session没有数据大小限制,cookie有

session数据安全,cookie相对于不安全

验证码案列 写了4个小时 ,得到的结果500错误,尚没有找到bug,明天再看吧,





本文转自博客园,原文地址:https://www.cnblogs.com/train99999/p/11001233.html