Beginner's Guide Java experts start Diagram 2

Source: Internet
Author: User
Tags add key mysql string table name jboss
J2ee second, the development of the first EJB

3 enterprise beans are defined in the EJB2.0 specification. Sessions beans (session beans), entity beans (Entity beans), and message-driven beans (messages driven beans), respectively. The session bean simulates a business process, the entity bean simulates business data, and the message-driven bean session bean resembles a business process, but the message-driven bean accepts only the invocation of the message.

This article focuses on how to quickly develop an entity bean. Because the session bean is relatively simple, it is easy to write when you understand the entity bean. Message-driven beans are not too late to learn when they are used.

1. In Eclipse, run "file"-> "new"-> "project" to choose Lomboz Java Project. In Project name, fill in our project name "Ejbtest". When you click Next, the Java Setting dialog box appears, and we do not make any changes, directly next. In this step, we will build an EJB module. As shown in figure nine, in the EJB Module tab, we create a new EJB module named MYEJB.


(Figure Nine)

Then in Targent server, select the jboss3.2.x that we just configured. Add later choose Finish, we will build the project.

2. Now using the Lomboz Wizard to generate our EJB, select "File"-> "new"-> "Lomboz EJB creation Wizard". As shown in Figure 10:


(Figure 10)

Fill in the package name in the package, such as "Rip.ejb.cmp"; in name, we give the EJB a name, such as "mytest". Select EJB type option, choose "Container Managed Entity EJB" to indicate that you want to create an entity EJB that is managed by the container. As shown in Figure 11


(Figure 11)

In this step we declare the data table name, the name of the data source, and the fields in the table. Refer to Figure 12:


Fill in the contents in the appropriate place. Notice that every time you add a field, click Add once, and put it in the list of joined fields below. Finally select the ID this field, click Make Primary Key, mark it as the main health. Where the table name item corresponds to the data table we previously established in MySQL Myejb;datasource corresponds to the data source we set in JBoss Mytest-ds.xml.

After clicking Finish, you can find and open the Mytestbean.java (mytest) file that we just generated in the RIP.EJB.CMP package in the left SRC directory. Locate the Ejbcreate () method, add two string input variable "IDs" and "context", and add two methods SetID (IDs) and SetContext (in), as shown in Figure 13:


(Figure 13) Click to enlarge


If there is an error loading the package or catching an exception during the change, click the error symbol, select load or throw the exception directly.

The newly created EJB needs to be placed in a module to be loaded and executed. As shown in Figure 14:


(Figure 14) Click to enlarge


Right click Mytestbean.java, choose Lomboz java ... ->add EJB to module, and then in the earlier set up MYEJB This module tick, click OK, then realize the load. A complete entity EJB is composed of several different function files, but they are generated automatically by tagged Mytestbean.java. The use of Xdoclet technology, interested readers can go to the Internet to search, there are a lot of related introductions, we will no longer introduce specific details. As shown in Figure 15: Right click MYEJB This module, choose Lomboz java ... ->generate EJB Classes.


(Figure 15) Click to enlarge


After the display is successful, you can see the mytesthome in the RIP.EJB.CMP package in the EJBSRC directory. Java, and so on six files. There are many XML files in the Meta-inf folder below the MyTest module. of which, JBoss. XML, Jbosscmp-jdbc. XML and Ejb-jar. XML is the three most critical configuration file for deploying EJBS. What we need to change at this stage is just jbosscmp-dbc.xml this file, which describes the description of the data source, and other files are already configured by Lomboz. We modify the relevant part of this file to:




Java:/mytestds
MySQL
Foreign-key


Other parts do not change. So far, we've done all the EJB development work. Right-click MYEJB This module, choose Lomboz java ... ->deploy module loads the modules into the server. Then, choose Lomboz-ee ... ->debug server runs servers. Everything is all that is owed to call!

  Iii. EJB invocation-using JSP to display data 

Now we will use JSP as the business layer and the presentation layer to invoke the EJB, store some data in the database, and print all the data already in the database to the client's browser.

1. As described above, we will build a web module called "MYEJB". Target server chooses our configured jboss3.2.x and will generate a myweb directory.

2. Open the MyWeb directory and replace the following code with the original code of INDEX.JSP.

<%@ page import= "javax.ejb.*"%>
<%@ page import= "javax.naming.*"%>
<%@ page import= "javax.ejb.*"%>
<%@ page import= "rip.ejb.cmp.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "Java.rmi.RemoteException"%>

<title> Welcome Every Body,this is i firstejb for QiQi </title>
<body>
<center> <%
/* Initializes the context of the system and looks for the entity named Mytestbean Jidi bean*/
InitialContext context = new InitialContext (System.getproperties ());
Mytesthome home = (mytesthome) context.lookup ("Mytestbean");

try {
/* Call the EJB Create () method to generate data in the database, you do not need to invoke the Create () method if you do not need to invoke EJB to generate data in the underlying database.
MyTest Mybean = Home.create ("A", "Hello,i am Rip");
/* According to the previous definition, the FindAll () method returns all the data in the MYEJB table, and if you want to use a different find method, such as to return id<5 data, you can write your own finder method. */

Collection col = Home.findall ();
String outstring = "";

/* Use loops to iterate through all the data and print them out * *
Iterator it = Col.iterator ();
while (It.hasnext ()) {

mytest element = (mytest) it.next ();
outstring = Element.getid () + ":" +element.getcontext () + "<br>";
Out.println (outstring);

}

/* catch and throw to the middle exception * *
catch (RemoteException e) {
E.printstacktrace ();
catch (CreateException e) {
E.printstacktrace ();
catch (Finderexception e) {
E.printstacktrace ();
}

%> </center>
</body>

3. Right-click MyWeb This module, choose Lomboz java ... ->deploy module mounts This web module to the server.

4. Open the browser and type "Http://localhost:8080/myWEB/index" in the Address bar. JSP ". If all goes well, you will see the page output as shown in Figure 16.


(Figure 16) Click to enlarge

At this point, we end the introduction to the Java EE learning.

   Iv. Concluding remarks

In the full text, the real development of the EJB to spend less effort, just a few lines of code, so EJB is very suitable for the rapid development of application systems. However, EJB has been a heavyweight application since its inception, especially with remote invocation (this is the method used in this article to illustrate the problem), using a large amount of resources from the system. Therefore, in general, the use of local interfaces can greatly reduce the use of resources. Alternatively, you can use other o-r mapping components, such as Hibernate, and so on.

Due to the limited space, the author can not be all possible errors and solutions enumerated, if any comments or questions, please send email to my email kknd0669@sina.com.

Previous page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.