In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the operation of Cookie in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Cookie operations in Java are a must for every Java Web developer, but I'll introduce you to four tips for Cookie operations in Java.
Test environment: JDK1.5, Tomcat5.5
1. Set Cookie
1) Cookie cookie = new Cookie ("key", "value")
2) cookie.setMaxAge (60); / / set the lifetime of 60 seconds. If it is set to a negative value, it will be the browser process Cookie (saved in memory). If you close the browser, it will become invalid.
3) cookie.setPath ("/ test/test2"); / / set the Cookie path, otherwise the current path (for Servlet, the url-pattern path part of the Servlet configured in request.getContextPath () + web.xml)
4) response.addCookie (cookie)
Second, read Cookie
This method reads all Cookie objects of the current path and the "direct parent path", and returns null if there is no Cookie.
22Cookie [] cookies = request.getCookies ()
3. Delete Cookie
1Cookie cookie = new Cookie ("key", null)
2cookie.setMaxAge (0); / / set to 0 to delete the Cookie immediately
3cookie.setPath ("/ test/test2"); / / Delete the Cookie on the specified path. The path is not set. The current path Cookie is deleted by default.
4response.addCookie (cookie)
Note: assume that the path structure is as follows
/
/ test
/ test/test2
/ test345
/ test555/test666
a. Cookie with the same key name (values can be the same or different) can exist under different paths.
b. When deleting, if there is no Cookie with the key "key" under the current path, all parent paths will be queried and deleted as soon as they are retrieved (only one nearest parent path Cookie can be deleted at a time)
FF. You must specify the same path as you used when setting cookie to delete and change cookie, and the key name of cookie specifies a path regardless of uppercase, lowercase, or mixed case.
IE. If the key name is lowercase, if the current path is / test/test2, if you can't find it, query / test, / test555, / test345, if you can't find it, query /. (/ test555/test666 does not query)
When the key name is mixed case or uppercase, if you do not specify a path, the current path is deleted by default, and no upward query is made.
c. Only the Cookie of the direct parent path can be read when reading Cookie.
If the current path is / test/test2, the key to read is "key". After the current path is read, you have to read / test,/test read, and read /
d. When working on Java's web project, because general Web servers (such as Tomcat or Jetty) use Context to manage different Web Application, there is a different Path for each Context
Be especially careful when there are multiple Web Application in a Server, do not set the Cookie with Path to /, it is easy to misoperate. (of course, if the domain name is the same)
The answer to the question about the operation of Cookie in Java is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.