架构实例之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