Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the Application of cookie examples

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "cookie example Application Analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "cookie example Application Analysis" article.

Cookie Learning / * * Servlet in the server determines whether there is a cookie 1 named lastTime. Yes: it's not the first time to visit. Response data: welcome back. Your last visit was at 11:50:20 on June 10, 2018. Write back to Cookie:lastTime= 11:50:01 on June 10, 2018. No: this is my first visit. Response data: Hello, welcome to visit 2. Writing back to Cookie:lastTime= at 11:50:01 on June 10, 2018 to learn about URLEncoder encoding and URLDecoder decoding, you need to use a variable to receive * / @ WebServlet ("/ cookieDemo") public class CookieDemo extends HttpServlet {protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / set the data format of the response message body and encode response.setContentType ("text/html;charset=utf-8"); boolean flag=false Cookie [] cookies = request.getCookies (); if (cookes.length > 0) {for (Cookie cookie: cookies) {String name = cookie.getName (); if ("lastTime" .equals (name)) {/ / visit Date date = new Date () again SimpleDateFormat sdf = new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Value = URLDecoder.decode (value, "utf-8"); response.getWriter (). Write ("come again, the last time you logged in was:" + value+ "); cookie.setValue (str_date); cookie.setMaxAge (60,60,24,30) Response.addCookie (cookie); break;} if (cookies==null | | cookies.length==0 | | flag==false) {/ / first time Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy System.out.println ("before coding:" + str_date); str_date = URLEncoder.encode (str_date, "utf-8"); System.out.println ("after coding:" + str_date "); Cookie cookie = new Cookie (" lastTime ", str_date); cookie.setMaxAge (60,60,24,30); response.addCookie (cookie) Str_date = URLDecoder.decode (str_date, "utf-8"); response.getWriter (). Write ("Hello, welcome to log in for the first time:" + str_date+ ");}} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response) }} the above is about the content of the article "Analysis of the Application of cookie examples". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report