程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 架構實例之Demo_JSP_JavaBean,demo_jsp_javabean

架構實例之Demo_JSP_JavaBean,demo_jsp_javabean

編輯:JAVA綜合教程

架構實例之Demo_JSP_JavaBean,demo_jsp_javabean


架構實例之Demo_JSP_JavaBean

1、開發工具和開發環境

     開發工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13

    開發環境:WIN10

2、Demo_JSP_JavaBean實現功能

     用戶登錄、用戶注冊、退出登錄。

3、Demo_JSP_Java_Bean使用技術

     本實例使用了JSP、JavaBean和JDBC來實現用戶登錄、用戶注冊和退出登錄功能。系統架構圖如圖一所示:

 圖一:Demo_JSP_Java_Bean系統架構圖

 

    下面請看圖二(系統中JSP與JavaBean之間的邏輯關系圖):

 

  

圖二:系統中JSP與JavaBean之間的邏輯關系圖

 

4、具體實現

(1)在MyEclipse中新建一個Web project項目,並命名為Demo_JSP_JavaBean;

(2)向Demo_JSP項目中導入mysql-connector-java-5.1.6-bin.jar,這個包是實現Java連接數據庫功能的包(不會導入包的同學,可以百度喲);

   附:mysql-connector-java-5.1.6-bin.jar百度雲下載鏈接:http://pan.baidu.com/s/1i5psdDF 密碼:meyg

(3)在Demo_JSP項目中新建以下JavaBean文件和JSP文件(PS:其中JSP文件代碼此處只貼出部分代碼,其他JSP文件代碼請參考本人上一篇博客哦(鏈接地址:http://www.cnblogs.com/liuzhen1995/p/5700409.html)):

      1)創建Java類DBAccess,包名取為liu,該類實現數據庫登錄連接的功能,具體代碼如下:

 

package liu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBAccess {

    private String drv = "com.mysql.jdbc.Driver";

    private String url = "jdbc:mysql://localhost:3306/library_system";

    private String usr = "root";

    private String pwd = "root";

    private Connection conn = null;

    private Statement stm = null;

    private ResultSet rs = null;

    public boolean createConn() {
        boolean b = false;
        try {
            Class.forName(drv).newInstance();
            conn = DriverManager.getConnection(url, usr, pwd);
            b = true;
        } catch (SQLException e) {
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        return b;
    }

    public boolean update(String sql) {
        boolean b = false;
        try {
            stm = conn.createStatement();
            stm.execute(sql);
            b = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return b;
    }

    public void query(String sql) {
        try {
            stm = conn.createStatement();
            rs = stm.executeQuery(sql);
        } catch (Exception e) {
        }
    }
    
    public boolean next() {
        boolean b = false;
        try {
            if(rs.next())b = true;
        } catch (Exception e) {
        }
        return b;        
    }
    
    public String getValue(String field) {
        String value = null;
        try {
            if(rs!=null)value = rs.getString(field);
        } catch (Exception e) {
        }
        return value;
    }

    public void closeConn() {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
        }
    }

    public void closeStm() {
        try {
            if (stm != null)
                stm.close();
        } catch (SQLException e) {
        }
    }

    public void closeRs() {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
        }
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getDrv() {
        return drv;
    }

    public void setDrv(String drv) {
        this.drv = drv;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public Statement getStm() {
        return stm;
    }

    public void setStm(Statement stm) {
        this.stm = stm;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsr() {
        return usr;
    }

    public void setUsr(String usr) {
        this.usr = usr;
    }
}

      2)在上一步中創建的包下,再創建一個Java類UserBean,該類實現數據庫查詢和寫入功能,具體代碼如下:

package liu;

public class UserBean {

    public boolean valid(String username, String password) {
        boolean isValid = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from userInfo where username='"+username+"' and password='"+password+"'";
            db.query(sql);
            if(db.next()) {
                isValid = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isValid;
    }
    
    public boolean isExist(String username) {
        boolean isExist = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from userInfo where username='"+username+"'";
            db.query(sql);
            if(db.next()) {
                isExist = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isExist;
    }
    
    public void add(String username, String password, String email) {
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "insert into userInfo(username,password,mail) values('"+username+"','"+password+"','"+email+"')";
            db.update(sql);
            db.closeStm();
            db.closeConn();
        }
    }
}

      3)login_action.jsp,接受login.jsp頁面中用戶輸入的用戶名和密碼,通過調用JavaBean實現登錄認證,具體代碼如下:

<%@ page import="liu.UserBean" %>
<%
//get parameters
String username = request.getParameter("username");
String password = request.getParameter("password");

//check null
if (username == null || password == null) {
    response.sendRedirect("login.jsp");
}

//validate
UserBean userBean = new UserBean();
boolean isValid = userBean.valid(username, password);

if (isValid) {
    session.setAttribute("username", username);
    response.sendRedirect("welcome.jsp");
} else {
    response.sendRedirect("login.jsp");
}
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
 
    
    <title>My JSP 'login_action.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    
  </body>
</html>

      4)register_action.jsp,通過調用JavaBean實現注冊,並把數據寫入數據庫,具體代碼如下:

<%@ page import="liu.UserBean" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
//get parameters
String username = request.getParameter("username");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
String email = request.getParameter("email");


//check null
if (username == null || password1 == null || password2 == null || !password1.equals(password2)) {
    response.sendRedirect("register.jsp");
}


//validate
UserBean userBean = new UserBean();
boolean isExist = userBean.isExist(username);
if(!isExist) {
    userBean.add(username, password1, email);
    response.sendRedirect("login.jsp");
} else {
    response.sendRedirect("register.jsp");
}


%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'register_action.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    
  </body>
</html>

5、運行結果展示

      具體運行結果請參考本人上一篇博客哦,運行結果是一模一樣的(鏈接地址:http://www.cnblogs.com/liuzhen1995/p/5700409.html)~~~

      附:Demo_JSP_JavaBean項目源碼文件百度雲下載鏈接:http://pan.baidu.com/s/1sl1nd9r 密碼:lrdk;         本實例所使用數據庫建表sql語句文件下載鏈接:http://pan.baidu.com/s/1eS0n9aM 密碼:7ttd

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved