Java Bean和POJO都可以用于业务逻辑和显示逻辑解耦合的作用,Java Bean遵循一些标准化的约定,有利于重用和互用,但是也牺牲了个性,对于比较小型的项目,POJO更加简洁灵活。
一般在做JSP网站的时候,比如像连接读写数据库的操作比较繁琐,这时候使用Java Bean或者POJO做成类,就可以使得JSP通过分离后端操作和重用类来简化JSP内的JAVA代码,使之简明清晰。
package db;
import java.beans.*;
import java.io.Serializable;
/**
*
* @author Administrator
*/
public class DB implements Serializable {
public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
private String sampleProperty;
private PropertyChangeSupport propertySupport;
public DB() {
propertySupport = new PropertyChangeSupport(this);
}
public String getSampleProperty() {
return sampleProperty;
}
public void setSampleProperty(String value) {
String oldValue = sampleProperty;
sampleProperty = value;
propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
package db;
import java.beans.*;
import java.io.Serializable;
/**
*
* @author Administrator
*/
public class DB1 implements Serializable {
public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
private String sampleProperty;
private PropertyChangeSupport propertySupport;
public DB1() {
propertySupport = new PropertyChangeSupport(this);
}
public String getSampleProperty() {
return sampleProperty+"987654321";
}
public void setSampleProperty(String value) {
String oldValue = sampleProperty;
sampleProperty = value+"123456789";
propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class DBLink {
Connection conn;
Statement stmt;
String url = "jdbc:mysql://localhost:3306/users?user=root&password=admin&useUnicode=true&charaterEncoding=UTF8";
String sql = "";
ResultSet rs;
String rsString="";
public String dbValueOfField(String field,String table,String conditions){
try {
//注意要是的数据库能够正常连接上必须在库中添加mysql-connector-java的jar包
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
sql="SELECT "+field+" FROM "+table+" WHERE "+conditions;
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
rsString+=rs.getString(1);
}
return "Hello "+rsString+"!";
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(DBLink.class.getName()).log(Level.SEVERE, null, ex);
}
return "Hello "+rsString+"!";
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="db.DB"%>
<%@page import="db.DBLink"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>三种使用Java Bean和POJO的小例子</h1>
<%
DB db = new DB();
db.setSampleProperty("hello my bean class 111");
String dbrs = db.getSampleProperty();
%>
<%= dbrs%>
<hr/>
<jsp:useBean id="db1" class="db.DB1" />
<jsp:setProperty name="db1" property="sampleProperty" value="hello db1 bean" />
<jsp:getProperty name="db1" property="sampleProperty" />
<hr/>
<%
DBLink dblink = new DBLink();
String welcome = dblink.dbValueOfField("nickname", "userinformation", "username='111@111.com'");
%>
<%= welcome%>
</body>
</html>