10.4Cookie和Session

一.概念:

10.4Cookie和Session_第1张图片

二.相关方法:

10.4Cookie和Session_第2张图片

10.4Cookie和Session_第3张图片

10.4Cookie和Session_第4张图片

10.4Cookie和Session_第5张图片

10.4Cookie和Session_第6张图片

10.4Cookie和Session_第7张图片

SendCookie:

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("/sendcookie")
public class SendCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        Cookie cookie1 = new Cookie("date", "123");
        Cookie cookie2 = new Cookie("time", "456");
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);
        resp.getWriter().write("成功");
    }
}

GetCookie:

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("/getcookie")
public class GetCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + " : " + cookie.getValue());
        }
        resp.getWriter().write("ok");
    }
}

三.代码练习(登录)

Login.java:

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;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        String uesrname = req.getParameter("uesrname");
        String password = req.getParameter("password");

        // 验证登录,假设只有一个账号:zs 123
        if (uesrname == null || password == null || password.equals("") || uesrname.equals("")) {
            resp.getWriter().write("参数不完整");
            return;
        } else if (!uesrname.equals("zs")) {
            resp.getWriter().write("用户名不存在");
            return;
        } else if (!password.equals("123")) {
            resp.getWriter().write("密码错误");
            return;
        }
        // 登录成功,创建会话,如果会后不存在,自动创建
        HttpSession session = req.getSession(true);
        session.setAttribute("username", uesrname);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String time = simpleDateFormat.format(new Date());
        session.setAttribute("time", time);
        resp.getWriter().write("成功");
        resp.sendRedirect("index");
    }
}

Index.java:

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("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        // 判断会后是否存在,如果不存在,不创建新的会话
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.getWriter().write("请先登录");
            resp.sendRedirect("login");
            return;
        }
        String username = (String) session.getAttribute("username");
        String time = (String) session.getAttribute("time");
        System.out.println(username);
        System.out.println(time);

        resp.getWriter().write("欢迎你," + username + "生产登录时间:" + time);
    }
}

你可能感兴趣的:(servlet,java,maven,java-ee)