Cookie&Session

Cookie(客户端)

  • Cookie(小甜饼) 是浏览器保存在本地的文本内容
  • Cookie常用于保存登录状态、用户资料等小文本
  • Cookie具有时效性,Cookie内容会伴随请求发送给Tomcat

Servlet/LoginServlet

package Servlet;

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("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("用户登录成功");
        Cookie cookie=new Cookie("user","admin");
        cookie.setMaxAge(60*60*24*7);
        //60s  60分钟  24小时  7天
        resp.addCookie(cookie);
        resp.getWriter().println("login success");
    }
}

Servlet/indexServlet

package Servlet;

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("/index")
public class indexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getCookies()用户获取所有的Cookie
        Cookie[] cs=req.getCookies();
        if(cs==null){
            System.out.println("not login");
            return;
        }
        String user=null;
        for(Cookie c:cs){
            System.out.println(c.getName()+":"+c.getValue());
            if(c.getName().equals("user")){
                user=c.getValue();
                break;
            }
        }
        if(user==null){
            resp.getWriter().println("user not login");
        }else{
            resp.getWriter().println("user:"+user);
        }
    }
}

Cookie的时效性

  • 不设置:即为窗口时效性

Session-用户会话(存在服务器端)

  • Session(用户会话)用于保存与”浏览器窗口”对应的数据

  • Session的数据存储在Tomcat服务器的内存中,具有时效性

  • Session通过浏览器Cookie的SessionId提取用户数据

Servlet/SessionServlet

package Servlet;

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("/Slogin")
public class SessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("登录成功");
        //获取到用户会话Session对象
        HttpSession session=req.getSession();
        String sessionId=session.getId();
        System.out.println(sessionId);
        session.setAttribute("name","张三");
        req.getRequestDispatcher("Sindex").forward(req,resp);
    }
}

Servlet/Sessionindex

package Servlet;

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("/Sindex")
public class Sessionindex extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        String name= (String) session.getAttribute("name");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("这是首页,当前用户:"+name);
    }
}

Session的原理

ServletContext

  • ServletContext(Servlet上下文对象),是Web应用全局对象
  • 一个Web应用只会创建一个ServletContext对象
  • ServletContext随着Web应用启动而自动创建

javaweb     

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!