|
The Java 2 Enterprise Edition (J2EE) is a standard that defines an environment for the development and deployment of enterprise applications. It reduces the cost and complexity of developing multitier enterprise applications as it provides a multitier distributed application model. In other words, it is inherently distributed and therefore the various parts of an application can run on different devices. Web applications developed using JavaServer Pages (JSP) may require some interaction with J2EE services. For example, a web-based inventory control system may need to access J2EE's directory services to gain access to a database. Or you may want to use Enterprise JavaBeans (EJB) in your application. This article presents a brief overview of J2EE, then it shows how to:
Overview of J2EEThe J2EE is a standard platform for the development and deployment of enterprise applications. The architecture of J2EE, which is component-based, makes developing enterprise applications easy because business logic is organized into reusable components and the underlying service is provided by J2EE in the form of a container for every component type. Think of a container as the interface between the component and the low-level functionality that supports the component. Therefore, before an application client component can be executed, it must be configured as a J2EE service and deployed into its container. The standard services included in version 1.3 of the J2EE specification and a brief description of each is shown in Table 1.
J2EE promotes the development of multitier applications in which the web container hosts web components that are dedicated to handling a given application's presentation logic, and responds to requests from the client (such as the web browser). The EJB container, on the other hand, hosts application components that respond to requests from the web tier as shown in Figure 1.
Figure 1: Multitier Applications Applications that use this architecture are implicitly scalable. This architecture decouples the accessing of data from end-user interactions, and encourages component-based code reusability. At the web tier, J2EE promotes the use of JSPs for the creation of dynamic content for Web clients. Custom Tags and J2EEJ2EE has a lot to offer to Web application developers and the JSP custom tag developer. As you can see from Table 1 above, it has a rich set of standard APIs for sending email, accessing databases, parsing XML documents, and so on. Your Web applications can benefit greatly from these APIs. For example, you can write a JSP custom tag for sending email that can be used easily by Web content developers who are not familiar with Java. If you are not familiar with JSP custom tags, their benefits, and how to create them, please refer to Developing JSP Custom Tags. EJBsAn EJB is a server-side component that encapsulates the business logic of an application. In any application, the enterprise beans implement the business logic in methods, which can be invoked by remote clients to access the relevant services provided by the application. BenefitsEJBs simplify the development of large, secure, distributed enterprise applications for the following reasons:
ComponentsThere are two main types of EJB components: session and entity. A session EJB is used to perform a task for a client, and an entity EJB is domain specific and is used to represent a business entity object that exists in persistent storage. Session and entity beans are, however, different in some ways, as described in Table 2.
Developing EJBsTo develop EJBs, each enterprise bean needs:
EJBs vs. ServletsAt first encounter, EJBs and Servlets are similar technologies since they are both distributed server-side components. There is however, a major difference between the two in the type of solution they offer; EJBs cannot accept HTTP requests. In other words, EJBs cannot serve requests coming directly from a Web browser, and servlets can. Servlets and JSPs can be used to implement Web presentation and control but unlike EJBs, they cannot handle distributed transactions. EJBs can be called from any Java-based client. When to use EJBsEJBs are a good fit if your application has any of these requirements:
Describing and Referencing J2EE ServicesNow that we have seen an overview of J2EE and EJBs, we will now see how to reference, access, and use such services. Fortunately, the J2EE specification defines a standard way for accessing and using such services. J2EE services can be referenced by looking them up according to a unique name in a directory. This functionality is supported by the Java Naming and Directory Interface or JNDI. In order for this to work, each J2EE-compliant system provides a JNDI service called an environment that contains:
Environment VariablesThe application component's naming environment allows the application component to be customized without the need to access or change the application component's source code. Each application component defines its own set of environment entries. All instances of an application component within the same container share the same entries. It is important to note that application component instances are not allowed to modify the environment at runtime. Declaring Environment Variables
The application component provider must declare all the environment entries accessed from the application's component code. They are declared using the
The example in Code Sample 1 shows a declaration of two environment entries. To specify a declaration for a new environment, you simply add it to your Web application descriptor (web.xml). Code Sample 1: Declaring environment variables
Each
Now, an application component instance can locate the environment entry using JNDI. It creates a Code Sample 2: Accessing environment entries
Note that the application component may also look up environment entries using the full path names as follows: javax.naming.Context ctx = new javax.naming.InitialContext(); String str = (String) ctx.lookup( "java:comp/env/greetings"); This snippet of code can be used in a JSP as shown in Code Sample 3: Code Sample 3: Accessing environment entries from a JSP
Ultimately, however, you would want to access the environment entries from a custom tag. So, you would develop a JSP custom tag or a tag library that can be reused without cutting and pasting code. A custom tag library can be easily developed following the guidelines and how-tos described in Developing JSP Custom Tags. EJB ReferencesThe application component's provider can refer to EJB homes using logical names (called EJB references) instead of JNDI registry values. They are special references in the application component's naming environment. These references allow Web applications to access EJBs in a customized fashion. Declaring EJB References
An EJB reference is an entry in the application component environment. However,
The example in Code Sample 4 shows a declaration of an EJB reference entry. To specify a declaration for a new entry, you simply add it to your Web application descriptor (web.xml). Code Sample 4: Declaring an EJB reference
Each
Now, an application component instance can locate the EJB reference entry using JNDI. It creates a Code Sample 5: Accessing an enterprise bean
You can either use similar code directly in your JSPs, or develop a custom tag library for accessing and using EJBs. Resource Factory References
Resource factory references allow applications to refer to connection factories, or objects that create connections to desired resources, using logical names as we have seen in the previous two sections. The resource connection factory references can be JDBC connections, JMS connections, email connections, and so on. The JNDI URLs start with: Declaring Resource Factory References
A resource factory reference is an entry in the Web application deployment descriptor. It must be declared using the
Code Sample 6 shows a declaration of an email resource reference factory. To specify a declaration for a new entry, you simply add it to your Web application descriptor, web.xml. Code Sample 6: Declaring a resource factory reference
Each
Now, an application component instance can obtain the resource factory reference entry using JNDI. As with other entries, it creates a Code Sample 7: Obtaining a reference to a resource factory and sending email
Again, this code can be used directly in your JSPs, or you can develop a custom tag library for accessing and using various resource factory references. As another example, Code Sample 8 shows a declaration of a database resource factory. Code Sample 8: Declaring a database resource factory
Code Sample 9 shows how an application component obtains a reference to a database connection factory and uses it. Code Sample 9: Obtaining a reference to a database connection factory and using it
ConclusionJ2EE offers many services that are invaluable for Web applications. These services range from opening a connection to a database using JDBC, to sending an email, to accessing and using enterprise beans. This article, along with sample programs, shows how to access J2EE services from within JSPs. Incorporating EJBs into your JSPs can be easily done, creating a reusable solution by developing custom tags. Why don't we access J2EE services from Servlets? While it is possible to do that, you would end up writing more unreusable code. If you wish to use J2EE services from JSPs, developing custom tag libraries provides reusable solutions that can even be used by content developers who have no Java experience. For more information
J2EE
About the authorQusay H. Mahmoud provides Java consulting and training services. He has published dozens of articles on Java, and is the author of Distributed Programming with Java (Manning Publications, 1999). | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||