后端开发--基础概念:Session 与 Cookie 的比较(2)
来源:互联网 发布:java 吧 编辑:程序博客网 时间:2024/06/06 05:22
日期:2017/11/22
下面以代码例子,讲述 Session 和 Cookie 在servlet的使用。
一、Session
例子1:
package srevletTest.Sessions;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ShowSession extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("text/html");PrintWriter out = resp.getWriter();String title = "Session Tracking Example";HttpSession session = req.getSession(true);String heading;// Use getAttribute instead of getValue in version 2.2.Integer accessCount = (Integer) session.getAttribute("accessCount");if(accessCount == null){accessCount = new Integer(0);heading = "welcome Newcomer.";} else{heading="Welcome back.";accessCount = new Integer(accessCount.intValue()+1);}// Use setAttribute instead of putValue in version 2.2.session.setAttribute("accessCount", accessCount);out.println("<html><head><title>Session追踪</title></head>"+ "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">"+ heading + "</H1>\n"+ "<H2>Information on Your Session:</H2>\n"+ "<TABLE BORDER=1 ALIGN=\"CENTER\">\n"+ "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n"+ "<TR>\n" + " <TD>ID\n" + " <TD>" + session.getId() + "\n"+ "<TR>\n" + " <TD>Creation Time\n" + " <TD>"+ new Date(session.getCreationTime()) + "\n" + "<TR>\n"+ " <TD>Time of Last Access\n" + " <TD>"+ new Date(session.getLastAccessedTime()) + "\n" + "<TR>\n"+ " <TD>Number of Previous Accesses\n" + " <TD>"+ accessCount + "\n" + "</TABLE>\n" + "</BODY></HTML>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req,resp);}}
二、Cookie
例子2:
package srevletTest.Cookies;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//设置Cookie//1:服务器可以向客户端写内容//2:只能是文本内容//3:客户端可以阻止服务器写入//4:只能拿自己webapp写入的东西//5:Cookie分为两种,第一种:属于窗口/子窗口(放在内存中的),第二种:属于文本(有生命周期的)//6:一个servlet/jsp设置的cookies能够被同一个路径下面或者子路径下面的servlet/jsp读到 (路径 = URL)(路径 != 真实文件路径)public class SetCookies extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {for(int i=0;i<3;i++){// Default maxAge is -1, indicating cookie applies only to current browsing session.Cookie cookie = new Cookie("Session-Cookie-" + i, "Cookie-Value-S" + i);resp.addCookie(cookie);// Cookie is valid for an hour, regardless of whether user quits browser, reboots computer, or whatever.cookie = new Cookie("Persistent-Cookie-" + i, "Cookie-Value-P" + i);cookie.setMaxAge(3600);resp.addCookie(cookie);}PrintWriter out = resp.getWriter();out.println("<html><head><title>设置Cookie</title></head>"+ "<BODY>\n" + "<H1 ALIGN=\"CENTER\">"+ "设置Cookie" + "</H1>\n"+ "6个Cookie\n"+ "<A HREF=\"ShowCookies\">\n"+ "查看</A>.\n"+ "</BODY></HTML>");}}
例子3:
package srevletTest.Cookies;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//读取客户端的Cookiepublic class ShowCookies extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { PrintWriter out = resp.getWriter(); String title = "Active Cookies"; out.println("<html><head><title>获取客户端Cookie</title></head>" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" + "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Cookie Name\n" + " <TH>Cookie Value"); Cookie[] cookies = req.getCookies(); if(cookies != null){ Cookie cookie; for(int i=0; i< cookies.length;i++){ cookie = cookies[i]; out.println("<TR>\n" + " <TD>" + cookie.getName() + "</TD>\n" + " <TD>" + cookie.getValue() + "</TD></TR>\n" ); } } out.println("</TABLE></BODY></HTML>"); }}
阅读全文
0 0
- 后端开发--基础概念:Session 与 Cookie 的比较(2)
- 后端开发--基础概念:Session 与 Cookie 的比较(1)
- Cookie与Session的比较
- Cookie 与 Session 的比较
- Session与Cookie的比较
- Session与Cookie的比较
- Session与Cookie的比较
- cookie与session的比较
- COOKIE与SESSION比较
- COOKIE与SESSION比较
- COOKIE与SESSION比较
- COOKIE与SESSION比较
- cookie与session比较
- COOKIE与SESSION比较
- session与cookie比较
- 【ASP.NET】session与cookie的比较
- cookie与session的比较,以及application
- PHP基础----PHP 会话管理----22Cookie 和 Session 的概念与区别
- 我的物联网项目(九)久违的死循环
- shell脚本中的export和PWD的作用
- 直播 | 大数据基础设施应用场景和数据隐私保护讲座
- php使用$this变量访问方法和属性
- 独家 | 盘点大数据标准化政策、组织及进展(附PPT下载)
- 后端开发--基础概念:Session 与 Cookie 的比较(2)
- Mysql中LAST_INSERT_ID()的函数使用详解
- PHP的final关键字
- 高德地图定位demo
- 【NoSql】Redis实践篇-简单demo实现(一)
- DP—矩阵链乘法(代码)
- php的抽象类
- 进制转换
- 我的物联网项目(十) 线下之战