Model1实现用户登录

一 jsp页面
1、login.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                         < form  action = "dologin.jsp"  method = "post" >
                         < p  class = "main" >
                                 < label > 用户名:  </ label >
                                 < input  name = "username"  value = ""  />
                                 < label > 密码:  </ label >
                                 < input  type = "password"  name = "password"  value = "" >        
                         </ p >
                         < p  class = "space" >
                                 < input  type = "submit"  value = "登录"  class = "login"  style =" cursor :  pointer ;" />
                         </ p >
                         </ form >
                 </ div >
         </ div >
         </ body >
</ html >
2、dologin.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
 
<%
  request.setCharacterEncoding( "utf-8" );
%>
< jsp:useBean  id = "loginUser"  class = "com.po.Users"  scope = "page" />
< jsp:useBean  id = "userDAO"  class = "com.dao.UsersDAO"  scope = "page" />
< jsp:setProperty  property = "*"  name = "loginUser" />
 
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
 
   //如果用户和密码都等于admin,则登录成功
   if (userDAO.usersLogin(loginUser))
  {
     session.setAttribute( "loginUser" , loginUser.getUsername());
     request.getRequestDispatcher( "login_success.jsp" ).forward(request, response);
    
  }
   else
  {
     response.sendRedirect( "login_failure.jsp" );
  }
%>
3、login_failure.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                     登录失败!请检查用户或者密码! < br >
                   < a  href = "login.jsp" > 返回登录 </ a >   
                 </ div >
         </ div >
         </ body >
</ html >
4、login_success.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                   <%
                     String loginUser =  "" ;
                      if (session.getAttribute( "loginUser" )!= null )
                     {
                        loginUser = session.getAttribute( "loginUser" ).toString();
                     }
                   %>
                     欢迎您 < font  color = "red" > <%= loginUser %> </ font > ,登录成功!
                 </ div >
         </ div >
         </ body >
</ html >
 
二 Javabean
1、Users.java
package  com.po;
//用户类
public  class  Users {
 
         private  String  username ;
         private  String  password ;
        
         public  Users()
        {
                
        }
 
         public  String getUsername() {
                 return  username ;
        }
 
         public  void  setUsername(String username) {
                 this . username  = username;
        }
 
         public  String getPassword() {
                 return  password ;
        }
 
         public  void  setPassword(String password) {
                 this . password  = password;
        }
        
        
}
2、UsersDAO.java
package  com.dao;
 
import  com.po.Users;
 
//用户的业务逻辑类
public  class  UsersDAO {
   
         //用户登录方法
         public  boolean  usersLogin(Users u)
        {
                 if ( "admin" .equals(u.getUsername())&& "admin" .equals(u.getPassword()))
                {
                         return  true ;
                }
                 else
                {
                         return  false ;
                }
        }
}
 
三 运行结果
一 jsp页面
1、login.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                         < form  action = "dologin.jsp"  method = "post" >
                         < p  class = "main" >
                                 < label > 用户名:  </ label >
                                 < input  name = "username"  value = ""  />
                                 < label > 密码:  </ label >
                                 < input  type = "password"  name = "password"  value = "" >        
                         </ p >
                         < p  class = "space" >
                                 < input  type = "submit"  value = "登录"  class = "login"  style =" cursor :  pointer ;" />
                         </ p >
                         </ form >
                 </ div >
         </ div >
         </ body >
</ html >
2、dologin.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
 
<%
  request.setCharacterEncoding( "utf-8" );
%>
< jsp:useBean  id = "loginUser"  class = "com.po.Users"  scope = "page" />
< jsp:useBean  id = "userDAO"  class = "com.dao.UsersDAO"  scope = "page" />
< jsp:setProperty  property = "*"  name = "loginUser" />
 
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
 
   //如果用户和密码都等于admin,则登录成功
   if (userDAO.usersLogin(loginUser))
  {
     session.setAttribute( "loginUser" , loginUser.getUsername());
     request.getRequestDispatcher( "login_success.jsp" ).forward(request, response);
    
  }
   else
  {
     response.sendRedirect( "login_failure.jsp" );
  }
%>
3、login_failure.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                     登录失败!请检查用户或者密码! < br >
                   < a  href = "login.jsp" > 返回登录 </ a >   
                 </ div >
         </ div >
         </ body >
</ html >
4、login_success.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
   String path = request.getContextPath();
   String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
< html >
         < head >
                 <!-- Page title -->
                 < title > imooc  - Login </ title >
                 <!-- End of Page title -->
                 <!-- Libraries -->
                 < link  type = "text/css"  href = "css/login.css"  rel = "stylesheet"  />         
                 < link  type = "text/css"  href = "css/smoothness/jquery-ui-1.7.2.custom.html"  rel = "stylesheet"  />         
                 < script  type = "text/javascript"  src = "js/jquery-1.3.2.min.js" ></ script >
                 < script  type = "text/javascript"  src = "js/easyTooltip.js" ></ script >
                 < script  type = "text/javascript"  src = "js/jquery-ui-1.7.2.custom.min.js" ></ script >
                 <!-- End of Libraries -->         
         </ head >
         < body >
         < div  id = "container" >
                 < div  class = "logo" >
                         < a  href = "#" >< img  src = "assets/logo.png"  alt = ""  /></ a >
                 </ div >
                 < div  id = "box" >
                   <%
                     String loginUser =  "" ;
                      if (session.getAttribute( "loginUser" )!= null )
                     {
                        loginUser = session.getAttribute( "loginUser" ).toString();
                     }
                   %>
                     欢迎您 < font  color = "red" > <%= loginUser %> </ font > ,登录成功!
                 </ div >
         </ div >
         </ body >
</ html >
 
二 Javabean
1、Users.java
package  com.po;
//用户类
public  class  Users {
 
         private  String  username ;
         private  String  password ;
        
         public  Users()
        {
                
        }
 
         public  String getUsername() {
                 return  username ;
        }
 
         public  void  setUsername(String username) {
                 this . username  = username;
        }
 
         public  String getPassword() {
                 return  password ;
        }
 
         public  void  setPassword(String password) {
                 this . password  = password;
        }
        
        
}
2、UsersDAO.java
package  com.dao;
 
import  com.po.Users;
 
//用户的业务逻辑类
public  class  UsersDAO {
   
         //用户登录方法
         public  boolean  usersLogin(Users u)
        {
                 if ( "admin" .equals(u.getUsername())&& "admin" .equals(u.getPassword()))
                {
                         return  true ;
                }
                 else
                {
                         return  false ;
                }
        }
}
 
三 运行结果


 

猜你喜欢

转载自cakin24.iteye.com/blog/2395425