In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Java conversation technology Session". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is Session?
Session technology is to save information in the server, and the client needs to receive, record and send back the ID of Session, so Session usually transfers ID to the server with the help of Cookie technology, and the server queries the corresponding records in memory after getting the session id.
A client corresponds to a Session, while a Session has multiple Attribute, and each Attribute has a unique name.
Write code to prove the point made:
Protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {HttpSession session = req.getSession (); PrintWriter writer = resp.getWriter (); / / bind a user object session.setAttribute ("user", new User (1, "kongsam") to session; List users = new ArrayList (); users.add ("kongsam"); users.add ("xiaoming"); users.add ("xiaohong") / bind a list array session.setAttribute ("list", users) to session; / / final printout writer.println ("JSESSIONID =" + session.getId ()); writer.println ("object = > user =" + session.getAttribute ("user"). ToString ()); for (String user: users) {writer.println ("list = > user =" + user);}}
Two different browsers are two different clients that correspond to different JSESSIONID.
How Session works
In real life, when you go to the barber shop to get a haircut, you can choose to apply for a membership card at the front desk, and the front desk staff will store your basic information and subsequent consumption information on the hard disk of the store's computer. when you spend in the future, you can query all your information and consumption records with only one membership card. Note that here you mean the client, and the front desk (store owner) refers to the server.
Understanding Session with Code
SessionDemo01 is used to create a fake database and store the database in Session for safekeeping.
@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {HttpSession session = req.getSession (); / / create a fake database Map vipUsers = new HashMap (); vipUsers.put ("kongsam", new VipUser (1, "kongsam", "123,50)); vipUsers.put (" xiaoming ", new VipUser (2," xiaoming "," 123", 100)) VipUsers.put ("xiaohong", new VipUser (3, "xiaohong", "123,200)); / / storing the data from the fake database in Session session.setAttribute (" vipUsers ", vipUsers);}
SessionDemo02 is then used to access the vipsUsers database in Session, and if the user does not apply or does not exist, register a new VIP for the user.
@ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding ("utf-8"); resp.setCharacterEncoding ("utf-8"); resp.setContentType ("text/html;charset=utf-8"); HttpSession session = req.getSession (); / / get username String username = req.getParameter ("username"); / / fetch database Map maps = (Map) session.getAttribute ("vipUsers") from Session / / determine whether there is a user if (maps.get (username)! = null & & maps.get (username). GetUsername (). Equals (username)) {resp.getWriter (). Println (maps.get (username). GetUsername () + "Hello, your current score is:" + maps.get (username). GetPoints ()) } else {resp.getWriter (). Println ("you haven't applied for a membership card yet. The front desk is working on it for you. Please refresh the page.") ; maps.put (username, new VipUser (1, username, "123,50)); session.setAttribute (" vipUsers ", maps);}}" how to use Java session technology Session ". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.