--------Code Download: Registration and Login template __dao not implemented. zip
Function:
> Registration (with verification code)
> Login
------
Jsp:
* login.jsp Login Form
* regist.jsp----registration form
* index.jsp-to-home (only visible if login is successful)
Servlet:
* Loginservlet
* Registservlet
Service:
* UserService business classes related to users
Dao:
* Userdao and user-related data classes
Domain:
* User (corresponding database, all forms are also needed)
> Username
> Password
> Verifycode
-------------
Steps:
1. Create an empty project
2. Guide Package:
Registered
regist.jsp
> First step: Complete the basic functions of regist.jsp!
Registservlet
> Encapsulates form data, encapsulated in a user object.
> Call service regist () method
* If there is no problem with this method, the output is "registered successfully"
* If this method throws an exception, save the error message to the request domain and forward it to regist.jsp (error message is displayed)
Userservice#regist ()
> did not return a value, but the registration failed to throw a custom exception! You can add exception information to the exception! (Customize an Exception class)
> Verify that the user name has been registered (by User name query user), if it has been registered, throw an exception, the exception information is "user name has been registered!" ”
> Add Users
Userdao: Get results with Business Analytics: two ways to deliver
> Query user objects by user name: User findbyusername (String username)
> Inserting a user into the database: void Add (User user)
Job:
1. Add a userexception to the service layer
2. Dao;
* User Findbyusername (String username)
* Void Add (user user)
3. Service
* void Regist () throws Userexception
4. servlet
1). Encapsulate the form data into the user object
2). Use the user to invoke the service's Regist () method
3). If you get userexception, save the exception information to the request domain and forward it back to regist.jsp (data echo)
4). Output "Registered successfully"
-------------------
Add a verification code to the registration
1. Verifycode class
* BufferedImage getImage ()-Get random captcha pictures
* String GetText () to get the text on the picture
* Static output (Bfferedimage, outputstream) and writes the picture to the specified output stream.
2. Verifycodeservlet
* Get random verification code picture
* Save the text on the CAPTCHA image to the session
* Respond the picture to response's OutputStream
3. regist.jsp
* Add
* Add a text box to enter the verification code
* "Can't see Clearly, change one", is a hyperlink. Put the above src again point to servlet! In order to handle the browser cache, you need to use time to do the parameters!
4. Modify Registservlet
* Check the Verification code!
* ERROR: Save form data to request domain, save error message to request domain, forward back to regist.jsp
* Correct: Do nothing, execute the original code down!
-------------------
Server-side form (input) check
We put this check to get the form data after the verification code check!
1. Use the map type to load the error message!
* Key: Table item name, for example: username, password, verifycode
* Value:
> Non-null: User name cannot be empty, or "password cannot be empty"
> Length: User name length must be between 3~20 the password length must be between 3~20
2. When the checksum fails, add the error message to the map! If that field goes wrong, add the error message to which field!
3. Determine if the map is empty (length is 0), if not empty, indicating that there is an error, save the map to the request domain, save the form to the request domain (ECHO), forward back to regist.jsp
4. On the regist.jsp page, display the error message in the map. ${map.username}
=================================
=================================
=================================
Login function
Page: login.jsp---Login form!
Loginservlet--
1. Get form data encapsulated in user
2. Call the service's login () method and pass the form past!
3. If the login () method of the service does not throw an exception! Returns a User Object!
4. Exception: Get exception information, save to request domain, save form, forward to login.jsp
5. No exception: Save the returned user object to the session!!! Redirect to welcome.jsp (displays current user information!) )
Userservice#login ()
Public user login (user form) {...}
1. Query the database with the user name to get the returned user
> returns null, throws an exception, exception information is (user name does not exist)
> return is NOT NULL, get the query out of the user's password and form the password to compare! If different: throws an exception (password error!) )\
> If same, return query result!
Userdao
1. Query users by user name! (Already there, no more writing!) )
Registration and Login template __dao not implemented