MVC模式實現注冊登錄


很多人對MVC模式搞不懂,剛開始是我也犯迷糊,知道看到一個前輩寫的代碼,我頓時有的恍然大悟,拿來分享給各位

MVC:

就是M:模型、V:視圖(前台界面)C:后台處理的servlet

 

 

 

 

 


 

 話不多說、上代碼

bean中代碼(用到的變量)

package bean;

public class Userbean {
    
     private String username;//用戶名
     private String phone;
     private String email;
     private String password;//密碼都是與數據庫匹配的,下面是set和get函數
   
    
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    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;
    }
    

}
View Code

com.Dao中代碼(對數據庫增刪改查操作)

package com.Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.DBUtil.DBUtil;;
public class userDao {
    public int login(String name,String password) {
        Connection conn = DBUtil.getConn();//這里就是從DBUtil類里面得到連接
        Statement sql=null;
        ResultSet rs=null;
        try{
            sql = conn.createStatement();            
        }catch(SQLException e){
            System.out.println(e);
        }
     int flag=0;
     try
     {
         rs = sql.executeQuery("select * from customer where name='"+name+"' and password='"+password+"'");
         if(rs.next())
         {
             if(rs.getString("password").equals(password))
             {
                 flag=1;
             }
         }
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     
     finally
     {
         DBUtil.close(rs, sql, conn);
     }
     return flag;
 }    
    
}
View Code

com.DbUtil中代碼(連接數據庫,返回conn)

package com.DBUtil;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class DBUtil 
    {       static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
              static String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=BookShop";
            static String user1="sa";
            static String pwd = "8000153";
            public static Connection getConn()//獲取連接,返回Connection類型,必須設置為static這樣才能在其他類中使用
            {
                Connection conn=null;
                try
                {
                    Class.forName(driver);//加載驅動
                    conn=DriverManager.getConnection(url,user1,pwd);//連接數據庫
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                return conn;
            }
            public static void close(Statement state,Connection conn)//關閉函數
            {
                if(state!=null)//只有狀態和連接時,先關閉狀態 
                {
                    try
                    {
                        state.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
                if(conn!=null)
                {
                    try
                    {
                        conn.close();
                    }
                    catch(SQLException e)
                    {
                         e.printStackTrace();
                    }
                }
            }
            public static void close(ResultSet rs,Statement state,Connection conn)
            {
                if(rs!=null)//有結果集,狀態和連接時,先關閉結果集,在關閉狀態,在關閉連接
                {
                    try
                    {
                        rs.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
                if(state!=null)
                    
                {
                    try
                    {
                        state.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
                if(conn!=null)
                {
                    try
                    {
                        conn.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                }
            }

        }
    
View Code

servlet中代碼(要配置web.xml文件,處理完以后把值傳給前台界面)

package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.Dao.userDao;
import bean.Userbean;


@WebServlet("/Userservlet")
public class Userservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public Userservlet() {
        super();
      
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //1.
                Userbean usermessage=new Userbean();
                //2.
                HttpSession session=request.getSession();
                //3.
                String username=request.getParameter("username");
                String password=request.getParameter("password");
                //4.
                usermessage.setUsername(username);
                usermessage.setPassword(password);
                 userDao userdao = new userDao();//創建Userdao的實例
                    int flag = userdao.login(username, password);//用來判斷是否登陸成功
                    if(flag==1)
                    {
                        session.setAttribute("username", "username");
                        response.sendRedirect("Showbookservlet");
                    }
                    else
                    {
                        response.getWriter().print("<script language='javascript'>alert('ERROR')</script>"); 
                        response.setHeader("refresh", "1;URL=Enter.html");
                        
                        
                    }    
                    
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        doGet(request, response);
    }

}
View Code

xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>BookShop</display-name>
    <servlet>
        <servlet-name>Userservlet</servlet-name><!--servlet的別名,隨便取  -->
        <!--servlet的包路徑,后面再加上.servlet類名 ,這里的類名必須是包下面的servlet類名,目的是讓找到該servlet的路徑 -->
        <servlet-class>com.servlet.Userservlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Userservlet</servlet-name><!--servlet的別名,和上面保持一致就行  -->
        <!--jsp跳轉到servlet的路徑名,自己取,用來從jsp界面跳轉到servlet的路徑,程序會根據路徑找到servlet的位置  -->
        <url-pattern>/Servlet/Userservlet</url-pattern><!--  -->
    </servlet-mapping>
View Code

前台界面代碼(如果想高端大氣上檔次可以在網上找一些css的樣式)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>登錄</title>
  <!-- Meta tags -->
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="keywords" content=""
     />
  <script>
     addEventListener("load", function () 
             { setTimeout(hideURLbar, 0); }, false); 
     function hideURLbar() { window.scrollTo(0, 1); }
  </script>
  <!-- Meta tags -->
  <!--stylesheets-->
  <link href="css/style.css" rel='stylesheet' type='text/css' media="all">
  <!--//style sheet end here-->
  <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,700" rel="stylesheet">
</head>
<body>
  <div class="mid-class">
     <div class="art-right-w3ls">
        <h2>登      錄      界       面</h2>
        <form action="Userservlet" method="get">
           <div class="main">
              <div class="form-left-to-w3l">
                 <input type="text" name="username" placeholder="Username" >
              </div>
              <div class="form-left-to-w3l ">
                 <input type="password" name="password" placeholder="Password" >
                 <div class="clear"></div>
              </div>
           </div>
           <div class="left-side-forget">
              <input type="checkbox" class="checked">
              <span class="remenber-me">記住我</span>
           </div>
           <div class="right-side-forget">
              <a href="changepassword.jsp" class="for">忘記密碼?</a>
           </div>
           <div class="clear"></div>
           <div class="btnn">
              <button type="submit">登錄</button>
           </div>
        </form>
        <div class="w3layouts_more-buttn">
           <h3>沒有賬號?
              <a href="#content1">注冊
              </a>
           </h3>
        </div>
    
     </div>
     <div class="art-left-w3ls">
        <h1 class="header-w3ls">
           Gaze sign up and login Form
        </h1>
     </div>
  </div>
  
  <div id="content1" class="popup-effect">
           <div class="popup">
              <!--login form-->
              <div class="letter-w3ls">
                 <form action="UserRegisterservlet" method="get">
                    <div class="form-left-to-w3l">
                       <input type="text" name="name" placeholder="Username" >
                    </div>
                    <div class="form-left-to-w3l">
                       <input type="text" name="phone" placeholder="Phone" >
                    </div>
                    <div class="form-left-to-w3l">
                       <input type="email" name="email" placeholder="Email" >
                    </div>
                    <div class="form-left-to-w3l">
                       <input type="password" name="password" placeholder="Password" >
                    </div>
                    <div class="form-left-to-w3l margin-zero">
                       <input type="password" name="password" placeholder="Confirm Password" >
                    </div>
                    <div class="btnn">
                       <button type="submit">Sign Up</button>
                       <br>
                    </div>
                 </form>
                 <div class="clear"></div>
              </div>
              <!--//login form-->
              <a class="close" href="#">&times;</a>
           </div>
        </div>
  <footer class="bottem-wthree-footer">
    
  </footer>
</body>
</html>
View Code

運行界面(輸入用戶名和密碼,通過和數據庫中的文件比對后返回結果。登陸成功則到顯示頁面,失敗則重新返回到這個頁面並彈窗顯示ERROR)

 

 

先寫這些把,后面的功能下一次在寫(●ˇ∀ˇ●)

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM