Application of MVC design pattern in ASP User Login

Source: Internet
Author: User

MVC is an excellent design mode. In MVC design mode, m represents model, c Represents controller, V represents viewer, and is also a model layer, control layer, and view layer.
Model is the place where business logic is implemented. controller controls forwarding, while viewer displays data.
The following uses user login verification as an example to describe the MVC implementation architecture in detail:
If you want to enter the web system background, you must pass the verification, that is, log on with the user name and password. If the verification succeeds, you can view the information. Otherwise, an error is prompted.
Design Model layer:
The model here is a user. The attributes of this user are only username and password. Therefore, username and password are the attributes of the login user. the following classes can be designed to encapsulate the user model layer.
Rem login to user model layer
Rem Class Name: User Login Attribute Class
Rem Creation Time: 2009-3-5
Dim loginuser
Set loginuser = new loginuser _ 'instantiate an object
Class loginuser _
Private my_username 'private property Username
Private my_password 'private attribute User Password

Private sub class_initialize ()
End sub

Private sub class_terminate ()
End sub
Public property get username' all the following get and set methods
Username = my_username
End Property
Public property let username (byval value)
My_username = Value
End Property
Public property get Password
Password = my_password
End Property
Public property let password (byval value)
My_password = Value
End Property
End Class

Well, the data access layer (DAO) is designed below. The data access layer mainly provides various methods for user login verification. The code I wrote is as follows:
<! -- # Include file = "../common/tool. asp" --> 'anti-SQL Injection class
<! -- # Include file = "../actionform/loginuser. asp" --> 'call model-layer user attributes
<! -- # Include file = "../common/Conn. asp" --> 'database connection class'
<%
Data access layer (data processing) accessed by REM users)
Rem Creation Time: 2009-3-5
Dim loginuserdao
Set loginuserdao = new loginuserdao _
Class loginuserdao _

Private sub class_initialize ()
End sub

Private sub class_terminate ()
End sub

Rem obtains the user's form value
If REM is not empty, the returned result is 1. If it is null, the returned result is 0.
Public Function getuserform ()
Loginuser. Username = checkstring. checkstr (request. Form ("username "))
Loginuser. Password = checkstring. checkstr (request. Form ("password "))
If loginuser. Username <> "" And loginuser. Password <> "" then
Getuserform = 1
Else
Getuserform = 0
End if
End Function

Rem authenticates User Login
Public Function checkuserlogin (username, password)
Oledbsqlhelper. db_conn username, password, "select username, password from [hg_admin_user] Where username = '" & username & "' and Password = '" & password &"'"
If not (oledbsqlhelper. m_rs.eof and oledbsqlhelper. m_rs.bof) then
Session ("username") = trim (oledbsqlhelper. m_rs ("username "))
Response. Redirect "index. asp"
Else
Response. Write ("<SCRIPT> alert ('Sorry, your username and password are incorrect '); history. Go (-1) </SCRIPT> ")
End if
End Function

End Class
%>
Finally, design Bll, that is, the business logic layer.
<! -- # Include file = "../Dao/loginuserdao. asp" --> 'method for referencing the Dal Layer
<%
Rem background User Login control layer (data forwarding)
Rem Creation Time: 2009-3-5
Dim loginuseraction
Set loginuseraction = new loginuseraction _ 'to materialized an object
Class loginuseraction _
Private sub class_initialize ()
End sub

Private sub class_terminate ()
End sub

Public sub checkuserloginaction ()
If loginuserdao. getuserform = 1 then 'is not empty, perform login verification.
Loginuserdao. checkuserlogin loginuser. username, loginuser. password'
End if
End sub
End Class
%>
View Layer Design:
In the view layer, we only need to call the method in bll in this way.
<! -- # Include file = "../hg_admin_manage/Action/loginuseraction. asp" -->
<%
Loginuseraction. checkuserloginaction
%>

The above code is tested and passed in the WINXP + iis5.0 environment. Based on this hierarchical architecture, the Code has a clear hierarchy, strong logic, good robustness and scalability, and is easy to maintain in the future.

Related Article

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.