Video Location: http://www.vimeo.com/4075401 Example location (for jeee 5) http://www.webagesolutions.com/knowledgebase/javakb/jkb005/index.html Example location (for jeee 6) http://www.oracle.com/technology/tech/java/newto/introejb.htm slides location: http://www.javapassion.com/j2ee/EJB3.0.pdf Installing GlassFish Tools Bundle For Eclipse: Go to website: http://download.java.net/glassfish/eclipse/ From drop down menu, select: Windows xp Click download In the dialog box showing, click: save file Download on Desktop or supply another folder name. After the file is downloaded, double click the downloaded file name In the dialog box showing, select: run In the set up dialog box shown, click: next In the License Agreement dialog box, click: I agree In Choose Install Location dialog box, for the destination folder, accept the folder name showing, and click: Install When installation is done, in the Installation Complete dialog box, click: Close In the start menu, under all programs, check that Glassfish bundle for eclipse is listed. (Installation is now completed). Testing the Installation: Starting GlassFish Eclipse bundle: Using the start button, under all programs, select: Glassfish bundle for eclipse (Glassfish bundle will start) (Workspace Launcher dialog box will show) For workspace name, accept the name showing, and click: OK In the Register Glassfish dialog box, click: Cancel Glassfish tools bundle for eclipse will run. If a welcome screen is showing, close it by clicking on “X” . Glassfish window will show with a menu bar with menu entries of File, Edit, Navigate, Search, Project etc. Also below the menu bar, a tool bar will show. Creating a project Select: File > New (EJB Project dialog box will show) Under project name, enter: as1 Click: next Click: next Check: Generate ejb-jar.xml deployment descriptor Click: Finish Selcet: File > New > Session Bean (ejb 3.x) In the dialog box showing, Enter java package: as1 Enter class name: Hello Under Start type, select: Stateless Under Create business interface: Check : Remote Uncheck: Local Click next [Record Bean Name (Hello), Interface name (as1.HelloRemote) Mapped Name(empty)] Click: Finish In Project Explorer view: Under ejbModule, Open as1, double click: HelloRemote.java In the code window, within the public interface HelloRemote, add an extra method as below: public interface HelloRemote { public String getMessage (String s); } The file content of HelloRemote.java would look as below: package as1; import javax.ejb.Remote; @Remote public interface HelloRemote { public String getMessage (String name); } Save the file by selecting: File > Save Open as1, double click: Hello.java In the code window, within the public class Hello, add an extra method as below: public String getMessage (String name){ String message = name + ", How are you!"; return message; } The content of file Hello.java would look as below: package as1; import javax.ejb.Stateless; /** * Session Bean implementation class Hello */ @Stateless public class Hello implements HelloRemote { /** * Default constructor. */ public Hello() { // TODO Auto-generated constructor stub } public String getMessage (String name){ String message = name + ", How are you!"; return message; } } Save the file by selecting: File > Save Creating a client Select: File > New > Application Client Project In the dialog box showing: Enter project name: as1Client Click: next Click: next Check: Create a default Main class Check: Generate application-client.xml deployment descriptor Click: Finish Under as1Client, under appClientModule, under (default package), double click Main.java The contents of Main.java will show in code window. Replace the content of method main, with the following: public static void main(String[] args) throws Exception { Context context = new InitialContext(); as1.HelloRemote myejb = (as1.HelloRemote)context.lookup("as1.HelloRemote"); String message = myejb.getMessage("Buddy"); System.out.println(message); } Also add the following imports import javax.naming.*; The class Main would look as below: public class Main { public static void main(String[] args) throws Exception { Context context = new InitialContext(); as1.HelloRemote myejb = (as1.HelloRemote)context.lookup("as1.HelloRemote"); String message = myejb.getMessage("Buddy"); System.out.println(message); } /* (non-Java-doc) * @see java.lang.Object#Object() */ public Main() { super(); } } Save the content of the Main.java file by selecting: File > Save In as1 project, under ejbModule, copy the package as1. In as1Client project, in appClientModule, paste the package as1. In appClientModule, in as1 package, remove all .java files except HelloRemote.java In Project Explorer, Right click as1Client, select: Build Path > Configure Build Path In the dialog box showing, select tab: Libraries Click the button: Add External Jars In the Look in drop down combo box, select: GlassFish-Tools-Bundle-For-Eclipse-v1.2>glassfishv3> glassfish>lib Select jar file: appserv-rt.jar Click: Open (the jar file will show in the list of jar files) Click: OK Start Server With EJB deployed Right click the project as1, select: Run As > Run on server In the Run on server dialog box, check: Choose an existing server. Click: Finish The server would start and the ejb in the as1 project would be automatically deployed. In the lower part, under the server tab, check that server has been started. After the server is started, in the lower part, select: Console tab. Then, in the lower part, towards the farther right, click the down arrow with the display icon, and select: server log. (The server log will show) At the end of the server log, there would be a message as below: INFO: JTS5014: Recoverable JTS instance, serverId = [3700] INFO: Portable JNDI names for EJB Hello : [java:global/as1/Hello, java:global/as1/Hello!as1.HelloRemote] INFO: Glassfish-specific (Non-portable) JNDI names for EJB Hello : [as1.HelloRemote#as1.HelloRemote, as1.HelloRemote] INFO: Hibernate Validator bean-validator-3.0-JBoss-4.0.2 INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. INFO: as1 was successfully deployed in 14,283 milliseconds. Th above shows that one of the jndi name we can use is : as1.HelloRemote Use as1.HelloRemote for lookup in the application main method. Run Application Under asClient, under application Module, under default package, Right Click: Main.java Select: Run As > Java Application The client will run and would display, in the lower part, in the Console tab, the message below: Buddy, How are you! When Client Doesn’t Work Make sure the following library is added to the client build path. This library contains the properties file. It is required to make JNDI work correctly. appserv-rt.jar Working With Database Configuring GlassFish: In the top right hand corner, just below the menu bar, two buttons labeled “Java” and “Java EE” will be showing. Select Java EE button if not already selected. In the lower part, a number of tabs such as Problems, Tasks, Properties, Servers, Data Source Explorer, Console etc will show. Select the tab: Servers Select: Windows > Preferences In the left navigation bar, select GlassFish. Check the entry: Start JavaDB database process when starting Glassfish server. In the left navigation bar, click on the + symbol on Java to open it. Under that, click on the + symbol on Build Path to open it Under that, select User Libraries Click the “New” button on the right side. For the User Library Name in the dialog box, enter: as1_lib Click OK On the right, click the “Add Jars” button. In the dialog box showing, browse to folder: C:\Glassfish-tools-bundle-for-eclipse1.2\glassfishv3\glassfish\modules (Depending upon the version installed, the above names may be slightly different) Within the folder, select all files with the word persistence in it. These would include the following: javax.persistence.jar org.eclipse.persistence.asm.jar org.eclipse.persistence.core.jar org.eclipse.persistence.jpa.jar org.eclipse.persistence.antlr.jar org.eclipse.persistence.oracle.jar org.eclipse.persistence.jpa.modelgen.gar (use ctl-click for selecting these files) Click open The file names will show under the library name. click ok. (The above library will be included in the GlassFish server when started) Starting GlassFish server: In the middle, towards the bottom, select the tab: Servers (The name of GlassFish server will show). Right click the the server name. In the popup menu, select Start. In the Console tab, message will show. When the server is started, the Server tab will show the server started. Connecting to database: Select the tab: Data Source Explorer (Under database connections, a database name will show) Right Click the data base name (i.e. Sample JavaDB database). In the popup menu select: Connect (The server will be connected to the database).