cookie

cookie_第1张图片

cookie_第2张图片

 cookie_第3张图片

package com.kuang.servlet;

import javax.servlet.ServletException;
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.io.PrintWriter;
import java.util.Date;

//保存用户上一次访问的时间
public class CookiesDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器,告诉你。你来的时间,把这个时间封装成一个信件,你下次带来,我就知道你来了
        //解决中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
      resp.setHeader("Content-type","text/html;charset=utf-8");
        //获得浏览器输出对象
        PrintWriter out = resp.getWriter();

        //Cookie,服务器端从客户端获取:客户端发送的请求中,可能会有上一次访问留下的Cookie
        Cookie[] cookies = req.getCookies();

        //判断Cookie是否存在
        if (cookies!=null){
            out.write("您上一次访问的时间是:");
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                //获取Cookie的名字
                if (cookie.getName().equals("lastLoginTime")){
                    //获取Cookie的信息
                    //将获得的系统当前日期,转化为时间戳
                    long lastLoginTime = Long.parseLong(cookie.getValue());
                    //通过Date类的构造器将时间戳,转化为具体时间
                    Date date = new Date(lastLoginTime);
                    //然后将Date具体对象,解析为字符串,并发送至服务器网页上
                    out.write(date.toLocaleString());
                }

            }

        }else {
            out.write("这是您第一次访问本站!");
        }

        //服务器给客户端响应一个Cookie
        //其实Cookie就是一个键值对,
        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
       //给cooki设置一个期限
         cookie.setMaxAge(60);
           resp.addCookie(cookie);

    }

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

package com.kuang.servlet;

import javax.servlet.ServletException;
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.io.PrintWriter;
import java.util.Date;

//保存用户上一次访问的时间
public class CookiesDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //服务器给客户端响应一个Cookie
        //其实Cookie就是一个键值对,
        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
       //给cooki设置一个期限
         cookie.setMaxAge(0);
           resp.addCookie(cookie);

    }

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

转码解码问题 

package com.kuang.servlet;

import javax.servlet.ServletException;
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.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

//保存用户上一次访问的时间
public class CookiesDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器,告诉你。你来的时间,把这个时间封装成一个信件,你下次带来,我就知道你来了
        //解决中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
      resp.setHeader("Content-type","text/html;charset=utf-8");
        //获得浏览器输出对象
        PrintWriter out = resp.getWriter();

        //Cookie,服务器端从客户端请求中获取:客户端发送的请求中,可能会有上一次访问留下的Cookie
        Cookie[] cookies = req.getCookies();

        //判断Cookie是否存在
        if (cookies!=null){
            out.write("您上一次访问的时间是:");
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                //获取Cookie的名字
                if (cookie.getName().equals("name")){
                    String value = cookie.getValue();
                    out.write(URLDecoder.decode(value,"utf-8"));
                }

            }

        }else {
            out.write("这是您第一次访问本站!");
        }

        //服务器给客户端响应一个Cookie
        //其实Cookie就是一个键值对,
        Cookie cookie = new Cookie("name", URLEncoder.encode("基基","utf-8"));

           resp.addCookie(cookie);

    }

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

cookie_第4张图片

你可能感兴趣的:(javaweb,servlet,java,服务器)