08-1Cookie

cookie

第1节 Cookie快速入门

总论

cookie概述

cookie入门

package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo1")
public class CookieDemo1 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建Cookie对象
    Cookie c = new Cookie("msg","hello");
    //2.发送Cookie
    response.addCookie(c);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}
package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo2")
public class CookieDemo2 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   //3. 获取Cookie
    Cookie[] cs = request.getCookies();
    //获取数据,遍历Cookies
    if(cs != null){
        for (Cookie c : cs) {
            String name = c.getName();
            String value = c.getValue();
            System.out.println(name+":"+value);
        }
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}

4会话技术原理分析

08-1Cookie_第1张图片

08-1Cookie_第2张图片

08-1Cookie_第3张图片

第2节 Cookie_细节

package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo1")
public class CookieDemo1 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建Cookie对象
    Cookie c = new Cookie("msg","hello");
    //2.发送Cookie
    response.addCookie(c);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}
package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo2")
public class CookieDemo2 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   //3. 获取Cookie
    Cookie[] cs = request.getCookies();
    //获取数据,遍历Cookies
    if(cs != null){
        for (Cookie c : cs) {
            String name = c.getName();
            String value = c.getValue();
            System.out.println(name+":"+value);
        }
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}
package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo3")
public class CookieDemo3 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建Cookie对象
    Cookie c1 = new Cookie("msg","hello");
    Cookie c2 = new Cookie("name","zhangsan");
    //2.发送Cookie
    response.addCookie(c1);
    response.addCookie(c2);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}
package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo4")
public class CookieDemo4 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建Cookie对象
    Cookie c1 = new Cookie("msg","setMaxAge");
    //2.设置cookie的存活时间
    //c1.setMaxAge(30);//将cookie持久化到硬盘,30秒后会自动删除cookie文件
    //c1.setMaxAge(-1);
    //c1.setMaxAge(300);
    c1.setMaxAge(0);//删除Cookie
    //3.发送Cookie
    response.addCookie(c1);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}
package cn.itcast.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;

/**
*

  • Cookie快速入门

*
*/

@WebServlet("/cookieDemo5")
public class CookieDemo5 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //1.创建Cookie对象
    Cookie c1 = new Cookie("msg","你好");
    //设置path,让当前服务器下部署的所有项目共享Cookie信息
    c1.setPath("/");

    //3.发送Cookie
    response.addCookie(c1);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

}

第3节 Cookie案例

因为日期中有空格的原因所以不编码解码的话会报错

08-1Cookie_第4张图片
package cn.itcast.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;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
在服务器中的Servlet判断是否有一个名为lastTime的cookie

  1. 有:不是第一次访问

    1. 响应数据:欢迎回来,您上次访问时间为:2018年6月10日11:50:20
    2. 写回Cookie:lastTime=2018年6月10日11:50:01
  2. 没有:是第一次访问

    1. 响应数据:您好,欢迎您首次访问
    2. 写回Cookie:lastTime=2018年6月10日11:50:01
    

*/

@WebServlet("/cookieTest")
public class CookieTest extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置响应的消息体的数据格式以及编码
    response.setContentType("text/html;charset=utf-8");

    //1.获取所有Cookie
    Cookie[] cookies = request.getCookies();
    boolean flag = false;//没有cookie为lastTime
    //2.遍历cookie数组
    if(cookies != null && cookies.length > 0){
        for (Cookie cookie : cookies) {
            //3.获取cookie的名称
            String name = cookie.getName();
            //4.判断名称是否是:lastTime
            if("lastTime".equals(name)){
                //有该Cookie,不是第一次访问

                flag = true;//有lastTime的cookie

                //设置Cookie的value
                //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
                Date date  = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String str_date = sdf.format(date);
                System.out.println("编码前:"+str_date);
                //URL编码
                str_date = URLEncoder.encode(str_date,"utf-8");
                System.out.println("编码后:"+str_date);
                cookie.setValue(str_date);
                //设置cookie的存活时间
                cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
                response.addCookie(cookie);


                //响应数据
                //获取Cookie的value,时间
                String value = cookie.getValue();
                System.out.println("解码前:"+value);
                //URL解码:
                value = URLDecoder.decode(value,"utf-8");
                System.out.println("解码后:"+value);
                response.getWriter().write("

欢迎回来,您上次访问时间为:"+value+"

"); break; } } } if(cookies == null || cookies.length == 0 || flag == false){ //没有,第一次访问 //设置Cookie的value //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String str_date = sdf.format(date); System.out.println("编码前:"+str_date); //URL编码 str_date = URLEncoder.encode(str_date,"utf-8"); System.out.println("编码后:"+str_date); Cookie cookie = new Cookie("lastTime",str_date); //设置cookie的存活时间 cookie.setMaxAge(60 * 60 * 24 * 30);//一个月 response.addCookie(cookie); response.getWriter().write("

您好,欢迎您首次访问

"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }

}

第4节 JSP改造Cookie案例

jsp概述

1. 概念:
    * Java Server Pages: java服务器端页面
        * 可以理解为:一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码
        * 用于简化书写!!!

jsp原理

2. 原理
    * JSP本质上就是一个Servlet

08-1Cookie_第5张图片
在apache的目录下生成了.class的文件

08-1Cookie_第6张图片

08-1Cookie_第7张图片

08-1Cookie_第8张图片

jsp脚本

3. JSP的脚本:JSP定义Java代码的方式
    1. <%  代码 %>:定义的java代码,在service方法中。service方法中可以定义什么,该脚本中就可以定义什么。

08-1Cookie_第9张图片

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

08-1Cookie_第10张图片

    3. <%= 代码 %>:定义的java代码,会输出到页面上。输出语句中可以定义什么,该脚本中就可以定义什么。

08-1Cookie_第11张图片

08-1Cookie_第12张图片
代码如下:
<%--
Created by IntelliJ IDEA.
User: fqy
Date: 2018/6/8
Time: 14:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

$Title$


  <%
    System.out.println("hello jsp");
    int i = 5;

    String contextPath = request.getContextPath();
    out.print(contextPath);
  %>

  <%!
    int i = 3;
  %>
  <%= "hello" %>


  System.out.println("hello jsp");
  

hi~ jsp!

<% response.getWriter().write("response....."); %>


4. JSP的内置对象:
    * 在jsp页面中不需要获取和创建,可以直接使用的对象
    * jsp一共有9个内置对象。
    * 今天学习3个:
        * request
        * response
        * out:字符输出流对象。可以将数据输出到页面上。和response.getWriter()类似
            * response.getWriter()和out.write()的区别:
                * 在tomcat服务器真正给客户端做出响应之前,会先找response缓冲区数据,再找out缓冲区数据。
                * response.getWriter()数据输出永远在out.write()之前

jsp改造cookie脚本

修改jsp的模板

08-1Cookie_第13张图片
jsp的内容(或者内容修改后)不用重启服务器 直接刷新就生效了
代码如下
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

itcast


<%

//1.获取所有Cookie
Cookie[] cookies = request.getCookies();
boolean flag = false;//没有cookie为lastTime
//2.遍历cookie数组
if(cookies != null && cookies.length > 0){
    for (Cookie cookie : cookies) {
        //3.获取cookie的名称
        String name = cookie.getName();
        //4.判断名称是否是:lastTime
        if("lastTime".equals(name)){
            //有该Cookie,不是第一次访问

            flag = true;//有lastTime的cookie

            //设置Cookie的value
            //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
            Date date  = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str_date = sdf.format(date);
            System.out.println("编码前:"+str_date);
            //URL编码
            str_date = URLEncoder.encode(str_date,"utf-8");
            System.out.println("编码后:"+str_date);
            cookie.setValue(str_date);
            //设置cookie的存活时间
            cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
            response.addCookie(cookie);


            //响应数据
            //获取Cookie的value,时间
            String value = cookie.getValue();
            System.out.println("解码前:"+value);
            //URL解码:
            value = URLDecoder.decode(value,"utf-8");
            System.out.println("解码后:"+value);
            %>
        

欢迎回来,您上次访问时间为:<%=value%>

<%

            break;

        }
    }
}


if(cookies == null || cookies.length == 0 || flag == false){
    //没有,第一次访问

    //设置Cookie的value
    //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
    Date date  = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String str_date = sdf.format(date);
    System.out.println("编码前:"+str_date);
    //URL编码
    str_date = URLEncoder.encode(str_date,"utf-8");
    System.out.println("编码后:"+str_date);

    Cookie cookie = new Cookie("lastTime",str_date);
    //设置cookie的存活时间
    cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
    response.addCookie(cookie);

%>

    

您好,欢迎您首次访问

<%

}

%>


你可能感兴趣的:(java)