JSP改造Cookie案例以及Session的快速入門
阿新 • • 發佈:2022-12-12
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的值,重新發送cookieDate 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); } }