当前位置:编程学习 > JSP >>

JBuilder2005+JBoss-4.0.2RC1+J2SDK1.5+Log4j 开发Session Bean例解(4)

答案:
作者:Junsan Jin

日期:2005-3-30

版本:1.0

信箱:junsan21@126.com ; junnef21@sohu.com

声明:本人保留本文的所有权利。







第四部分:测试Session bean



在前面的三个部分已经完整地讲述了一个Session Bean的建立过程。下面来建立测试工程。




 


添加测试类:



在右边的树型菜单中选中com.inspiresky.trader右键 〉new 〉package,在对话框的输入框最后添加user,新建com.inspiresky.trader.user包。



选中com.inspiresky.trader.user右键 〉new 〉class,创建UserInfo.java类,代码如下。



----------------------------------



UserInfo.java



----------------------------------



package com.inspiresky.trader.user;




 


import javax.naming.InitialContext;



import trader.UserInfoLocalHome;



import trader.UserInfoLocal;



import javax.naming.NamingException;



import java.rmi.RemoteException;



import javax.ejb.CreateException;



import java.util.HashMap;



import java.util.Date;




 


/**



 * <p>Title: </p>



 *



 * <p>Description: </p>



 *



 * <p>Copyright: Copyright (c) 2005</p>



 *



 * <p>Company: </p>



 *



 * @author not attributable



 * @version 1.0



 */



public class UserInfo {




 


    public UserInfo() {



    }




 


    public HashMap getUserInfo(){



        HashMap map = new HashMap();




 


        try {



            UserInfoLocal local = (UserInfoLocal) findEjb();



            map = local.showUserInfo();



        } catch (CreateException ex) {



            System.out.println("Error 1");



        } catch (RemoteException ex) {



            System.out.println("Error 2");



        } catch (NamingException ex) {



            System.out.println("Error 3");



        }




 


        return map;



    }




 


    private Object findEjb() throws NamingException, RemoteException,



        CreateException {



        javax.naming.Context context  = new javax.naming.InitialContext();




 


        Object obj = context.lookup("UserInfoLocal");




 


        UserInfoLocalHome home = (UserInfoLocalHome)javax.rmi.PortableRemoteObject.narrow(obj,



            UserInfoLocalHome.class);




 


        UserInfoLocal local = (UserInfoLocal)home.create("Jak","12",new Date().toString());




 


        return local;



    }



}




 


添加客户类:



同样的方法在同一个包中创建类Test.java,代码如下。



---------------------------------



Test.java



-----------------------------------



package com.inspiresky.trader.user;




 


import javax.servlet.*;



import javax.servlet.http.*;



import java.io.*;



import java.util.*;



import com.inspiresky.trader.user.UserInfo;




 


public class Test extends HttpServlet {



    private static final String CONTENT_TYPE = "text/html; charset=GBK";



    private HashMap map;




 


    //Initialize global variables



    public void init() throws ServletException {



        UserInfo user = new UserInfo();



        map = user.getUserInfo();



    }




 


    //Process the HTTP Get request



    public void doGet(HttpServletRequest request, HttpServletResponse response) throws



        ServletException, IOException {



        response.setContentType(CONTENT_TYPE);



        PrintWriter out = response.getWriter();



        out.println("<html>");



        out.println("<head><title>Test</title></head>");



        out.println("<body bgcolor=\"#ffffff\">");



        out.println(map.toString());



        out.println("</body>");



        out.println("</html>");



        out.close();



    }




 


    //Process the HTTP Post request



    public void doPost(HttpServletRequest request, HttpServletResponse response) throws



        ServletException, IOException {



        doGet(request, response);



    }




 


    //Process the HTTP Put request



    public void doPut(HttpServletRequest request, HttpServletResponse response) throws



        ServletException, IOException {



    }




 


    //Process the HTTP Delete request



    public void doDelete(HttpServletRequest request,



                         HttpServletResponse response) throws ServletException,



        IOException {



    }




 


    //Clean up resources



    public void destroy() {



    }



}

上一个:引用 Eclipse 照亮Java众生
下一个:JBuilder2005+JBoss-4.0.2RC1+J2SDK1.5+Log4j 开发Session Bean例解(3)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,