后端开发--基础概念:Session 与 Cookie 的比较(2)

网友投稿 652 2022-10-27

后端开发--基础概念:Session 与 Cookie 的比较(2)

后端开发--基础概念:Session 与 Cookie 的比较(2)

日期: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.javax.servlet.javax.servlet.javax.servlet.class ShowSession extends HttpServlet{ @Override protected 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("Session追踪" + "\n" + "

" + heading + "

\n" + "

Information on Your Session:

\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + "
Info TypeValue\n" + "
ID\n" + " " + session.getId() + "\n" + "
Creation Time\n" + " " + new Date(session.getCreationTime()) + "\n" + "
Time of Last Access\n" + " " + new Date(session.getLastAccessedTime()) + "\n" + "
Number of Previous Accesses\n" + " " + accessCount + "\n" + "
\n" + ""); } @Override protected 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.javax.servlet.javax.servlet.javax.servlet.(路径 = URL)(路径 != 真实文件路径)public class SetCookies extends HttpServlet{ @Override protected 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("设置Cookie" + "\n" + "

" + "设置Cookie" + "

\n" + "6个Cookie\n" + "\n" + "查看.\n" + ""); }}

例子3:

package srevletTest.Cookies;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.javax.servlet.javax.servlet.javax.servlet.class ShowCookies extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); String title = "Active Cookies"; out.println("获取客户端Cookie" + "\n" + "

" + title + "

\n" + "\n" + "\n" + " \n" + " \n" + " \n" ); } } out.println("
Cookie Name\n" + " Cookie Value"); Cookie[] cookies = req.getCookies(); if(cookies != null){ Cookie cookie; for(int i=0; i< cookies.length;i++){ cookie = cookies[i]; out.println("
" + cookie.getName() + "" + cookie.getValue() + "
"); } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:sanguo是一个golang手游服务器网络框架
下一篇:基于egg框架的同构(首页直出)实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~