First, let's have a general directory structure.
Code directly below
HelloWorld.java
package com.fhit.ejb; public interface HelloWorld { public String say(String name); }
HelloWorldLocal.java
package com.fhit.ejb; public interface HelloWorldLocal extends HelloWorld{ }
HelloWorldBean.java
package com.fhit.ejb.impl; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import com.fhit.ejb.HelloWorld; import com.fhit.ejb.HelloWorldLocal; @Stateless(mappedName="HelloWorldBean") @Remote(HelloWorld.class) @Local(HelloWorldLocal.class) public class HelloWorldBean implements HelloWorld,HelloWorldLocal{ @Override public String say(String name) { // TODO Auto-generated method stub return name+"Hello"; } }
application.xml
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" version="5"> <display-name>EJB3 Sample Application</display-name> <module> <web> <web-uri>HelloWorld.war</web-uri> <context-root>HelloWorld</context-root> </web> </module> <module> <ejb>HelloWorld.jar</ejb> </module> </application>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <description>fhit ejb3.0 Sample</description> <ejb-local-ref> <ejb-ref-name>ejb/HelloWorldLocal</ejb-ref-name>//This is the name registered at jndi ENC <ejb-ref-type>Session</ejb-ref-type> <local>com.fhit.ejb.HelloWorldLocal</local> <ejb-link>HelloWorldBean</ejb-link> </ejb-local-ref> <welcome-file-list> <welcome-file>Test.jsp</welcome-file> </welcome-file-list> </web-app>
Test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="com.fhit.ejb.*,javax.naming.*"%> <% InitialContext context = new InitialContext(); HelloWorldLocal hello = (HelloWorldLocal) context.lookup("java:comp/env/ejb/HelloWorldLocal"); out.print(hello.say("local")); %>
Below is build.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="WeblogicHelloWorld" default="ear" basedir=".."> <property name="app.dir" value="${basedir}\WeblogicHelloWorld"/> <property name="src.dir" value="${app.dir}\src"/> <property name="weblogic.home" value="D:\Weblogic"/> <property name="wls.username" value="weblogic"/> <property name="wls.password" value="a7937387"/> <property name="wls.hostname" value="localhost"/> <property name="wls.port" value="7001"/> <property name="wls.server.name" value="AdminServer"/> <property name="build.dir" value="${app.dir}/build"/> <property name="build.classes.dir" value="${build.dir}/classes"/> <!-- <property name="build.classes.metainf" value="${build.classes.dir}/META-INF"/> --> <path id="build.classpath"> <!-- <fileset dir="${basedir}/lib/javaee"> <include name="*.jar"/> </fileset> --> <fileset dir="${weblogic.home}/oracle_home/wlserver/server/lib"> <include name="weblogic.jar"/> </fileset> <pathelement location="${build.classes.dir}"/> </path> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" classpathref="build.classpath"/> <target name="prepare" depends="clean"> <mkdir dir="${build.dir}"/> <mkdir dir="${build.classes.dir}"/> <!-- <mkdir dir="${build.classes.metainf}"/> --> </target> <target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**"> <classpath refid="build.classpath"/> </javac> <!-- <copy todir="${build.classes.metainf}"> <fileset dir="${app.dir}/WebRoot/META-INF"> <exclude name="application.xml"/> </fileset> </copy> --> </target> <target name="ear" depends="compile"> <jar jarfile="${app.dir}\HelloWorld.jar"> <fileset dir="${build.classes.dir}"> <include name="**/*.*"/> </fileset> </jar> <war warfile="${app.dir}/HelloWorld.war" webxml="${app.dir}/web/WEB-INF/web.xml"> <fileset dir="${app.dir}/web"> <include name="*.*"/> </fileset> </war> <ear earfile="${app.dir}/HelloWorld.ear" appxml="${app.dir}/META-INF/application.xml"> <fileset dir="${app.dir}"> <include name="HelloWorld.jar"/> <include name="HelloWorld.war"/> </fileset> </ear> <delete file="${app.dir}/HelloWorld.jar"/> <delete file="${app.dir}/HelloWorld.war"/> </target> <target name="deploy" depends="undeploy,ear"> <wldeploy action="deploy" name="HelloWorld" source="${app.dir}/HelloWorld.ear" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="true" verbose="true" failonerror="true"/> <echo message="EJB can be called via http://${wls.hostname}:${wls.port}/HelloWorld/Test.jsp"/> </target> <target name="undeploy" > <wldeploy action="undeploy" name="HelloWorld" targets="${wls.server.name}" user="${wls.username}" password="${wls.password}" adminurl="t3://${wls.hostname}:${wls.port}" debug="false" verbose="false" failonerror="false"/> </target> <target name="clean"> <delete dir="${build.dir}"/> </target> </project>
build.xml is the file that is deployed using ant, and the ant window will appear on the right side of eclipse-windows-show view-ant
Right-click the project name under ant, run as is ant build and the following appears to indicate success
Then enter the URL and the following will appear
If you want to test the remote interface remote that appears above, you can create a new java project test
import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.foshanshop.ejb3.HelloWorld; public class Tclient { public static void main(String[] args) throws NamingException { Properties properties=new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); properties.setProperty(Context.PROVIDER_URL,"t3://localhost:7001"); Context context = new InitialContext(properties); HelloWorld hello = (HelloWorld) context.lookup("HelloWorldBean#com.fhit.ejb.HelloWorld"); System.out.println(hello.say("Outlander")); } }
The console output is