One of JBoss Development Practices in Win2K Environment: Initial Basic Development Environment Configuration and Test Description: This section is for EJB development beginners, mainly to manually configure a basic text-based environment, so that beginners will start from the beginning Class programs and profiles to understand the step gathering and principles of EJB development and deployment. This is a summary of the author in learning EJB development, welcome to correct. Author Information: Josserchai@yahoo.com 1. Demand Software List and Download Address 1, Sun Java SDK, our version is: J2SDK1.4.1_01, download address is: http://www.sun.com/downloads /, The downloaded file name is: 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 step gathering, the folder is shown below: Third, configure the operating environment and test program directory 1, set the environment variable 1, set the Java_Home system variable: Under Win2k, right-click "on the desktop" My Computer, select Properties, and then click "Advanced" -> Environment Variable "on the pop-up page, and then click" New "under" System Variable "in the pop-up page, then" variable name in the pop-up window " (N) Enter "java_home", enter "C: /J2SDK1.4.1_01" in the "Variable value (V)" column. 2, set the same method: jboss_home value: C: / jboss 3, same The method adds the value of PATH to add: "c: /j2sdk1.4.1_01/bin; c: / jboss / bin" 2, create a program directory 1, create a MyProject directory under C: / jboss Used 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, the formation of the directory structure is shown below: IV: Writing 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 the JBoss configuration servers with all features. 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, create a new index.jsp file with Notepad, the content is as follows: Used to test the JSP program <% @ Page ContentType = "text / html; charset = GB2312 "%>
* / Public String hello () throws java.rmi.RemoteException;} Also a new HelloLocal.java in this directory file, to achieve Local interfaces, as follows: package hello.ejb; import javax.ejb.EJBLocalObject; public interface HelloLocal extends EjblocalObject {public string hello ();} Similarly, new HelloHome.java files are created 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;} Also in this directory create a HelloLocalHome.java file, LocalHome implement interfaces, as follows: package hello.ejb; import javax.ejb.EJBLocalHome; public interface HelloLocalHome extends EJBLocalHome {HelloLocal create () throws javax.ejb.CreateException;} Similarly a new in this directory HelloBean .java, implement the EJB class, [By following: package hello.ejb; / ** * bean importation class for enterprise bean: hello * / public class hellobean imports javax.ejb.sessionBean {PR ivate javax.ejb.SessionContext mySessionCtx; / ** * 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 create by ejb!";}}
We have written all kinds of all kinds of classes needed for EJB classes. Let's write files for testing this EJB program. Specifically, there are two: one is to call EJB with client servlet, and the other is used The client class calls EJB. The implementation is as follows: 5. Programs that implement the test EJB enters the C: / jboss / myproject / src directory, create a new helloservlet.java file for testing EJB through the 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 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 Properties (); if (initCtxFactory = null) {env.put (Context.INITIAL_CONTEXT_FACTORY, initCtxFactory);}!! if (providerURL = 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 IOException {res.setContentType ( "text / html"); PrintWriter out = Res. Getwriter (); out.println ("
body> html> ");}} The same in this directory, new HelloClient.java file is created, used to test EJB through the client class, as follows: package hello.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 profment (); //config.properties file should be placed in the same-level directory in the directory where the Hello package directory is located. It and the Hello folder is 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 intert bean // jboss default jndi named EJB-JAR.XML: EJB-NAME Object Ref = CTX.lookup ("Hello"); System.out.Println ("got reason"); // 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.ord.println (Hello.Hello ());} catch (Exception E ) {System.out.println (e.tostring ());}}} then, we also need to add a JNDI profile required for this client program runtime.
The method is as follows: Enter: C: / jboss / myproject / ejb / client directory, use Notepad to create a new config.properties file, 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. Writing a server-side profile, first enter the c: /jboss/myproject/jsp/hello.nwar/web-inf directory, create a web with Notepad. XML file, used to configure servlet, as follows: XML Version = "1.0" encoding = "gb2312"?> - // Sun Microsystems, Inc.//dtd Enterprise JavaBeans 2.0 // En "" http://java.sun.com/dtd/ejb-jar_2_0.dtd "> 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/toLS. 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 / jnpser.jar; c: /jboss/client/jnp-client.jar; c: /jboss/client/clog4j.jar; c: /jboss/client/jboss-j2ee.jar; c: / jboss / Server / all / lib / javax.servlet.jar Javac% 1 Similarly, a new runclient.bat is used in this directory for running client programs, 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 5, compile and run the test program 1, enter the C: / jboss / myproject / src directory, run: com * .java will produce a series .CLASS file. 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 in the C: / JBoss / MyProject / EJB / Client / Hello / EJB directory 4, will result in hello.class and hellobean.class and hellohome. Class and Hellolocal.class and HellolocalHome.Class are placed in a c: /jboss/myproject/ejb/hello.jar/hello/ejb 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 C: / JBoss / Server / All / Deploy under JSP. At this time, the workbench that starts JBoss will automatically publish this two directory. We can test it.