Demo_JSP_JavaBean and demo_jsp_javabean for schema instances

Source: Internet
Author: User

Demo_JSP_JavaBean and demo_jsp_javabean for schema instances

Demo_JSP_JavaBean for schema instances

1,Development tools and development environment

Development tools: MyEclipse10, JDK1.6.0 _ 13 (32-bit), Tomcat7.0 (32-bit), and mysql5.7.13

Development Environment: WIN10

2,Demo_JSP_JavaBean implementation function

 User Logon, user registration, and logout.

3,Demo_JSP_Java_Bean Usage Technology

This example uses JSP, JavaBean, and JDBC to Implement User Logon, user registration, and logout. Figure 1 shows the system architecture:

 Figure 1: Demo_JSP_Java_Bean System Architecture

 

See figure 2 (logical relationship between JSP and JavaBean in the system ):

 

  

Figure 2: Logical Relationship Between JSP and JavaBean IN THE SYSTEM

 

4,Implementation

(1) create a new Web project in MyEclipse and name it Demo_JSP_JavaBean;

(2) Import mysql-connector-java-5.1.6-bin.jar to Demo_JSP project, this package is to achieve Java Connection database function package (will not import the package of students, can Baidu yo );

Attachment: mysql-connector-java-5.1.6-bin.jar, Baidu cloud download link: http://pan.baidu.com/s/1i5psdDF password: meyg

(3) create the following JavaBean file and JSP file in the Demo_JSP Project (PS: the JSP file code here is only part of the code, for other JSP file code, please refer to my previous blog Oh (link address: http://www.cnblogs.com/liuzhen1995/p/5700409.html )):

1) create a Java class DBAccess with the package name "liu" to implement the database login connection function. The specific code is as follows:

 

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) create a Java UserBean under the package created in the previous step to implement the database query and write functions. The Code is as follows:

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: receives the username and password entered by the user on the login. jsp page and calls JavaBean for logon authentication. The specific code is as follows:

<%@ page import="liu.UserBean" %><%//get parametersString username = request.getParameter("username");String password = request.getParameter("password");//check nullif (username == null || password == null) {    response.sendRedirect("login.jsp");}//validateUserBean 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">

4) register_action.jsp: registers data by calling JavaBean and writes data to the database. The specific code is as follows:

<%@ page import="liu.UserBean" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%//get parametersString username = request.getParameter("username");String password1 = request.getParameter("password1");String password2 = request.getParameter("password2");String email = request.getParameter("email");//check nullif (username == null || password1 == null || password2 == null || !password1.equals(password2)) {    response.sendRedirect("register.jsp");}//validateUserBean 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">

5,Running result display

For specific running results, please refer to my previous blog Oh, running results are exactly the same (link address: http://www.cnblogs.com/liuzhen1995/p/5700409.html )~~~

Appendix: Demo_JSP_JavaBean project source code file Baidu cloud download link: http://pan.baidu.com/s/1sl1nd9r password: lrdk; this instance uses the database to Create Table SQL statement File Download link: http://pan.baidu.com/s/1eS0n9aM password: 7ttd

 

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.