Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Enterprise Java on Mac OS X

05/05/10 23:30

Search
Advanced Search Log In | Not a Member? ADC Home > Internet & Web > Java > Support

Enterprise Java with J2EE


Enterprise Java is a popular subject these days. An increasing number of web applications are being deployed using application servers which conform to the Java 2 Platform Enterprise Edition (J2EE) standard. J2EE adds a set of extensions to server-side Java which aid in writing distributed and multi-tier applications. If youre completely new to J2EE, you may want to review the pages at http://java.sun.com/j2ee/overview.html. The J2EE standard covers several technologies, including EJB (Enterprise JavaBeans), Servlets, JSP (JavaServer Pages), JNDI (Java Naming and Directory Interface), and JDBC (Java DataBase Connectivity). In this article, Ill try to give you all the information you need to start deploying and testing your J2EE applications on Mac OS X using the open-source servers JBoss and Jetty. JBoss is an open-source J2EE application server that claims to be the most downloaded J2EE web server in the world. The JBoss product is open source, but the developers encourage users to buy their documentation. There are several documentation options, all detailed at http://www.jboss.org/docs/index. This article covers only a tiny fraction of JBosss capabilities. If youre going to use JBoss, I strongly recommend buying the documentation. Jetty is an open-source HTTP server and Servlet container. Its available for download in a bundle with the JBoss server. You can also download JBoss with the popular open-source Tomcat server. The combination of JBoss with Jetty (or Tomcat) provides a complete J2EE container.

Installing and Configuring JBoss and Jetty on Mac OS X


Since Mac OS X 10.3 includes JBoss, these installation instructions are only relavent to non-server versions of Mac OS X. All versions of Mac OS X come with Java 2, so pure-Java applications like JBoss and Jetty can be rapidly deployed. First, get the current stable JBoss/Jetty bundle from http://www.jboss.org/downloads/index. Versions of JBoss after 3.0 have Jetty included by default. In this article, Ill be using JBoss-3.0.2.zip. Pick an installation location (I picked / u s r / l o c al/ j bos s ) and make sure you can write to the folder.

sh e ll > s ud o m kd i r / us r / l o c a l / jb o s s Pa s sw o r d : sh e ll > s ud o c ho w n l iz : s t a f f / us r / l o c a l /j bo s s

Then move the . zi p file to its installation base directory (if it isnt there already) and unzip it using the j a r command.
sh e ll > m v j b o ss - 3 .0 .2 . z i p / u s r/ l o c a l / j bo ss sh e ll > c d / u s r/ l o ca l/ j b o s s sh e ll > j ar - x f j b os s- 3 . 0 . 2 . z i p

Now youll need to make the startup scripts executable.


sh e ll > c d j b o ss - 3 .0 .2 / b i n / sh e ll > c hm o d ug + x * .s h
http://developer.apple.com/internet/java/enterprisejava.html Pgina 1 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

sh e ll > c hm o d ug + x * .s h

If you happen to be using a version of JBoss prior to 3.0, you may have to fix a glitch in one of the startup scripts. This bug has been fixed in JBoss 3.0. In earlier versions, the ru n. sh script incorrectly identifies a HotSpot server VM (Mac OS X comes standard with only the HotSpot client VM). You can fix this problem by replacing this line in jb o s s/ b i n/ r u n .s h :

HO T SP O T = `j a v a - v e rs io n 2 > & 1 | g r e p H o t Sp ot ` "x"

with this one


HO T SP O T = `j a v a - v e rs io n 2 > & 1 | g r e p H o t Sp ot | g re p Se rve r` "x "

Now youre ready to start JBoss and Jetty:


sh e ll > b in / r u n. s h

The JBoss/Jetty bundle comes pre-configured and ready to run simple J2EE applications. One of the nicer features of JBoss is its ability to instantly deploy an application. Once youve created your application, drop it into the j bo s s/ d e p l oy directory and it will be loaded. Theres no need to re-start the server. In order to compile and deploy the example code in this article, youll need to install the Jakarta projects opensource build tool, called Ant. Im using version 1.4.1 and installed it in / usr /l oc al /an t , with these commands:

sh e ll > s ud o m kd i r / us r / l o c a l / an t Pa s sw o r d : sh e ll > s ud o c ho w n l iz : s t a f f / us r / l o c a l /a nt Pa s sw o r d : sh e ll > m v j a k ar t a -a nt - 1 . 4 . 1 - b in . t a r . g z / us r /lo ca l/ an t sh e ll > c d / u s r/ l o ca l/ a n t sh e ll > t ar - x zv f ja ka r t a - a n t - 1. 4 . 1 - b i n .t ar . gz

In order to use Ant, youll want to have the environment variable $JAVA_HOME set, and you may want to add the Ant bi n directory to your $PATH.

sh e ll > e xp o r t J A V A_ HO M E = / u s r sh e ll > e xp o r t P A T H= $P A T H : / u s r /l o c a l / a n t/ ja k art a- an t- 1.4 .1 /b in

Of course, you can put these commands into a dotfile (a Unix shell startup file) so theyre permanently available. See the man page for your default shell for how to set up dotfiles (for example m an tc sh or m an sh ). Click T er m in a l / P re f e re n c e s /S h e l l from within the Terminal application if you dont know which shell youre running.

Enterprise JavaBeans and EJB Containers


JBoss is an EJB 1.1-compliant application server. If youre completely new to EJB, I recommend that you review the pages at http://java.sun.com/products/ejb/. Why use Enterprise JavaBeans? The EJB container (the environment in which your beans run, like JBoss) can easily handle some typically cumbersome tasks. It can handle transactions, session management, and can even abstract database code if you want it to. EJB technology also makes it easier to write scalable applications, with code distributed across more than one machine. There are three types of Enterprise JavaBeans: Entity, Session, and Message-Driven. Ill show you examples of Entity and Session beans in this article.
http://developer.apple.com/internet/java/enterprisejava.html Pgina 2 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

Session beans represent a single clients session. They can be either stateful or stateless. They do not persist beyond a single clients session. Entity beans represent objects maintained in persistent storage (like a database). One of the cool things about using EJB technology is that your application server handles all kinds of tricky things for you. The cost of all these free goodies is that your applications are forced to have a fairly complicated setup process. But after creating a J2EE application or two, youll get the hang of it. And if you shelled out for a graphical J2EE development environment, that tool will automate most or all of the setup process for you. For now, though, Ill show you how to create some simple J2EE applications using only command-line tools. Heres a snapshot of the files necessary to create a Hello World application consisting of a servlet and a session bean.

Here is a tarball that should be helpful for the HelloWorld application, as well as HelloEntity and MailList which we explore below: enterprisejava.tar.gz

The J2EE Platform Specification calls for class and XML files to be bundled together in a specific format for deployment. Web objects, like servlets and their associated we b.x ml files, go into a WAR (Web ARchive) file. EJB classes and their XML config files go into a JAR (Java ARchive) file. Then both those archives get bundled with a file called a pp l i ca t i o n .x m l into a new EAR (Enterprise ARchive) file. Once thats done, you can deploy your new web application by copying the EAR file into your j bos s/ de pl oy directory. First, lets look at the XML files. The first two define a session bean called H el lo Wo rld . The jb oss .x ml file is not strictly required for this application, but Ive included it anyhow. This is the file that would contain vendorspecific configuration information relating to your EJBs. Another file, called j bo ss -w eb. xm l (not shown) can be used for JBoss-specific configuration relating to your web application.
sh e ll > c a t e j b -j a r .x ml <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "U T F - 8 " ? > <! D OC T Y P E e j b -j a r P UB L I C
http://developer.apple.com/internet/java/enterprisejava.html Pgina 3 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

<! D OC T Y P E e j b -j a r P UB L I C '- / /S u n Mi c r o sy s t em s, I n c . / / D TD E n t e r p ri se Jav aB ea ns 1. 1/ /E N' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ e j b - j a r _1 _1 . dtd '> <e j b- j a r > < di s p l ay - n a me > H el lo W o r l d < / di s p l a y - n am e> < en t e r pr i s e -b e a ns > < s e s si o n > < d es c r i pt i o n> He l l o W o r l d E J B < / d e sc ri p tio n> < d is p l a y- n a me >H e l l o W o r l d< / d i s p l a y- na m e> < e jb - n a me > H el lo W o r l d < / e jb - n a m e > < h om e > H el l o Wo rl d H o m e < / h om e > < r em o t e >H e l lo Wo r l d < / r e m ot e > < e jb - c l as s > He ll o W o r l d E J B< / e j b - c l as s> < s es s i o n- t y pe >S t a t e l e s s </ s e s s i o n -t yp e > < t ra n s a ct i o n- ty p e > C o n t a in e r < / t r a ns ac t ion -t yp e> < / s e ss i o n > < /e n t e rp r i s e- b e an s> </ e jb - j a r>

sh e ll > c at j b os s . xm l <? x ml v e rs i o n =" 1 . 0" ?> <j b os s > <e n t e rp r i s e- b e an s> < s es s i o n> < e j b -n a m e> He l l o W o r l d </ e j b - n a m e> < / se s s i on > </ e n t er p r i se - b ea ns > </ j bo s s >

The we b. x m l file should look familiar to you if youve worked with other servlet engines, or have read the Apple Internet Developer articles on Tomcat. This project includes a bare-bones we b. xm l file.

sh e ll > c at w e b. x m l <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "I S O - 8 8 5 9 -1 "? > <! D OC T Y P E w e b -a p p PU B LI C ' -/ / S u n M i cr os y s t e m s , In c . / / D T D W eb App li ca ti on 2. 2/ /E N' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ w e b - a p p _2 .2 . dtd '> <w e b- a p p > <d i s p la y - n am e > He ll o W o r l d < /d i s p l a y - na me > <d e s c ri p t i on > H el lo W o r l d < / de s c r i p t i on > <s e r v le t > <s e r v le t - n am e > He ll o W o r l d S e rv l e t < / s e rv le t -na me > < s er v l e t- c l as s> H e l l o W o r ld S e r v l e t < / se r v l et - c la ss > </ s e r vl e t > <s e r v le t - m ap p i ng > < s er v l e t- n a me >H e l l o W o r l dS e r v l e t < /s er v let -n am e> < u rl - p a tt e r n> /H e l l o W o r l ds < / u r l - p at te r n> </ s e r vl e t - ma p p in g> <e j b - re f > < e jb - r e f- n a me >H e l l o W o r l dH o m e < / e j b- re f -na me > < e jb - r e f- t y pe >S e s s i o n < / ej b - r e f - t yp e> < h om e > H el l o Wo rl d H o m e < / h om e > < r em o t e >H e l lo Wo r l d < / r e m ot e > < e jb - l i nk > H el lo W o r l d < / e jb - l i n k > </ e j b -r e f > </ w eb - a p p>

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 4 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

Finally, heres the a p p l ic at i o n . x m l file, with information about the application as a whole.

sh e ll > c at a p pl i c at io n . x m l <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "U T F - 8 " ? > <! D OC T Y P E a p p li c a ti on PU B LI C ' -/ / S u n M i cr os y s t e m s , In c . / / D T D J 2E E Ap pl ic at ion 1 .2 // EN' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ a p p l i c a ti on _ 1_2 .d td '> <a p pl i c a ti o n > <d i s p la y - n am e > He ll o W o r l d < /d i s p l a y - na me > <d e s c ri p t i on > H el lo W o r l d < / de s c r i p t i on > <m o d u le > < e jb > H e ll o W or ld . j a r < / e j b> < / mo d u l e> <m o d u le > < w eb > < w e b -u r i >H el l o W o r l d . wa r < / w e b - ur i> < c o n te x t -r oo t > H e l l o W or l d < / c o n te xt - roo t> < / we b > </ m o d ul e > </ a pp l i c at i o n >

Next, lets look at the Java code in He l l o W o r l d/ s rc . The first two files, H ell oW or ld .ja va and H el l oW o r l d Ho m e .j a v a , define the home and remote interfaces for the new session bean.

sh e ll > c at H e ll o W or ld . j a v a im p or t j av a . r mi . * ; im p or t j av a x . ej b . *; pu b li c i nt e r f ac e He ll o W o r l d e xt e n d s E J BO bj e ct { p u b l ic S t ri n g h i( ) t h r o w s R e m o t e E x ce pt i on; }

sh e ll > c at H e ll o W or ld H o m e . j a v a im p or t j av a . r mi . * ; im p or t j av a x . ej b . *; pu b li c i nt e r f ac e He ll o W o r l d H o me e x t e n d s EJ B Hom e { p u b l ic H e ll o W or ld c r e a t e ( ) t h r o w s Re mo t eEx ce pt io n, Cr ea te Exc ep ti on ; }

Next is the class file for the bean. This is the file that contains your business logic. It creates a string with the contents hiya. Finally, heres the servlet code. Now that youve got a pile of . x m l files and a pile of .j av a files, you can use Ant to compile your code, bundle it, and deploy it to your application server. By default, when Ant is run, it looks for a file in the current directory called b u i l d. x m l and will follow the directions inside. Heres the bu il d. xml for the Hello World application. With all these files in place, youre ready to run Ant.
sh e ll > a nt

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 5 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

On success, you should see output something like this. Once your application is deployed, you can visit http://localhost:8080/HelloWorld/HelloWorlds in your browser to see all that business logic in action.

Entity Beans
An entity bean represents an object in persistent storage. There are two ways to handle persistence usermanaged and container-managed. In user-managed persistence, the code author must explicitly store and retrieve the beans data from storage, usually via JDBC calls. In container-managed persistence, the EJB container handles all the storage and retrieval behind the scenes. JBoss comes pre-configured to use the HypersonicSQL embedded database, which means you can run this example code without changing your JBoss configuration. If youd like to use a different database, such as MySQL, see the JBoss documentation. Heres another simple web application. This one uses an entity bean to create, store, and retrieve user records. A user record consists of two data fields: name, and email. Notice that even though the records are being stored in the HypersonicSQL database, I didnt write a line of database code. Instead, I specify the fields in my e j b j ar . xm l file, and rely on my EJB container to manage persistence. Here is a listing of the other XML files this application uses. Now to the Java source. First, the remote and home interfaces, the entity bean, the servlet, and the bu i l d. x m l file. You might have noticed by now that the build files look a lot alike. In fact, the only thing I really had to change was the appname property (although I also changed the project name). The rest of the file is pretty standard, and I hope you can re-use it for your projects. You will have to customize your bu il d.x ml files further, of course, once youre building more complicated applications, using packages, and requiring more libraries in your compilation path. My advice is that you either get to know Ant, or invest in a Java IDE with project management capabilities. Once deployed (again, just type a n t once your files and directories are in place), your application should be available via http://localhost:8080/HelloEntity/Hi.

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 6 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 7 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

Transactions
Transactions are multi-part actions that are required to function as one unit. Either all actions succeed together, or they all fail. Just as an entity beans data persistence may be user- or container-managed, a J2EE transaction can be handled by your bean code, or handed off to the container. If the container is managing a transaction associated with a certain method, it will automatically roll back any database calls in that method upon receiving a system exception. For more on transactions, visit this tutorial on java.sun.com. You can define container-managed transactions in your beans deployment descriptor (the e jb -ja r. xm l file). See the assembly-descriptor section of http://java.sun.com/dtd/ejb-jar_2_0.dtd for full details. In general, your code
http://developer.apple.com/internet/java/enterprisejava.html Pgina 8 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

will look something like this:


[. . .d e c l ar a t i on f or a b e a n c a ll e d " M y E xa mp l e". .. ] <t r an s a c ti o n - ty p e >C on t a i n e r < / tr a n s a c t i on -t y pe> [. . .] < a ss e m b ly - d e sc r i pt or > < c o n ta i n e r- t r an sa c t i o n > < m et h o d > <e j b - na m e >M yE x a m p l e < / ej b - n a m e > <m e t h od - n am e> * < / m e t h o d- n a m e > < / me t h o d> < t ra n s - at t r ib ut e > R e q u i r ed < / t r a n s -a tt r ibu te > < / c o nt a i n er - t ra ns a c t i o n > < /a s s e mb l y - de s c ri pt o r >

JBoss comes with a built-in transaction manager. Like other JBoss component parts, it can be replaced with another JTA (Java Transaction API) transaction manager implementation. See the JBoss documentation for instructions.

Putting it Together: Simple Mailing List Manager


Ill wrap up this article with a simple web application to manage an announcement mailing list. Two servlets take care of the user interface for adding/removing users and sending announcements. A container-managed entity bean handles the user data. There are several things lacking in this example application, most notably bounce handling, error checking, and any kind of security. For security reasons, I dont recommend installing this demo in its existing form on a publicly accessible server for any length of time. If you plan on extending this code to create your own real-world application, note that JBoss makes it relatively easy to password-protect your applications (once again, via XML configuration files). See the JBoss documentation for more information. Here are the files that make up this last application. First, a listing of the xml files, the bean interfaces, the bean class, and the two servlets. Once youve built and deployed your application using Ant, you should see pages like these. Ive fudged the example email addresses in the screen shots, but you should get the idea.

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 9 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 10 de 11

Enterprise Java on Mac OS X

05/05/10 23:30

Conclusion, and Suggestions for Further Reading


In this article, youve seen how to deploy some simple J2EE web applications using JBoss, Jetty, and Ant. Youve also seen a few of the advantages of the EJB architecture. Being able to hand off whole sections of complex code to your EJB container is a non-trivial advantage, and in my opinion its well worth the learning curve. To learn more about J2EE and EJB, you can visit the excellent tutorials at java.sun.com. The JBoss documentation is also invaluable. For books on J2EE and Java, visit java.oreilly.com.

Get information on Apple products. Visit the Apple Store online or at retail locations. 1-800-MY-APPLE Copyright 2010 Apple Inc. All rights reserved. | Terms of use | Privacy Notice

http://developer.apple.com/internet/java/enterprisejava.html

Pgina 11 de 11

You might also like