<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    新的起點 新的開始

    快樂生活 !

    Creating EJB3 Sessions Beans using Netbeans 6.1 and Glassfish(來自互聯網)

    Netbeans 6.x brought nice features and let the web development much easier. You can create many things in a ".VS-style", in other words, drag-and-drop components on the User Interface. A good example of this it is the Visual Java Server Faces features from NetBeans 6 and the Web Service module.

    Netbeans also brought Glassfish Application Server as its official server. Glassfish is the the RI (Reference Implementation) from JEE 5, therefore it is the better choice when you want to work with EJB 3.

    Using Glassfish within Netbeans 6.1

    When you are installing Netbeans, you will be asked about extra-programs. One of them is the Glassfish. If you choose the Glassfish in the installation process, it will be installed/configured automatically within NetBean 6.1

    To check out if Glassfish was installed successfully, open the Netbeans and go to the tab Services (left window). Expand Servers option and you should see the Glassfish there.

    If you want, you can start the server in this way: right mouse click on Glassfish -> Start.

    Creating an Enterprise Application Project

    Let's get started the code itself. The first thing to do is to create the Enterprise Application Project, or simply, EAR.

    To do that, back to the tab Projects and right mouse click and select option New Project. On the new screen, select the Category Enterprise and then the project Enterprise Application, click next to go to the next screen.

    On the next screen select the Project name and the Project Location. (In my example, I am going to use the name Test), click next again.

    On the third screen, select the server (by default Netbeans brings Glasshfish), choose the Java Version (Java EE 5 by default), and select the EJB and Web module.

    Note: By default, the EJB module's name is "EAR name" plus "-ejb". The Web module's name is "EAR name" plus "-war". However you are free to change these names.

    When the projects are created, they will be shown on the Projects tab.

    Creating our first EJB component (Stateless Session Beans)

    On the Projects tab, expand the EJB module (in my case called "Test-ejb"). You will see some options, such as: Enterprise Beans, Configuration Files, Server Resources and so on. We will work around the Enterprise Beans, therefore right mouse click on Enterprise Beans -> New -> Session Bean.

    On the screen that will come up, choose the EJB Name, Package, Session Type and Interface. For my example, I am using "TestEJB","stateless","Stateless","Remote". Click fisnish when your fields are filled.

    After the Session Bean is created, you will see the class (and the interface) in the Source Packages section, as well as the Bean in the Enterprise Beans section. In the main tab, the TestEJBBean.java will be opened automatically.

    Our Stateless Session Bean was created into the stateless package, but now we have to implement it. Open it (if it is closed) with double click on the TestEJBBean from Enterprise Beans section (you can open the file directly from the Source Package as well).

    In the body of the class there is a comment " // Add business logic below. (Right-click in editor and choose // "EJB Methods > Add Business Method" or "Web Service > Add Operation")"

    So right mouse click in the editor and choose EJB Methods > Add Business Method. A new screen will come up. In your example, we will have only one method (called getMessage()) with String return. Hence, put in the field name getMessage and return type: String.

    Done, our first EJB3 Session Bean object has been created. To verify the code it has created, you can open the class TestEJBBean (implementation) and the interface TestEJBRemote (remote interface). Different than Eclipse, where we put ourself the interface and the class name, Netbeans uses the pattern Object Name plus Remote or Local. Of couse you can change it yourself

    The last thing to do is to implement the business method we just added. It is simple, only return a string message: Hello EJB World. The code looks like below:

    package stateless;

    import javax.ejb.Stateless;

    @Stateless
    public class TestEJBBean implements TestEJBRemote {
    public String getMessage() {
    return "Hello EJB World";
    }
    }

    Simple, huh?

    Using the Web Module as EJB Client

    We have already created the Web Module previously when the EAR has been created, so let’s use it as EJB Client.

    When the Web module is created, by default Netbeans already creates a file called index.jsp in the Web Pages section. Open this file to add a call to the servlet. The code looks like below:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <h2>Hello World!</h2>
    <a href="TestServlet">Click here to call the EJB component</a>
    </body>
    </html>

    As you can see, we must create a servlet called TestServlet. it will call the EJB component when the link from User Interface is clicked.

    To create the servlet itself, right click on Web project (in my case Test-web), New -> Servlet.

    On the new screen, select the Class Name and the Package. On next screen, you can keep all options and click on Finish button. The Servlet will be created and its code will appear on the Main tab.

    Much easier than JBoss, in Glassfish there is the annotation @EJB. When the container sees this annotation, is inject the component directly in the Servlet, Java Class, Managed Bean, whatever. (In JBoss this annotation has not been implemented yet, so you must use JNDI to call a remote component).

    The @EJB annotation gets inside the attribute and can be used directly in the code. The code from Servlet looks like below:

    package servlets;

    import java.io.*;

    import javax.ejb.EJB;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import stateless.TestEJBRemote;

    public class TestServlet extends HttpServlet {

    //This annotation INJECTS the TestEJBRemove object from EJB
    //into this attribute
    @EJB
    private TestEJBRemote testEJB;

    @Override
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException,
    IOException {
    System.out.println(testEJB.getMessage());
    }
    }

    Look at the line #13. It has the @EJB annotation. Also, look at the body of doGet method. No JNDI method was used, no code to call the EJB object was used, thus the code gets much clear than JNDI (Point to Glassfish over JBoss :) )

    Running the code

    You can run the application right click on EAR project (in my case Test) and select option Run. Netbeans will do the deploy automatically and when the glassfish is started, the index.jsp page will be opened in your web browser.

    After that, click on the LINK and see in the console of Netbeans (tab Glassfish) to see the output.

    Note:
    Although the @EJB annotation is much better than JNDI calls, it only works in the Glassfish server environment. Therefore if you are creating a stand-alone application that calls a remote object, you can not use the @EJB annotation.

    Creating a Java Stand-Alone Client

    As mentioned above, you cannot use the @EJB annotation out of a Glassfish server environment, therefore in a Stand-Alone application you must use the JNDI. Below following an example of how to call a EJB from a Java Stand-Alone client.

    First of all, create a Java Project. New Project -> Java -> Java Application.

    In this application, you must add two JARs file, as well as associate your application with the EJB module (thus your client knows about the EJB Interface).

    To do that, right click on Java Project, Properties -> Libraries -> Add Project (to add the Test project) and Add JAR/Folder to add the following jar files:

    1. appserv-rt.jar
    2. javaee.jar

    Both jar can be located into $GLASSFISH_HOME/lib directory.

    By default, the appserv-rt.jar brings a jndi.properties file, but I will show you how to create one. Why? Because when you are running the client in the machine different than the app server, you must override this file.

    So create a file called jndi.properties file into the root of the Java project and put the following content:

    java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
    java.naming.factory.url.pkgs = com.sun.enterprise.naming
    java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
    #optional. Defaults to localhost. Only needed if web server is running
    #on a different host than the appserver
    org.omg.CORBA.ORBInitialHost = localhost
    #optional. Defaults to 3700. Only needed if target orb port is not 3700.
    org.omg.CORBA.ORBInitialPort = 3700

    Now, open the Main class and add the following commands:

    package testclient;

    import java.io.FileInputStream;
    import java.util.Properties;
    import javax.naming.InitialContext;
    import stateless.TestEJBRemote;

    public class Main {

    public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.load(new FileInputStream("jndi.properties"));
    InitialContext ctx = new InitialContext(props);
    TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
    System.out.println(testEJB.getMessage());
    }
    }

    Save it and before run this code, make sure the Glassfish is running. After you run the java class, you should see the EJB message onto the console.

    This topic overs here. I hope this topic be useful for anyone, as well as you can choose yourself which environment (Eclipse or Netbeans) is better for you.

    posted on 2008-07-26 21:56 advincenting 閱讀(1386) 評論(1)  編輯  收藏 所屬分類: 服務器 比如:WebLogic(Develop+Deploy)

    評論

    # re: Creating EJB3 Sessions Beans using Netbeans 6.1 and Glassfish(來自互聯網) 2009-03-26 17:43 lztang

    這個例子不能在遠程服務器上運行,
    2009-3-26 17:36:21 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl <init>
    警告: "IOP00410201: (COMM_FAILURE) 連接失敗: 套接字類型: IIOP_CLEAR_TEXT;主機名: 127.0.0.1;端口: 3700"
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at eaptlztest.Main.main(Main.java:31)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:244)
    ... 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    ... 14 more
    2009-3-26 17:36:21 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl <init>  回復  更多評論   

    公告

    Locations of visitors to this pageBlogJava
  • 首頁
  • 新隨筆
  • 聯系
  • 聚合
  • 管理
  • <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    統計

    常用鏈接

    留言簿(13)

    隨筆分類(71)

    隨筆檔案(179)

    文章檔案(13)

    新聞分類

    IT人的英語學習網站

    JAVA站點

    優秀個人博客鏈接

    官網學習站點

    生活工作站點

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 春暖花开亚洲性无区一区二区| 亚洲国语在线视频手机在线| 国产亚洲精品美女| 亚洲中文无码永久免费| 亚洲一卡二卡三卡四卡无卡麻豆| 国产成人精品免费视频大全麻豆| 亚洲日产2021三区在线| 999国内精品永久免费观看| 亚洲看片无码在线视频 | 亚洲视频在线免费观看| 久久永久免费人妻精品下载| 亚洲色图在线观看| 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 一本色道久久综合亚洲精品高清| 日本中文字幕免费看| 亚洲性在线看高清h片| 三级黄色片免费看| 亚洲男人都懂得羞羞网站| 日本黄网站动漫视频免费| 亚洲中文字幕久久久一区| 免费人成年激情视频在线观看 | 亚洲天堂一区在线| 日韩免费电影在线观看| eeuss免费影院| 亚洲国产香蕉碰碰人人| 最近免费中文字幕4| 一级片在线免费看| 久久亚洲国产伦理| 青娱乐免费在线视频| 黄色毛片免费观看| 久久久婷婷五月亚洲97号色 | 亚洲精品自产拍在线观看动漫| 亚洲一区二区三区免费观看| 亚洲av永久无码天堂网| 亚洲国产天堂久久久久久| 一级毛片免费观看不卡视频| 亚洲国产综合AV在线观看| 久久夜色精品国产亚洲| 免费精品一区二区三区在线观看| 美女无遮挡拍拍拍免费视频| 亚洲一区二区三区久久|