• No results found

Data flow

10.1 Web application elements

A Web application environment consists of different components. In this section, we will give an overview of the most common technologies. The focus is set on the Java elements and the different markup languages for the mobile devices.

10.1.1 Java servlet

Java servlets are used to provide dynamic Web content based on an HTTP request. In general, they are server-side Java programs, so they offer all the advantages of the Java language. Servlets are object-oriented and can utilize the full Java API. This allows the developer to use predefined and implemented code and methods easily. The Java servlet can take advantage of the built-in memory control of the Java Virtual Machine (JVM). For example, unused objects are automatically removed by the garbage collector.

Sun’s Java Servlet API also defines a session object. The Java servlets are able to store information about the session, such as user data input or user

preferences, in the context memory.

Unlike common server-side programs using CGI, Java servlets can be deployed on each Web server (with servlet support) without advance compilation.

Furthermore, the servlets are only compiled and initialized once in the runtime environment. For the incoming request, an instance of the appropriate servlet is used. This strategy ensures a higher performance compared to scripting languages.

There are many tools on the market for developing Java servlets. The offering also provides the development tools: IBM VisualAge for Java 3.5 and WebSphere Studio 3.5. Both tools can be combined for quick application development. We describe these components in detail in 10.2.1, “WebSphere Studio” on page 166 and 10.2.2, “VisualAge for Java” on page 173.

For more information, please refer to the redbook Programming with VisualAge for Java, SG24-5264.

10.1.2 JSPs

JavaServer Pages (JSPs) are used to generate the formatted output for a Web page response. JSPs do not include any business logic. They are useful in separating the development of the Web site from the Web page design. It is possible to define clear responsibilities for the Web application development. On

Chapter 10. Application development 155 one hand, the Web page designer does not have to know how to obtain dynamic data, but only where to place it, and can therefore concentrate on the look and feel of the Web page. On the other hand, Java programmers do not have to handle layout issues. Their focus is to provide the logic for the application.

The dynamic content of the Web pages is typically provided by a JavaBean (see 10.1.3, “JavaBeans” on page 156). JavaBeans are reusable software

components that can be created by other Java objects, such as for example Enterprise Java Beans (EJBs) or Java servlets. Through the use of a special scripting language, the data in the JavaBean can be accessed from the JSP.

The output of a JSP can be any type of document; in our case the following ones were used:

򐂰 HTML

򐂰 WML

򐂰 XML

򐂰 Related HTML markup languages, like cHTML

򐂰 Other XML-based languages, like VoiceXML

JSPs are used in the common MVC model in the View component. The servlet dispatches the request to the JSP. Figure 10-1 gives an overview of how JSPs are used in this context.

Figure 10-1 Interaction between Java components and Web application objects Servlet

The servlet receives an request from the client and invokes an EJB to fulfill this request. The EJB collects the data from a database, then instantiates and sets a JavaBean.

The JavaBean then has to be put into the scope, so the JSP can reach it as an object through the application server.

Depending on the client, the servlet now dispatches the request to the appropriate JSP file. For example, WML JSP files are used for WAP devices, while for a normal desktop browser, HTML JSP files will be invoked from the servlet.

A simple way to develop JSP files is to use WebSphere Studio (see 10.2.1,

“WebSphere Studio” on page 166). It provides wizards and additional

functionalities for creating and editing JSPs. The WebSphere Test Environment in VisualAge for Java allows you to test the JSP files in conjunction with your Web application (see 10.2.2, “VisualAge for Java” on page 173).

10.1.3 JavaBeans

JavaBeans are simple component classes in the Java language and are mainly utilized as reusable objects. The following rules must be applied to a JavaBean:

򐂰 Public class

򐂰 Public constructor without arguments

򐂰 Setter and getter methods for accessing the properties

The setter methods are used to set values for certain fields of an object. It is not possible to access and set them directly, so with getter methods, values can be retrieved from the object. Furthermore, it is possible to define events for the bean, in case the JavaBean is able to interact with other beans.

Most development environments support the instrospector feature for JavaBean development. This feature allows users to modify the bean using a visual interface. Note that you can normally use this feature only when the syntax rules are implemented correctly. The developer of a JavaBean can also provide the structure and further information about the bean via the JavaBean Information class.

In the Java servlet and JSP Web application context, JavaBeans are mainly used as a kind of a container for data exchange between different components. For example, an EJB object providing the business logic can instantiate the bean and set the properties values. The JSP is then able to retrieve the data by using the getter methods of the bean.

Chapter 10. Application development 157 JSPs can reach the JavaBeans as objects via the application server. In order to be accessible, the bean has to be put into the scope of the Web application. The following three scopes are available during runtime:

1. Application: the object is accessible while the application server is running.

2. Session: the object is only available for the session it has been created in.

The object disappears together with the session.

3. Request: the object is available during the request; after the response has been sent out, the object disappears.

JavaBeans are typically distributed in .jar files.

For JavaBeans development, we recommend the two IBM tools IBM WebSphere Studio 3.5 and IBM VisualAge for Java 3.5 (see 10.2.1, “WebSphere Studio” on page 166 and 10.2.2, “VisualAge for Java” on page 173 for more details).

For more informations on JavaBeans, please refer to http://java.sun.com/products/javabeans/

10.1.4 Enterprise JavaBeans

The EJB 1.0 specification was announced by Sun Microsystem in March of 1998.

The main focus and advantages of Enterprise JavaBeans (EJBs) are to separate the business logic of an application from the middleware supporting it.

The EJBs are deployed and managed in the application server in a container framework. One container can include one or more beans. To split up the development, management and handling of EJBs, the following roles are defined:

򐂰 Provider: develops the business logic

򐂰 Deployer: deploys the EJBs

򐂰 Application Assembler

򐂰 Container Provider

򐂰 Server Provider

򐂰 System Management

Every EJB consists of a Remote and a Home Interface. The name of the EJB Home Interface is stored in the JNDI directory. One way for clients to access the EJB is to use this Home Interface. It allows clients to create, find and remove EJB instances. Another approach is to use AccessBeans. They hide the complexity of the EJB interface, but offer the client the same functionality.

An Enterprise JavaBean can be either a session bean or an entity bean.