Develop CMP

xiaoxiao2021-03-05  31

The last article has developed a STATELESS SESSION bean, which introduces the development of CMP.

The establishment of the web test module is no longer repeated. Ant scripts do not need to be modified. On the basis of the previous example, we can directly Code CMP.

step:

1 Built a database named mydb in PointBase, build a User table, there are ID, name, and agnus in the table.

2 Create a CMP called Userbean, and there is a three fields in the bean with UserID, Name, AGE.

3 Edit the bean and modify the deployment descriptor

4 compile, deploy

5 Write JSP for testing

Now, Let's Go!

1 In PointBase

In this example we use WebLogic's own PointBase database, if you want to know information about the configuration of PointBase, please refer to my other post - "PointBase Getting Started"

Position to the PointBase folder. It is usually located in:

/ weblogic81 / common / evAl / PointBase

Enter the Tools directory, start the StartPointBase.cmd script. Then you will see a CMD window that pops up. When you display the database port information, you will get started (very fast, less than 1 second)

Then start the StartPointBaseConsole.cmd script, a dialog box will pop up.

JDBC Driver: No modification

URL: Enter JDBC: PointBase: Server: // localhost / mydb

User: Enter PBPUBLIC

Password: Enter PBPUBLIC

There are three options below, select "CREATE New Database" in the middle, then click OK.

OK, the MYDB database creates success.

Next we create a table. You can use the script to get the Wizard using the script.

Create Table User

ID Integer Not Null,

Name varchar (30),

Age INTEGER,

ConsTRAINT PK_ID PK_ID PRIMARY Key (ID)

);

Then insert three data:

INSERT INTO User (ID, Name, Age) Values ​​(1, 'Diegoyun', 25);

INSERT INTO User (ID, Name, Age) Values ​​(2, 'Houqi', 25);

INSERT INTO User (ID, Name, Age) Values ​​(3, 'Zhihua ", 26);

At this point, the database preparation is completed.

2 Create EJB

New Entity packages under com.diegoyun.ejb.

In Idea, create a new CMP, pop-up EJB's properties box, this and sessionbean, the property settings are as follows:

: Enter User

Package: com.diegoyun.ejb.entity

Primary Key Class: Change to Java.lang.integer

For the sake of simplicity, our CMP here uses the Remote interface, so other information do not have to be modified, point OK

Then the CMP property setting box will pop up, and make the following settings:

Display name: Userbean

Add CMP Field:

Click on this box, add three fields, and make the following settings: UserId Integer PK

Name string

Age Integer

Then switch to the WebLogic Server property page, set the JNDI name: EJB / ENTITY / Userbean

Many properties will also be seen in this property page, this example is not set first.

Close the property page.

In this way, CMP and most of the configuration information including the Remote interface have been created.

You can see the three files of User, Userbean, and UserHome in com.diegoyun.entity, and the contents of the three configuration files in Meta-INF have changed.

3 Edit the bean and modify the deployment descriptor

Here we add a few ways to the Home interface.

Open the UserHome file, add the following method:

Public Collection Findbyage (Integer Age) Throws RemoteException, FINDEREXCEPTION;

Public User Create (Integer Userid, String Name, Integer Age) Throws RemoteException, CreateException;

Then open the Userbean file, add:

Public Integer Ejbcreate (Integer Userid, String Name, Integer Age) throws createException {

SetUserID (userID);

SetName (Name);

Setage (agn);

Return UserId;

}

Public void ejbpostcreate (Integer Userid, String Name, Integer Age) {

}

And add a private field:

Private EntityContext context;

Modify the settentContext and unsetentityContext methods are as follows:

Public void setentityContext (EntityContext EntityContext) THROWS EJBEXCEPTION {

THIS.CONTEXT = EntityContext;

}

Public void unsentityContext () throws ejbexception {

THIS.CONTEXT = NULL;

}

OK, source code Code finish.

Next, modify the deployment descriptor.

Open EJB-JAR.XML

Just now, we define a Finder method in UserHome, you must define it:

There is another line under Userid , plus:

Findbyage

java.lang.integer

Then open WebLogic-Cmp-RDBMS-JAR.XML, modified as follows:

'- // bea systems, Inc.//dtd WebLogic 7.0.0 EJB RDBMS PERSISTENCE // En' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd' >

Userajb

EJBSAMPLES-DS

User

Userid

id

Name

name

Age

age

disabled

Next, open WebLogic-ejb-jar.xml, modified as follows:

Userajb

WebLogic_cmp_rdbms

7.0

meta-inf / weblogic-cmp-rdbms-jar.xml

EJB / ENTITY / Userbean

HelloWorldejb

ejb / session / helloworld

OK, CMP development work has been completed.

4 compile, deploy

Use the original Build script.

Restart the server and log in to the management interface.

Create a ConNetion Pool:

Database Type: Select PointBase

Database driver: Select PointBase's Driver (Type 4) Version: 4.x

Then point Continue

In this interface, enter:

Name: EJBSAMPLESPOOL

Database Name: Enter myDB

Database User Name: Enter PBPUBLIC

Password: Enter PBPUBLIC

Then point Continue. The test connection can then be tested, if there is no accident, Connectin Pool creates complete.

Create Data Source:

Name and JNDI Name Enter: EJBSAMPLES-DS

Pool Select EJBSamplesPool

Deploy EJB:

About Deploy, I said before, if you use UPLOAD DEPLOY, you don't need to re-deploy EJB. But because this CMP is new, you still re-deploy, you can find a potential error when WebLogic is in Deploy.

Delete the original EJB, re-deploy EJB, it should be in:

/ user_projects / domains / mydomain / myserver / upload directory

OK, the deployment work is completed.

5 Write JSP for testing

New four files under Web are as follows:

Index.html (navigation role)

Simple CMP EXAMPLE - User </ Title></p> <p></ hEAD></p> <p><body></p> <p><center></p> <p><H1> Simple CMP EXAMPLE - User </ h1></p> <p><a href="createuser.jsp"> Create New User </A></p> <p><p></p> <p><a href="searchuser.jsp"> search user by id </A></p> <p><p></p> <p><a href="searchuserbyage.jsp"> Search user by age </A></p> <p><p></p> <p></ center></p> <p></ body></p> <p></ html></p> <p>CreateUser.jsp</p> <p><% @ page language = "java"%></p> <p><% @ page import = "java.util.arraylist"%></p> <p><% @ Page Import = "java.util.collection"%></p> <p><% @ page import = "javax.ejb.objectNotFoundException"%></p> <p><% @ Page Import = "javax.naming.context"%></p> <p><% @ page import = "javax.naming.initialcontext"%></p> <p><% @ Page Import = "javax.naming.namingexception"%></p> <p><% @ Page Import = "com.diegoyun.ejb.entity.user"%></p> <p><% @ page import = "com.diegoyun.ejb.entity.userhome"%></p> <p><% @ Page Import = "javax.rmi.portableremoteObject"%></p> <p><% @ Page Import = 'Java.util. *'%></p> <p><html></p> <p><head> <title> cmp_deemo </ title> </ head></p> <p><body bgcolor = "white"></p> <p><center></p> <p><H2> cmp_demo_title </ h2></p> <p>CREATE User:</p> <p><p></p> <p><form method = "post" Action = "createuser.jsp"></p> <p><Table Border = 10></p> <p><tr></p> <p><TD> UserId: </ TD></p> <p><TD> <input type = "text" name = "userid" size = "11" value = "> </ td></p> <p></ TR></p> <p><tr></p> <p><TD> Name: </ TD></p> <p><TD> <input type = "text" name = "name" size = "25" value = "> </ td></p> <p></ TR></p> <p><tr></p> <p><TD> AGE: </ TD></p> <p><TD> <Input Type = "text" name = "age" size = "25" value = "> </ td></p> <p></ TR></p> <p></ TABLE></p> <p><p></p> <p><Input Type = "Submit" name = "submit" value = "submit"></p> <p><p></p> <p></ form> <%</p> <p>String Tid = Request.getParameter ("UserID");</p> <p>String name = Request.getParameter ("name");</p> <p>String TAGE = Request.getParameter ("age");</p> <p>Integer userid = NULL;</p> <p>Integer Age = NULL;</p> <p>IF (TID! = NULL &&! "" "" "Equals (TID))</p> <p>Userid = INTEGER.VALUEOF (TID);</p> <p>IF (TAGE! = NULL &&! "" "" Equals (TAGE))</p> <p>AGE = Integer.Valueof (TAGE);</p> <p>IF (userid! = null&&! "" "" Equals (userid) && name! = null&&! "" Equals (name) && age! = null&&! "". Equals (age) {</p> <p>Try {</p> <p>InitialContext IC = New InitialContext ();</p> <p>Object o = Ic.lookup ("EJB / Entity / Userbean);</p> <p>UserHome Home = (userHome) O;</p> <p>User = home.create (userid, name, age);</p> <p>%></p> <p>New user [<% = userid%>] created !!:</p> <p><%</p> <p>} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>Out.println ("CREATE User Failed:" E.TOString ());</p> <p>}</p> <p>}</p> <p>%></p> <p><hr></p> <p>[<a href="index.html"> Home </a>]</p> <p></ center></p> <p></ body></p> <p></ html></p> <p>Searchuser.jsp</p> <p><% @ page language = "java"%></p> <p><% @ page import = "java.util.arraylist"%></p> <p><% @ Page Import = "java.util.collection"%></p> <p><% @ page import = "javax.ejb.objectNotFoundException"%></p> <p><% @ Page Import = "javax.naming.context"%></p> <p><% @ page import = "javax.naming.initialcontext"%></p> <p><% @ Page Import = "javax.naming.namingexception"%></p> <p><% @ Page Import = "com.diegoyun.ejb.entity.user"%></p> <p><% @ page import = "com.diegoyun.ejb.entity.userhome"%></p> <p><% @ page import = 'java.util. *'%> <html></p> <p><head> <title> cmp_demo_title </ title> </ head></p> <p><body bgcolor = "white"></p> <p><center></p> <p><H2> cmp_demo_title </ h2></p> <p>Search by ID:</p> <p><p></p> <p><form method = "get" action = "searchuser.jsp"></p> <p><Input Type = "text" name = "searchtetext" size = "25"></p> <p><p></p> <p><Input Type = "Submit" value = "Search" /></p> <p></ form></p> <p><%</p> <p>String text = Request.getParameter ("SearchText");</p> <p>Integer ID = null;</p> <p>IF (Text! = Null &&! "" "" Equals (text))</p> <p>ID = Integer.Valueof (text);</p> <p>User User = NULL;</p> <p>IF (ID! = null &&! "" "" "Equals (ID)) {</p> <p>Try {</p> <p>String URL = "T3: // localhost: 7001";</p> <p>Hashtable h = new hashtable ();</p> <p>H.PUT (Context.Initial_Context_Factory, "WebLogic.jndi.wlinitialContextFactory";</p> <p>H.PUT (Context.Provider_URL, URL);</p> <p>Context IC = New InitialContext (h);</p> <p>Object o = Ic.lookup ("EJB / Entity / Userbean);</p> <p>UserHome Home = (userHome) O;</p> <p>Try {</p> <p>User = home.findbyprimaryKey (ID);</p> <p>} catch (ObjectNotFoundException EX) {</p> <p>// throw new exception (ex);</p> <p>}</p> <p>%></p> <p>Results: <p></p> <p><%</p> <p>IF (user! = null) {</p> <p>%></p> <p>User [<% = User.getUserId ()%>]:</p> <p><% = User.getname ()%>, <% = user.getage ()%></p> <p><p></p> <p><%</p> <p>} else {</p> <p>%></p> <p>User [<% = id%>] not found.</p> <p><%</p> <p>}</p> <p>} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>Out.println (E.TOString ());</p> <p>}</p> <p>}</p> <p>%></p> <p><hr></p> <p>[<a href="index.html"> Home </a>]</p> <p></ center></p> <p></ body></p> <p></ html></p> <p>Searchuserbyage.jsp</p> <p><% @ page language = "java"%> <% @ page import = "java.util.arrayList"%></p> <p><% @ Page Import = "java.util.collection"%></p> <p><% @ page import = "javax.ejb.objectNotFoundException"%></p> <p><% @ Page Import = "javax.naming.context"%></p> <p><% @ page import = "javax.naming.initialcontext"%></p> <p><% @ Page Import = "javax.naming.namingexception"%></p> <p><% @ Page Import = "com.diegoyun.ejb.entity.user"%></p> <p><% @ page import = "com.diegoyun.ejb.entity.userhome"%></p> <p><% @ Page Import = "javax.rmi.portableremoteObject"%></p> <p><% @ Page Import = 'Java.util. *'%></p> <p><html></p> <p><head> <title> cmp_demo_title </ title> </ head></p> <p><body bgcolor = "white"></p> <p><center></p> <p><H2> cmp_demo_title </ h2></p> <p>Search by agge:</p> <p><p></p> <p><form method = "get" action = "searchuserbyage.jsp"></p> <p><Input Type = "text" name = "searchtetext" size = "25"></p> <p><p></p> <p><Input Type = "Submit" value = "Search" /></p> <p></ form></p> <p><%</p> <p>String text = Request.getParameter ("SearchText");</p> <p>Integer ID = null;</p> <p>IF (Text! = Null &&! "" "" Equals (text))</p> <p>ID = Integer.Valueof (text);</p> <p>User User = NULL;</p> <p>Collection userList = null;</p> <p>IF (ID! = null &&! "" "" "Equals (ID)) {</p> <p>Try {</p> <p>String URL = "T3: // localhost: 7001";</p> <p>Hashtable h = new hashtable ();</p> <p>H.PUT (Context.Initial_Context_Factory, "WebLogic.jndi.wlinitialContextFactory";</p> <p>H.PUT (Context.Provider_URL, URL);</p> <p>Context IC = New InitialContext (h); Object O = Ic.lookup ("EJB / Entity / Userbean);</p> <p>UserHome Home = (userHome) O;</p> <p>Try {</p> <p>UserList = home.findbyage (id);</p> <p>} catch (ObjectNotFoundException EX) {</p> <p>// throw new exception (ex);</p> <p>}</p> <p>%></p> <p>Results: <p></p> <p><%</p> <p>IF (userlist! = null&&! userlist.isempty ()) {</p> <p>ITERATOR IT = UserList.ITerator (); it.hasnext ();) {</p> <p>User = (user) PortableRemoteObject.narrow (it.next (), user.class;</p> <p>%></p> <p>User: ID [<% = User.getuserid ()%>] <br></p> <p>Name: <% = User.getName ()%> <br></p> <p>Age: <% = user.getage ()%> <br></p> <p><%</p> <p>IF (it.hasnext ())</p> <p>%> <P></p> <p><%;</p> <p>}</p> <p>} else {</p> <p>%></p> <p>User NOT FOUND.</p> <p><%</p> <p>}</p> <p>} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>Out.println (E.TOString ());</p> <p>}</p> <p>}</p> <p>%></p> <p><hr></p> <p>[<a href="index.html"> Home </a>]</p> <p></ center></p> <p></ body></p> <p></ html></p> <p>Open in the browser:</p> <p>Http: // localhost: 7001 / Web / INDEX.HTML</p> <p>Enjoy your own results :)</p> <p>Diegoyun at 4/20/2005 12:20:45 AM</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-34111.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="34111" 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.035</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 = 'aiAjMUDrbDZ9RqD_2BfpYF_2FR0Tb8dOzVkylyVLtJmDYJz5_2BlbfjiuS3Pa_2FMLUwxD4cRmNYU0sWgpN3vrFuS8wTgA_3D_3D'; 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>