Many people know J2EE, but how many people really know its meaning? Simply put, J2EE is a business application system model or a specification that includes Java Remote Method Call (RMI), Enterprise JavaBean (EJB), Java Message Service (JMS), Java Server Page (JSP), etc. Wait. With J2EE resources, we can easily and quickly create a powerful enterprise application, such as telecommunications toll system, business knowledge management system, etc. You may want to develop such a powerful system, do you need a lot of investment? In fact, we can find free or even open source components to find existing networks, and assemble them into an available system. Our goal is to start a family! Of course, you have to buy a computer, you have to pay yourself: This article is the following components as an example to build our J2EE system. Please pay attention to the version number of each component, otherwise there may be compatible problems between components: the underlying database with mysql3.23.35, EJB and web server JBoss3.2.3, and the development environment we use the most popular Eclipse2.1 Lomboz2.1.3. Let's first configure the various components required to develop J2EE, and actually create an entity EJB to call the content in the underlying database, and display it with a JSP page. First, install and configure it 1. First we need it. Sun's JDK, go to the Sun's website (http://java.sun.com/) to see, find the latest JDK, download it back. Then release it into a convenient directory, such as d: /worktools/jdk1.4. Right click "My Computer" to select "Advanced" -> Environment Variable "as shown in Figure 1:
(Figure 1)
The environment variable is divided into two parts: the above is the user variable area; the following is the system variable area. In the user variable area, we create a variable named Java_Home, pointing to the JDK's directory, such as d: /worktools/jdk1.4, below we use% Java_home to refer to. Newly built a ClassPath value% java_home / lib;% java_home / Tools.jar; in the system variable area, we modify the environment variable of Path, add a value of% java_home / bin, middle ";" separated. OK, all the way to determine it, and we have set all the basics of Java development - the settings of JDK. (This article is WinXP as an example. If you use Win9x system, you need to restart your computer.) 2. Almost all enterprise applications require data storage systems, we will also use a famous open source database: mysql. The version used in this article is mysql3.23.35, you can go to MySQL website (http://www.mysql.com) to download. Install MySQL is very simple, unwounded the downloaded compressed package, "setup.exe", all the way "next" can be customized, it is recommended to install it in C: / MySQL. After installation, there is a program called "Winmysqladmin.exe" in MySQL / BIN. After running this program, enter your username and password (you set up), MySQL is already available. Now you can use its default username "root" password for empty manipulating database. 3. There are generally two ways to operate the database, one is a console; the other is a graphical operation. Simple, we choose the latter way. The choice of software is mysql-control-center0.9.4, which is also mySQL Group, the biggest advantage is free! After we download and install MySQL-CC on the MySQL website, we generate a shortcut on the desktop. We have to enter the MySQL-CC, the user name "root" password is empty, the default port number is "3306", the hostname is added to "localhost". After confirming, the right should be selected. Now we can manipulate your MySQL database. (Figure 2) Click to prevent
The purpose of using EJB is to manipulate the underlying data in an object, so we must first establish your own underlying data. As shown in Figure 3, right click on "Databases" on the left of MySQL-CC, select "New", named "MyTest".
(Figure 3)
On the newly created database, we are building a data sheet. As shown in Figure 4, add two fields "ID", "context", type "varchar", and name "myejb" when saved.
(Figure 4)
Open a few data in the data sheet just built, "ID" is 1, 2, 3; "Context" and "ID" should be "Time", "Blank", "Talk". 4. It is the time our application server JBoss appeared. Download JBoss3.2.3 from JBoss SourceForge subprojects (http://sourceforge.net/projects/jboss/). Differently extract it into a folder, such as D: /Worktools/JBoss3.2.3. Copy the mysql_ds.xml under / DOCS / EXAMPLES / JCA to / Server / Default / Deploy, use Notepad to open the editing. There are several modifications as shown in Figure 5: (Figure 5)
After saving, rename it "MyTest-DS.xml", which is the data source we want to use when writing EJB. Connect our MySQL database by JBoss, you need to use the MySQL Java driver. We still have to download Mysql-Connector-Java-3.0.11-Stable on MySQL homepage. Unzip this compressed file package, extract mysql-connection-java-3.0.11-stable-bin.jar, put it in the / server / default / lib directory under the JBoss directory, and rename this file mysql. JAR. 5. At this point, we can develop actually. Maybe you will ask, what to use to edit our program, is it difficult to bring a notepad with Windows? of course can! But various cumbersome, repetitive operations are very disadvantageous for our newcomers. What we need is an integrated development tool (IED) to help you handle some low-level operations, quickly prepare an available system, improve our confidence in J2EE. After the more in-depth understanding of J2EE understands, we will return to write each required file with a text editing tool, then compile, deploy, and quickly improve your level. Here I recommend a good development tool to everyone, which is the eclipse mentioned earlier. In fact, it itself can imagine a very friendly framework to achieve our various development needs by installing a variety of compatible plugins. You have to know that its background can be IBM, but for us more importantly, it is completely free, we can even develop its own plugin! Download Eclipse2.1 from the Eclipse home page (http://www.eclipse.org), then download Lomboz2.1.3 to www.objectlearn.org, which is a powerful Eclipse plugin developed in the J2EE process. First decompress Eclipse and Lomboz, copy the com.objectLearn.jdt.j2ee and com.Objectors generated by the Lomboz compression package to the Eclipse primary / PLUGINS folder. Run Eclipse, select "Windos" -> "Customize Perspective ...". As shown in Figure 6:
Click to defense
Expand all nodes, select all options related to Lomboz, then click "OK". (Figure 6) Select "Window" -> "preference". Click Lomboz, select /LIB/Tools.ja under your JDK directory in JDK Tolls .jar, click Apply. Because our application servers choose JBoss that supports heat deployment, make sure the Restart Server Instance option is not selected. As shown in Figure 7, the Lomboz option is expanded on the left, selecting Server Definition. In Server Types, we choose JBoss 3.2.x. The same selection of JBoss's primary directories in Application Server DirectoTY and ClassPath Variable, and click Apply. (Figure 7) Click to enlarge
Still in "Window" -> "preference", expand the Java class, select New Project we will change it into folders in Source and Output Folder, as shown in Figure 8.
(Figure 8)
Click Apply and finally click OK. Page ~ Gas, so I have installed all the software needed to develop J2EE systems. Drink cups, develop our first EJB! Second, the development of the first EJB EJB2.0 specification defined three kinds of enterprise beans. Session beans, entity beans, and messaging beans (Message Driven Beans). Session bean simulates a business process; entity bean simulates business data; and the message-driven bean is similar to the meeting of a business process, but the message driver bean accepts only the call 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 after understanding the entity bean. The message driver bean is not late when used. 1. In Eclipse, run "file" -> "new" -> "Project" Select Lomboz J2EE Project. Fill in our project name "EJBTEST" in Project Name. After clicking Next, the Java Setting dialog will appear, we don't make any modifications, directly next. In this step, we will establish an EJB module. As shown in Figure Nine, in the EJB Module tab, we create a new EJB Module named myejb.
(Figure 9)
Then in TARGENT Server, choose our JBoss3.2.x that we just configured. After adding, I choose Finish, and our project is established. 2. Now use 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 in Package, such as "rip.eb.cmp"; in Name, we give this EJB a name, such as "mytest". In the SELECT EJB TYPE option, select "Container Management Entity EJB", indicating that you want to create an entity EJB managed by the container. As shown in Figure 11 (Figure 11)
In this step we have to declare the data table name, data source name, and various fields in the table. Refer to Figure 12:
(Figure 12)
Fill in the contents in the appropriate place. Note that each additional field is clicked once, put it in the list below. Finally, select the text, click on the make primary key, mark it as a close. Among them, the table name corresponds to the data sheet MYEJB established in MySQL; DataSource corresponds to the data source mytest-ds.xml set in JBoss. After clicking Finish, you can find and open our just generated MyTestBean.java in the left SRC directory (Mytest's bean is automatically added) file. Find the EJBCREATE () method, add two string type input variables "ID" and "Context, and add two methods setId (ID) and setContext (context), the specific implementation is shown in Figure 13:
(Figure 13) Click to enlarge
If you appear in the change, you can add a package or capture an exception, click Error symbol, select loading or throw an exception. This newly established EJB needs to be put into one module to be loaded, executed. As shown in Figure 14:
(Figure 14) Click to enlarge
Right click MyTestBean.java, select Lomboz J2EE. . . -> add ejb to module, then tick before the MYEJB established, and then click OK, it is loaded. A complete entity EJB is combined by a plurality of different functional files, but here they are automatically generated by adding tags. What is XDoclet technology, interested readers can go online to search, there are a lot of related introductions, we will no longer introduce specific details. As shown in Figure 15: Right click on the MYEJB module, select Lomboz J2EE. . . -> Generate EJB Classes.
(Figure 15) Click to enlarge
After successful, you can see MyTestHome in the rip.ejb.cmp package under the EJBSRC directory. Six documents such as Java. There are many XML files in the META-INF folder under this MYTEST module. Among them, JBoss. XML, JBossCMP-JDBC. XML and EJB-JAR. XML is the three most critical configuration files deploying EJB. And we need to change this file that is just jbosscmp-dbc.xml, which describes the description of the data source, and other files have been configured by Lomboz. We modified the relevant part of this file to:
<% @ 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"%>
* / Collection col = home.findall (); string outstring = ""; / * uses a loop to traverse all data and print them out * / itrator it = col.iterator (); whil (it.hasnext) )) {MyTest Element = (mytest) it.next (); outstring = element.getID () ": Element.getContext () "
"; out.println (outstring);} / * capture And throw a given abnormality * /} catch (remoteException e) {E.PrintStackTrace ();} catch (cretestacktrace ();} catch (FINDEREXCEPTION E) {E.PrintStackTrace ();}%> Center> body> html> 3. Right click on the MYWEB module and select Lomboz J2EE. . . -> Deploy Module Loads this web module into the server. 4. Open the browser and enter "http: // localhost: 8080 / myweb / index.jsp" in the address bar. If everything goes well, you will be able to see the page output as shown in Figure 6. (Figure 6) Click to enlarge
At this point, we ended this time for J2EE. Fourth, conclude the full text, truly the effort of the EJB development, only a few lines of code, so EJB is very suitable for the rapid development of the application system. However, EJB is a heavyweight application since the birth, especially using remote calls (for illustrative problems, this paper is this method), a large number of resources used the system. So in general, the use of local interfaces can greatly reduce the use of resources. Alternatively, you can also use other O-R mapping components, such as Hibernate, and more. Due to limited space, the author cannot list all possible errors and solutions. If there is any opinions or problems, please send email to my mailbox chesheng913@yahoo.com.cn.