添加Cookies和Session的代码
package Cookies; public class AddCookies extends HttpServlet { /** * @author Administrator */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Session HttpSession session = req.getSession(); session.setAttribute("Lckiss", "Session NotPass"); // Cookies Cookie C = new Cookie("Lckiss", "Cookies Pass"); // C.setPath("/Cookies/"); C.setMaxAge(60 * 60 * 24); resp.addCookie(C); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.println("<h3>已添加一个Cookies</h3>"); out.println("<h3>已生成一个Session</h3>"); } }
读取Cookies和Session的代码
package Cookies; public class ReadCookies extends HttpServlet { /** * @author Administrator */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); Cookie MYCookies[] = req.getCookies(); out.println("<h3>以下为Cookies数据</h3>"); out.println(MYCookies.length); for (int i = 0; i < MYCookies.length; i++) { Cookie C = MYCookies[i]; out.println("<p>Cookies Name:" + C.getName() + " "); out.println("| Cookies Value:" + C.getValue() + "</p>"); out.println("-----------"); // out.println("<p>Cookies.getPath:" + C.getPath() + "</p>"); // System.out.println(C.getVersion()); } HttpSession S = req.getSession(); Enumeration<String> Sname = S.getAttributeNames(); String Svalue = S.getAttribute("Lckiss").toString(); out.println("<h3>以下为Session数据</h3>"); out.println("<p>Session Name:" + Sname.nextElement().toString() + " "); out.println("| Session Value:" + Svalue + "</p>"); } }
书上一个记录访问时间和访问次数的代码
package Cookies; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class VisitTimes extends HttpServlet { /** * @author Administrator */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowTime = sdf.format(new Date()); String lastVistTime = ""; int vistedCount = 0; // 获取所有的Cookies Cookie[] cookies = req.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { // 判断是否为记录最近访问时间的Cookies if ("lastVistTime".equals(cookie.getName())) { lastVistTime = cookie.getValue(); } // 判断是否为记录访问次数的Cookies if ("VistedCount".equals(cookie.getName())) { vistedCount = Integer.valueOf(cookie.getValue()); } } System.out.println(vistedCount); // 若曾经访问过,输出上次访问时间 if (!"".equals(lastVistTime)) out.println("<p>您上次访问的时间是:" + lastVistTime + "</p>"); // 输入访问次数 out.println("<p>您是第" + (vistedCount + 1) + "次访问网站</p>"); // 以本次访问时间重建同名新Cookies Cookie lastVistedTimeC = new Cookie("lastVistTime", nowTime); lastVistedTimeC.setMaxAge(60 * 60 * 24); // 以本次访问时间重建同名新Cookies Cookie VistedCountC = new Cookie("VistedCount", String.valueOf(vistedCount + 1)); VistedCountC.setMaxAge(60 * 60 * 24); resp.addCookie(lastVistedTimeC); resp.addCookie(VistedCountC); } } }
我的感觉就是Cookies是Session的一个载体,Cookies没了 Session也肯定没了,百度的结果如下
cookie 和session 的区别:
1、cookie数据存放在客户的浏览器上,session数据放在服务器上。
2、cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗
考虑到安全应当使用session。
3、session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能
考虑到减轻服务器性能方面,应当使用COOKIE。
4、单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。
5、所以个人建议:
将登陆信息等重要信息存放为SESSION
其他信息如果需要保留,可以放在COOKIE中
Anr-C 2016.10.28
本站由以下主机服务商提供服务支持:
0条评论