后端开发--基础概念: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>");    }}