### session setAttribute getAttribute removeAttribute invalidate //设置 session 失效,当用户执行退出登录时,调用该方法 ### Cookie Cookie 时服务端在 HTTP 响应过程中附带传送给浏览器的一个文本文件,一旦浏览器保存了某个 Cookie,那么在以后的每次访问中都会将这个 Cookie 传递,服务端在做响应的时候会再次将 Cookie 传给浏览器,以此类推,如此 Cooki 就可以在客户端和服务端之间来回传递,从而实现数据交互的功能。 Cookie 常用方法 - new Cookie(String name,String value) 创建一个 Cookie 对象,用键值对的形式来存储数据 ```jsp Cookie cookie = new Cookie("age","22"); response.addCookie(cookie); ``` - request.getCookies() 获取 request 中的所有 Cookie - void setMaxAge(int age) 设置 Cookie 的有效期,以秒为单位 - int getMaxAge() 获取 Cookie 的有效期 - void setValue(String value) 修改 Cookie 的 value 值 - String getName() 获取 Cookie 的 name 值 - String getValue() 获取 Cookie 的 value 值 ### Session 和 Cookie 的区别 Session 保存在服务端,Cookie 保存在客户端。 Session 存储的数据类型 Object,Cookie 只能存储 String Session 会随着会话的结束而销毁,Cookie 可长期保存在客户端浏览器中 Session 保存重要信息,Cookie 保存不重要的信息 ### JSP 内置对象作用域 page 作用域:对应的内置对象是 pageContext request 作用域:对应的内置对象是 request session 作用域:对应的内置对象是 session application 作用域:对应的内置对象是 application page 作用域只在当前页面有效,对应 JSP。 request 作用域在同一次请求内有效,对应请求。 session 作用域在同一次会话中有效,对应会话。 application 作用域对应的是整个 Web 应用,只要 Tomcat 不重启,application 就可用,对应 Tomcat。
LoginServlet.java
package com.southwind.controller; 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("/login.do") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); if(username.equals("zhangsan")&&password.equals("123")){ //用户信息保存到session中 HttpSession session = req.getSession(); session.setAttribute("username",username); resp.sendRedirect("index.jsp"); }else{ resp.sendRedirect("login.jsp"); } } }
LogoutServlet.java
package com.southwind.controller; 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("/logout.do") public class LogoutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); session.invalidate(); resp.sendRedirect("index.jsp"); } }
cookie.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 20:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% //创建 Cookie Cookie cookie = new Cookie("age","22"); response.addCookie(cookie); %> </body> </html>
count.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 21:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% Integer num = (Integer) application.getAttribute("num"); if(num == null){ num = 0; } num++; application.setAttribute("num",num); %> 您是当前第<%=num%>位访客。 </body> </html>
getCookie.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 20:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% Cookie[] cookies = request.getCookies(); for (Cookie cookie:cookies){ System.out.println(cookie.getMaxAge()); // System.out.println(cookie.getName()+"-"+cookie.getValue()); } %> </body> </html>
index.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 20:13 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> 欢迎回来!<% String username = (String) session.getAttribute("username"); %> <%=username%> <a href="/logout.do">退出登录</a> </body> </html>
login.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 20:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/login.do" method="post"> <input type="text" name="username"/> <input type="password" name="password"/> <input type="submit" value="登录"/> </form> </body> </html>
pageContext.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 21:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% application.setAttribute("name","zhangsan"); request.getRequestDispatcher("target.jsp").forward(request,response); %> </body> </html>
target.jsp
<%-- Created by IntelliJ IDEA. User: southwind Date: 2019-07-09 Time: 21:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String name = (String)application.getAttribute("name"); %> <%=name%> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> </web-app>