Javaweb Quick Start Session&cookie Code Ideas Chapter (VII)

Source: Internet
Author: User
Tags set cookie unique id

Introduction to Cookie Practice

Try to share the code N, and finally did not find a good way. I think the code is hard to read. It is better to write ideas, the future code to change into a thought-piece

Session code Exercise 1. Shopping Cart Ideas
1. Listbookservlet Class 1.1 Displays all the items on the site, each item plus a purchase link (Buyservlet) to store the book Object Map.put ("1", New book ("1", "Javaweb development", "Lao Zhang") with the map collection, " Map.put ("2", "New book" ("2", "JDBC Development", "Lao Li", "a Good Book")), Map.put ("3", New Books ("3", "Hibernate", "Lao Fang", "a Good book")); Map.put ("4", New book ("4", "Struts", "Lao Li", "a Good Book")), Map.put ("5", New Books ("5", "Android Development", "Lao Zhang", "a Good Books"));p rivate String Id;private string Name;private string Author;private string description;2. Buyservlet class gets session2.1 according to the book ID gets book 2.2 for purchase of items reserved, multi-browser sharing, manual set cookie validity period 2.3 Gets the collection of all books from the session, if empty, creates the list, Session Set the list,2.3.1 otherwise add the book to the collection in 2.4 Redirect to the shopping Cart (Bookcartservlet), because after forwarding, the URL in the browser still points to the start page, at this time if the current page is overloaded, The start page will be recalled. Note: After Response.encodeurl disables cookies, write directly to the cookie on the label 3. The Bookcartservlet class gets the session, if it is empty, returns the collection, and iterates over it.
2. User Login Ideas
0. Login Landing Page 1. The user class private string Username;private string Password;private string nickname;2. UserDB stores the user class/Determines whether the user matches, matches the return Userlist.add ("AAA", "123", "Week Star"), List.add ("BBB", "123", "Chow Yun Fat"); List.add (The New User ("CCC", "123", "Stephen Chow Chi")); 3. Loginservlet Class 1. Encapsulates the class BEANUTILS2. Take the user input verification code, get the verification code value in session 2.1 Create a session field, use a string buffer to record randomly generated characters, and then set attribute2.2 to take the user input verification code, get the session of the Verification Code value 3. |--if equal, |--re-verify the user name, if empty, please re-enter, jump login page |--Otherwise jump welcome page, set session, redirect to home page |--otherwise re-enter, jump login page 4. IndexServlet1. Gets the session and gets the user object 2 through the session field. If user is not empty, username ... Logoff 3. Otherwise, jump to login page 5. logoutservlet//already exists user session will need to removeattribute re-jump login page
3. Prevent duplicate submissions of forms
Logingenericservlet Class 1. Handling garbled problem 2. Produces a unique ID (UUID.randomUUID.toString ()/base64/biginteger) 3. Login page out.write ("<form action= '" +request.getcontextpath () + "/servlet/tokenservletdemo2 ' method= ' post ' >"); o Ut.write ("Username: <input type= ' text ' name= ' username '  /><br/>"); Out.write ("<input type= ' hidden ' name = ' token ' value= ' "+id+" '/><br/> "); Out.write (" <input type= ' submit ' value= ' login '/><br/> '); o Ut.write ("</form>"); Tokenservlet Anti-submission Class 1. Handling garbled problem 2. Gets the hidden domain ID compared to 3 before generating tokens. If equal, the commit succeeds, removing the Token4. Otherwise, prompt for repeated submissions

Cookie code Idea 1. Record User Login Cases
1. Loginservlet class used as login interface 1.0 to solve the output garbled Problem 1.1 login page, after submission jump to save user cookie information Out.write ("<form method= ' post ' action= '" + Getservletcontext (). Getcontextpath () + "/servlet/cookie02_savauserservlet >"); Out.write ("Username: <input type=" Text ' name= ' username ' value= ' "+username+" '/><br/> '); Out.write ("Password: <input type= ' password ' name= ' Password ' value= ' "+password+" '/><br/> "); Out.write (" Remember user name: <input type= ' checkbox ' name= ' Remember ' value= "+checked+" '/><br/> "); Out.write (" <input type= ' submit ' value= ' login '/><br/> '); Out.write ("</ Form> "); 1.2 Get cookie information, traverse cookies, save usernames, and tick status. First define the variable 2. SaveUserServlet2.1 solve output garbled problem 2.2 Define entity class store user Information 2.3 beanutils in populate set up user information (Getparametermap ()) 2.4 |-- If the obtained user name and password match the specified user name and password, save the cookie|--if the user name is checked in the cookie |--the cookie is deleted. C.setmaxage (0); login successful |--Otherwise login failed, refresh to login interface
2. Show user browsing History
Book class private String Id;private string Name;private string Author;private string Description;db class Store Books Class Map.put ("1", new Book ("1", "Javaweb development", "Lao Zhang", "a good one"), Map.put ("2", New Books ("2", "JDBC Development", "Lao Li", "a Good Book")), Map.put ("3", "New Books" ("3", " Hibernate "," The Old Side "," a Good book "), Map.put (" 4 ", New Books (" 4 "," Struts "," Lao Li "," a Good Book ")), Map.put (" 5 "," New "(" 5 "," Android Development "," Lao Zhang "," a Good Book "); 1. Showallbooks Class 1.1 Show All Books 1.2 show browsed books 1.3 get the values in the cookie, cut IDs, get book 2 by ID. ShowBookInfo2.1 get the book after the user clicks 2.2 Show book details 2.3 build cookiesvalues value 2.3.1 Get the value of the cookie, there is a bookhistory variable 2.3.2 if NULL returns the id2.3.3 directly Otherwise cut the bookhistory turn into LinkedList2.3.4 if there is this ID to remove the 2.3.5 other case if greater than 3 this removes the last 2.3.6 add ID to the front 2.3.7 traverse the collection and then exist in the string buffer 2.3.8 Return the string buffer, return it as a string, and don't forget to delete the last delimiter 2.4 create a cookie and write the cookie back

Javaweb Quick Start Session&cookie Code Ideas Chapter (VII)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.