J2EE development practices based on JBoss in the Win2K environment - - one of the initial underlying environment configuration a

zhaozj2021-02-16  49

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 "%> </ title> <meta http-equiv =" content-type "content =" text / html; charset = GB2312 "> </ head> <body bgcolor =" # fffff "Text =" # 000000 "TopMargin =" 5 "> <% string hello =" Hello World! this created by JSP file! "; Out.println (Hello);%> </ body> </ html> three, enter 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 IOException, ServletException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); out.println ( "<hr>" Out.println ("Hello World! This Is Created By Servlet!"); Out.println ("<HR>");}} To this, we have established JSP files and servlet classes for testing. HelloWorld's stateless SessionBean will be built below. 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.</p> <p>* / 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!";}}</p> <p>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.</p> <p>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 ("<html> <head> <title> the first ejb </ title> </ head>"); 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 ("</p> <p></ 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.</p> <p>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 <! - This line can also be replaced with java.naming.factory.url.pkgs = org.jboss.naming.client -> 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"?> <! doctype web-app public "- // sun microsystems, Inc.//dtd web application 2.3 /////////dtd Web Application 2.3 // En "" http://java.sun.com/dtd/web-app_2_3.dtd "> <web-app> <servlet> <servlet-name> HelloWorld </ servlet-name> <display-name> HelloWorld </ Display-name> <servlet-class> hello.HelloWorld </ servlet-class> </ servlet> <servlet> <servlet-name> helloservlet </ servlet-name> <display-name> helloservlet </ display-name> <Database & Sports> <Display-Name> < Servlet-Class> Hello.Helloservlet </ servlet-class> </ servlet> <servlet-maping> <servlet-name> HelloWorld </ servlet-name> <URL -pattern> / helloworld </ url-pattern> </ servlet-maping> <servlet-maping> <servlet-name> helloservlet </ servlet-name> <url-pattern> / helloservlet </ url-pattern> </ servlet -mapping> </ 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"?> <! doctype EJB-JAR PUBLIC</p> <p>- // Sun Microsystems, Inc.//dtd Enterprise JavaBeans 2.0 // En "" http://java.sun.com/dtd/ejb-jar_2_0.dtd "> <EJB-JAR> <Description> this is Hello EJB Example </ description> <display-name> Helloeejb </ display-name> <Enterprise-Beans> <session> <display-name> Hello </ display-name> <ejb-name> Hello </ ejb-name> < Home> Hello.ejb.hellohome </ home> <remote> Hello.ejb.hello </ remote> <local-home> hello.ejb.hellolocalhome </ local-home> <local> hello.ejb.hellolocal </ local > <EJB-Class> Hello.ejb.hellobean </ EJB-Class> <session-type> stateless </ session-type> <transaction-type> container </ transaction-type> </ 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"?> <jboss> <Enterprise-Beans> <session> <ejb-name> Hello </ ejb-name> <jndi-name> Hello </ jndi-name> </ session> <secure> true </ secure> </ enterprise-beans> <remrce-managers /> </ JBoss> Seven, until this, we write all the test files and configuration files.</p> <p>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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24528.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="24528" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.039</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ePTHBBSv5Yq_2FglqLcKNk3bn10c8MIcB1GNRRi7SC3NkjqWoQRaQqvwYyERjN76TUT48yGE_2Fpwj1_2Bq0nr'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>