#副標=Java Code Generator #大標= XDoclet進階篇 #作者=文/歐宣修 -----box----- #程式1 UseraccountFacade原始碼 /** * 取得使用者帳號檔
* Last Modified Date:2004/07/04
* @author Senshaw //……(1) * @ejb.bean * name="ejb/UseraccountFacade" * type="Stateless" * view-type="remote" * transaction-type="Container" */ public class UseraccountFacade implements SessionBean { static final Category log = Category.getInstance( UseraccountFacade.class.getName()); DAOBean dao = null; private SessionContext ctx; //……(2) /** @ejb.create-method */ public void ejbCreate() throws CreateException { dao = new DAOBean(); dao.setDefaultDBName("MasterDB"); } public void ejbPostCreate() throws CreateException { } public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { this.ctx = ctx; } //……(3) /** * @ejb.interface-method */ public HashData getUseraccount() { StringBuffer sql = new StringBuffer(); sql.append("select * from useraccount "); HashData hd = dao.executeQuery(sql.toString()); log.info("hd rowcount=" + hd.getRowcount()); return hd; } //以下略… } -----end----- -----box----- #程式2 UseraccountServlet2原始碼 /** * 透過EJB取得使用者帳號檔
* Last Modified Date:2004/07/03
* @author Senshaw * @web.servlet name="GetUseraccount2" * @web.servlet-mapping url-pattern= "/getUseraccount2" */ public class UseraccountServlet2 extends HttpServlet { static final Category log = Category.getInstance( UseraccountServlet2.class.getName()); DAOBean dao = null; Context context = null; UseraccountFacadeHome home = null; public void init() throws ServletException { //……(1) Hashtable environment = new Hashtable(); environment.put(Context.INITIAL_CONTEXT_ FACTORY, "org.jnp.interfaces. NamingContextFactory"); environment.put(Context.URL_PKG_ PREFIXES, "org.jboss.naming:org.jnp.interfaces"); environment.put(Context.PROVIDER_URL, "jnp://localhost:1099"); try { context = new InitialContext(environment); } catch (NamingException ex) { log.error(ex); } try { //look up jndi name Object ref = context.lookup( "ejb/UseraccountFacade"); //look up jndi name and cast to Home interface home = (UseraccountFacadeHome) PortableRemoteObject.narrow(ref, UseraccountFacadeHome.class); } catch (Exception ex) { log.error(ex); } } 略… private void getUseraccount(HttpServletRequest request, HttpServletResponse response) { request.getSession().setAttribute("header", "帳號\t密碼\t角色"); request.getSession().setAttribute("column", "userid\tpasswd\trolename"); try { //……(2) UseraccountFacade facade = home.create(); HashData hd = facade.getUseraccount(); log.info("hd rowcount=" + hd.getRowcount()); request.getSession().setAttribute("data", hd); facade.remove(); } catch (Exception ex) { log.error(ex); } 以下略… } } -----end----- ( -----box----- #程式3 -----end----- -----box----- #程式4 ant gen-ejb -----end----- -----box----- #程式5 /** * 使用者帳號檔 * @struts.form * name="useraccount.query" */ public class UseraccountForm extends ValidatorForm implements Serializable{ 以下略… } -----end----- -----box----- #程式6 /** * @struts.action * name="useraccount.query" * path="/queryUseraccount" * input="/useraccount_query.jsp" * * @struts.action-forward * name="success" * path="/useraccount_result.jsp" */ public class QueryUseraccountAction extends Action { 以下略… } -----end----- -----box----- #程式7 StrutsActionServlet org.apache.struts.action. ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 2 -----end----- -----box----- #程式8 -----end----- -----box----- #程式9 ant clean ant gen-struts -----end-----