JSP改造Cookie案例以及Session的快速入门
<%@ 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" %> <html> <head> <title>itcast</title> </head> <body> <% //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 format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//更改为中国格式的时间 String str_date = format.format(date); //URL编码 System.out.println("编码前:"+str_date); 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 //响应数据 //获取cookie的value,时间 String value = cookie.getValue(); System.out.println("解码前:"+value); //URL解码 value = URLDecoder.decode(value, "utf-8"); System.out.println("解码后:"+value); %> <h1>欢迎回来,您上次访问时间为:<%=value%>></h1> <% //找到直接停止 break; } } } if (cookies == null || cookies.length == 0 || flag == false ){ //没有,第一次访问 //设置Cookie的value //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//更改为中国格式的时间 String str_date = format.format(date); //URL编码 System.out.println("编码前:"+str_date); 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);//发送cookie %> <h1>您好,欢迎您首次访问</h1> <% } %> </body> </html>
Session的快速入门
概念:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中,HttpSession
快速入门:
1.获取Session对象
HttpSession session = request.getSession();
2.使用HttpSession对象:
Object getAttribute(String name)
void setAttribute(String name,Object value)
void removeAttribute(String name)
session存储数据:
@WebServlet(name = "SessionDemo1", value = "/SessionDemo1") public class SessionDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //使用Session共享数据 //1.获取session HttpSession session = request.getSession(); //2.存储数据 session.setAttribute("msg", "hello session"); } }
session获取数据
@WebServlet(name = "SessionDemo2", value = "/SessionDemo2") public class SessionDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //使用Session共享数据 //1.获取session HttpSession session = request.getSession(); //2.获取数据 Object msg = session.getAttribute("msg"); System.out.println(msg); } }
标签:cookie,name,Session,request,value,session,Cookie,date,JSP From: https://www.cnblogs.com/qihaokuan/p/16974300.html