Note: This section is for EJB development beginners, mainly to manually configure a basic text-based environment, so that beginners have written all kinds of programs and profiles from the beginning to more clearly understand the step gathering of EJB development and deployment. principle. This is a summary of the author in learning EJB development, welcome to correct. Author contact address: josserchai@yahoo.com
I. Requirements software list and download address 1, Sun Java SDK, we use the version: J2SDK1.4.1_01, download address is: http://www.sun.com/download/, downloaded file name: J2SDK -1_4_1_01-windows-i586.exe
2, JBoss, a free EJB container. We use versions: JBoss-3.2.1_tomcat-4.1.24, which is an EJB container that integrate Tomcat internally. The download address is: http://www.jboss.org/, the downloaded file name is: jboss-3.2.1_tomcat-4.1.24.zip.
3, Note: The author only guarantees that the following steps will be successful under the above software version, which should be the same for other versions, but the author does not date. The following steps will be strictly followed.
Second, install the required software 1, install SDK to C: /J2SDK1.4.1_01; method is: Double-click to perform the downloaded J2SDK-1_4_1_01-windows-i586.exe file, select the installation directory to c: /j2sdk1.4.1_01, other By default.
2, install the JBoss; method is: unfolded the downloaded JBoss-3.2.1_tomcat-4.1.24.zip file to any directory, decompress a JBoss-3.2.1_tomcat-4.1.24 folder in this directory, There is a series of files and folders under this folder. Then use a JBoss directory in the C-plate root directory and copy a series of files and folders under the JBoss-3.2.1_tomcat-4.1.24 folder just under pressure to the C: / jboss directory. The JBOSS is installed in the C: / JBoss directory.
3, after the above two steps are processed, the folder is shown below:
Third, configure the operating environment and test program directory, set environment variables
1, set the Java_Home system variable: Method under Win2k, right-click "My Computer", select Properties, and then "Advanced" -> Environment Variables "on the pop-up page, then in the pop-up page Under "System Variable", click "New" and enter "Java_Home" in the pop-up window, enter "C: /J2SDK1.4.1_01" in the Variable value (V) "column.
2, set the same method: jboss_home value is: c: / jboss
3, the same method adds the value of PATH to add: "C: /J2SDK1.4.1_01/bin; c: / jboss / bin"
Second, create a program directory
1. Create a MyProject directory under C: / JBoss to store all your projects.
2, build three directory in myProject directory: one is a JSP directory, which is used to store all JSP files and servlet files and other HTML, Image files, etc. Class files; one is a src directory that is used to store all Java source files. 3, create a new directory in the JSP directory and name: hello.war, used to publish a web program, then create a series of folders in this directory: New Web-INF under Hello.War The clip is newly created under WEB-INF, and then a Hello folder is created under the Classes folder.
4. Newly build a client directory and a hello.jar directory in the EJB directory, which are used to store EJB clients and server-side classes to be published. Then create a Hello directory in the client directory, and then create a new EJB directory in this directory. Then create a Hello directory and a Meta-INF directory in the Hello.jar directory and create a new EJB directory in the Hello directory.
5. After the above process is over, the structure formed is shown in the figure below:
Fourth, write test procedures and related scripts 1. Test JBoss installation success or not, the method is as follows: Enter the C: / JBOSS directory, execute run.bat -c all to launch a JBoss configuration server with all functions. The screen is no longer scrolling after a while. At this time, start IE input: http: // localhost: 8080 / jmx-console / index.jsp You can see a series of lists in IE, that is, the JBoss starts success. At this point, press CTRL C to close the JBoss server on the DOS window that starts JBoss.
Second, enter the c: /jboss/myproject/jsp/hello.war directory, use Notepad to create a new index.jsp file, as follows: Used to test the JSP program
<% @ Page ContentType = "text / html; charset = GB2312"%>
hEAD>
<%
String hello = "Hello World! This created by jsp file!";
Out.println (Hello);
%>
body>
html>
Third, enter the c: / jboss / myproject / src directory, new HelloWorld.java, is a servlet class, the content is as follows:
Package hello;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public class helloworld extends httpservlet {
Public void doget (httpservletRequest Request, httpservletResponse response) throws oException, servletexception
{
Response.setContentType (Text / HTML ");
PrintWriter out = response.getwriter ();
Out.println ("
Out.println ("Hello World! This Is CREATED BY Servlet!");
Out.println ("
}
}
At this way, we have established JSP files and servlet classes for testing, and will build a HelloWorld's stateless SessionBean.
Fourth, enter the c: / jboss / myproject / src directory, create a Hello.java file to implement the EJB Remote interface, the content is as follows,
Package hello.ejb;
/ **
* Remote Interface for Enterprise Bean: Hello
* Remote interface
* /
Public interface hello extends javax.ejb.ejbobject {
/ **
* Unique method Hello returns to the client.
* /
Public string hello () throws java.rmi.remoteException;
}
Similarly, create a new Hellolocal.java file in this directory, implement the local interface, the content is as follows:
Package hello.ejb;
Import javax.ejb.ejblocalobject;
Public interface hellolocal extends ejblocalobject {
Public string hello ();
}
Similarly, create a HelloHome.java file in this directory, implement the HOME interface, the content is as follows:
Package hello.ejb;
/ **
* Home Interface for Enterprise Bean: Hello
* Home interface
* /
Public interface hellohome extends javax.ejb.ejbhome {
/ **
* Creates a Default Instance of Session Bean: Hello
* /
Public Hello Create () throws javax.ejb.createException, java.rmi.remoteException;
}
Similarly, create a new hellolocalhome.java file in this directory, implement the localhome interface, the content is as follows:
Package hello.ejb;
Import javax.ejb.ejblocalhome;
Public interface hellolocalhome extends ejblocalhome {
Hellolocal create () throws javax.ejb.createException;
}
Similarly, create a new Hellobean.java in this directory, implement the EJB class, [By following:
Package hello.ejb;
/ **
* Bean Implementation Class for Enterprise Bean: Hello * /
Public class hellobean imports javax.ejb.sessionBean {
Private javax.ejb.sessionContext mysis;
/ **
* GetSessionContext
* /
Public javax.ejb.SessionContext getsessionContext () {
Return mysessionCTX;
}
/ **
* setSessionContext
* /
Public void setsessionContext (javax.ejb.sessionContext CTX) {
MysessionCTX = CTX;
}
/ **
* ejbactivate
* /
Public void ejbactivate () {
}
/ **
* ejbcreate
* /
Public void ejbcreate () throws javax.ejb.createException {
}
/ **
* ejbpassivate
* /
Public void ejbpassivate () {
}
/ **
* ejbremove
* /
Public void ejbremove () {
}
/ **
* Business method
* /
Public string hello () {
Return "Hello, World! This is created by ejb!";
}
}
At this point, we have written all kinds of all kinds of classes needed by EJB classes. Let's write files used to test this EJB program. Specifically there are two: one is to call EJB with client servlet, another It is called EJB with the client class. The implementation is as follows:
V. Programs that implement the test EJB
Enter the C: / JBoss / MyProject / src directory, create a Helloservlet.java file for testing EJB through servlet. The content is as follows:
Package hello;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import java.io. *;
Import javax.ejb. *;
Import javax.naming.initialcontext;
Import javax.naming.context;
Import hello.ejb. *;
Import java.util. *;
Public Class Helloservlet Extends Httpservlet
{
Private hellohome home;
Public void init () THROWS servletexception
{
Try
{
String InitctXFactory = getinitParameter (Context.Initial_Context_Factory);
String providerurl = getinitParameter (context.provider_url);
Properties env = new property ();
IF (INitctXFactory! = null)
{
Env.put (Context.Initial_Context_Factory, InitctXFactory);
}
IF (providerurn! = null)
{
Env.put (Context.Provider_URL, ProviderURL);
}
Context CTX = New InitialContext (ENV); Object Objref = CTX.lookup ("Hello");
// main interface
HOME = (HelloHome) javax.rmi.portableremoteObject.narrow (Objref, Hello.ejb.hellohome.class);
} catch (javax.naming.namingexception ne) {
System.out.println ("Create Exception Caught: NE);
Throw new servletexception (ne.tostring ());
} catch (Exception E)
{
Throw new servletException (e.tostring ());
}
}
Public Void Service (httpservletRequest Req, httpservletResponse res) throws oException
{
Res.SetContentType ("text / html");
PrintWriter out = res. maxwriter ();
Out.println ("
Try {
// Component interface
Hello bean = home.create ();
Out.println (bean.hello ());
} catch (javax.ejb.createexception ce) {
Out.println ("Create Exception Caught: CE);
Ce.PrintStackTrace (out);
} catch (java.rmi.RemoteException Re) {
Out.println ("Remote Exception Caught: Re);
Re.PrintStackTrace (out);
}
Out.println (" Body> HTML>");
}
}
Also in this directory, create a HelloClient.java file for testing EJB through the client class, as follows:
Package hello.ejb;
Import java.util. *;
Import java.io. *;
Import javax.naming.initialcontext;
Import javax.rmi.portableremoteObject;
Import javax.naming.context;
Import javax.rmi.portableremoteObject;
Import hello.ejb. *;
Public class helloclient
{
Public static void main (string [] args)
{
Try
{
// JNDI configuration, implemented as an external property file
Properties env = new property ();
The //config.properties file should be placed in the same-level directory in the directory where the Hello package directory is located. That is, it and the Hello folder are in a folder.
Env.Load (New FileInputStream ("config.properties");
// Get a naming context
System.out.println (ENV);
InitialContext CTX = new javax.naming.initialcontext (env); System.out.Println ("got context");
// Get a reference to the intend bean
// jboss default jndi named EJB-JAR.XML: EJB-NAME
Object ref = ctx.lookup ("Hello");
System.out.println ("Got Reference");
// Get a reference from this to the bean "S Home Interface
HelloHome Home = (HelloHome) PortableRemoteObject.narrow (Ref, HelloHome.Class);
// Create an Hello Object from the home interface
Hello Hello = Home.create ();
// Call the Hello () Method
System.out.println (Hello.Hello ());
} catch (Exception E)
{
System.out.println (E.TOString ());
}
}
}
Then we also need to increase the JNDI profile required for this client program run. Methods as below:
Enter: c: / jboss / myproject / ejb / client directory, create a new config.properties file with Notepad, the content is as follows:
Java.naming.factory.initial = org.jnp.interfaces.namingContextFactory
Java.naming.factory.url.pkgs = org.jboss.naming: Org.jnp.interfaces
Java.naming.Provider.URL = jnp: //192.168.10.114: 1099
6. Write the configuration file of the server side,
First enter the c: /jboss/myproject/jsp/hello.nwar/web-inf directory, create a web.xml file with Notepad, configure the servlet, the content is as follows:
XML Version = "1.0" encoding = "GB2312"?>
servlet>
servlet-maping>
servlet-maping>
web-app>
Then enter the c: /jboss/myproject/ejb/hello.jar/meta-inf directory, create an ejb-jar.xml file, the content is as follows:
XML Version = "1.0" encoding = "GB2312"?>
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
session>
enterprise-beans>
ejb-jar>
In this directory, build a jboss-service.xml file, the content is as follows:
XML Version = "1.0" encoding = "GB2312"?>
session>
jboss>
Seven, until this, we write all the test files and configuration files.
8. Below we write scripts that compile these class files, enter the C: / jboss / bin directory, create a new com.bat file, as follows:
Set classpath = .; c: /j2sdk1.4.1_01/lib/tools.jar; c: /jboss/client/jbossall-client.jar; c: /jboss/client/jboss-client.jar; c: / jboss / Client / jboss-iiop-client.jar; c: /jboss/server/default/lib/jnpserver.jar; c: /jboss/client/jnp-client.jar; c: /jboss/client/log4j.jar; c : /Jboss/client/jboss-j2ee.jar; c: /jboss/server/all/lib/javax.servlet.jar
Javac% 1
Similarly, create a new runclient.bat in this directory for running the client program, as follows:
Set classpath = .; c: /j2sdk1.4.1_01/lib/tools.jar; c: /jboss/client/jbossall-client.jar; c: /jboss/client/jboss-client.jar; c: / jboss / Client / jboss-iiop-client.jar; c: /jboss/server/default/lib/jnpserver.jar; c: /jboss/client/jnp-client.jar; c: /jboss/client/log4j.jar; c : /Jboss/client/jboss-j2ee.jar; c: /jboss/server/all/lib/javax.servlet.jar
Java% 1
V. Compile and run the test program 1, enter the C: / JBoss / MyProject / src directory, run:
com * .java
A series of .CLASS files will be generated.
2, copy the resulting helloworld.class and helloservlet.class to the c: /jboss/myproject/jsp/hello.nwear/web-inf/classes/hello directory.
3, put the resulting hellohome.class and helloclient.class and hello.class files into the C: / jboss / myproject / ejb /client / hello / ejb directory
4. Add Hello.class and HelloBean.class and HelloHome.Class and HellolocalHome.Class and HellolocalHome.class to the c: /jboss/myproject/ejb/hello.jar/hello/eb directory.
At this point, we have completed all kinds of compilation and put them in the appropriate place, so far, the file and directory distribution structure is shown below: At this time, we will use the hello.jar directory in the EJB directory in MyProject. Copy to the Hello.War directory under JSP to
C: / jboss / server / all / deploy.
At this time, the workbench that starts JBoss will automatically publish this two directory. We can test it.
Start JBoss:
Run.bat -c all
Use IE to open http: // localhost: 8080 / hello / index.jsp test JSP file with IE HTTP: // localhost: 8080 / hello / helloWorld test Server
Use IE to open http: // localhost: 8080 / hello / helloservlet test Servlet call EJB
Then enter
C: / JBoss / MyProject / EJB / Client Operation:
Runclient Hello / EJB / HelloClient
-------------------------------------------------- ---------------------------