|
在USERBEAN里必须有下面类似的METHOD:
private ConnectionPool connPool;
private SqlCommand sqlComd;
public void setConnectionPool(ConnectionPool connPool)
{this.connPool=connPool;}
public void setSqlCommand(SqlCommand sqlComd)
{this.sqlComd=sqlComd;}
public boolean checkUser(...){...}
初始化USERBEAN时必须调用上面两个METHOD否则USERBEAN不能用啊(若USERBEAN要连数据库的话).要调用的话得传CONNECTIONPOOL和SQLCOMMAND过去,那么他们就必须先初始化,在哪初始话,放在什么SCOPE中或者干脆只用一次,别的JSP或SERVLET不需要调用,那么也不需要放在SCOPE里,这些全看你自己的需要.这里给个例子(放在APPLICATION SCOPE里,若用SESSION和REQUEST SCOPE已经有DEFAULT的SESSION和REQUEST OBJECT了,所以挑个难的)
// first servlet called before any jsp page (or you can convert them to 1st jsp)
ConnectionPool connPool;
SqlCommand sqlComd;
// in init()
// ... initialize ConnectionPool
...
// ... initailize SqlCommand
...
// put them into application scope
getServletContext().setAttribute("connection", connPool);
getServletContext().setAttribute("sql", sqlComd);
// in detroy()
// remove connPool and sqlComd
// any jsp page to use connectionPool and sqlCommand
<%
UserBean user = new UserBean();
// pull the them from application scope
ConnectionPool connPool = (ConnectionPool)pageContext.getAttribute("connection", PageContext.APPLICATION_SCOPE);
SqlCommand SqlComd = (SqlCommand)pageContext.getAttribute("", PageContext.APPLICATION_SCOPE);
user.setConnectionPool(connPool);
user.setSqlCommand(sqlComd);
session.setAttribute("user", user);
...
%>
在SERVLETs(或JSPs)之间传递或共享数据还有好几种方法,上面的是"最好用的"(我认为),其它的细节看OREILLY的JAVA SERVLET,里面有更详细的讨论. |
|