8.1 Developing Web Applications

   

Trying to understand what technologies should be applied when developing a web-based application is a common dilemma. Should you use HTML, Java, JavaScript, applets, servlets, EJBs, and/or XML? In some organizations there is no dilemma because someone else, someplace else in your organization, has already made the choice for you. If you are given the choice, however, what approach should you use?

Your first step is to understand how Java and related technologies can be used for web-based applications. This section examines the use of Java and related technologies for web-based application development in three layers : the presentation layer, the data and business rules layer, and the control layer, as depicted in Figure 8-1.

Figure 8-1. Web application architecture
figs/oas_0801.gif

8.1.1 Presentation Layer

The presentation layer represents the interface to the application that will be used by a human being. In a presentation (or view) layer of a web-based application, it isn't uncommon to see a combination of HTML, JavaScript, Java applets, and XML employed to provide a graphical user interface for an end user . The following are some examples:

  • HTML can create a data-entry form, a menu, and so on.

  • JavaScript can provide client-side (web-browser) based validation and enhanced functionality that is beyond the capabilities of a simple HTML form.

  • A Java applet can be used in an HTML form to extend the capabilities of JavaScript or to replace HTML form tags altogether, by providing a rich GUI using Java.

8.1.1.1 HTML

An HTML form, created using the <form> related tags in HTML, provides a simple user interface. It doesn't allow you to do any fancy client-side validation or dynamic population of drop-down list boxes. It's like using a block-mode terminal from the good old days, where everything in the form must be transmitted to the server to be validated . If a validation error occurs, the server must rerender the entire HTML form and send it back to the end user's browser.

This type of validation can be both time-consuming and resource- intensive . It also creates a poor experience for the user of the application. However, it is extremely lightweight, efficient, portable, and well supported.

8.1.1.2 JavaScript

By adding JavaScript routines to an HTML page you can perform client-side validation, dynamic loading of drop-down list box values, custom error windows , and so on. Using JavaScript can improve the GUI capabilities of an HTML form so that the end user's experience is closer to that of using a client-server application. Of course, this enhanced end-user experience comes with a price, which is that JavaScript isn't equally supported on all browsers. That, in turn , means that you will have compatibility issues. Regardless, the use of JavaScript as an extension to HTML forms is very popular.

8.1.1.3 Java applets

If JavaScript can't extend an HTML form's capabilities to match your requirements, you can extend JavaScript's capabilities with a Java applet or use an applet to provide some additional GUI functionality on part of a web page. You can also replace the combination of an HTML form and JavaScript altogether by using a Java applet as a rich-content GUI.

Using a Java applet as a rich-content GUI allows you to provide a client-server application look-and-feel inside a browser. That is how Oracle's Forms applications, for example, are used on the Web, by employing an applet run inside your browser. The one drawback with applets used in this fashion is that they are large, and consequently take longer than HTML pages to download and display in a browser.

8.1.1.4 JSP and Java servlets

JavaServer Pages are nothing but Java servlets alternatively created by coding HTML with scriptlets and other JSP syntax, instead of by coding Java that outputs HTML, as is done in a servlet. Because JSP is nothing but a servlet, the discussion in this section covers both technologies.

Java servlets can generate the HTML pages that are then presented in an end user's browser. Essentially, you use a servlet to create HTML forms, possibly with embedded JavaScript routines and applet tags, to load an applet. Servlets (or JSPs) allow you to retrieve values from a database, create a form, and send it to the browser. When the user submits the form back to the server, it calls another servlet (or possibly the same one, depending on design) to process the data values and update the database.

You can see from the discussion in the previous sections that HTML, JavaScript, and applets are probably used together, with the HTML generated by a servlet. But what about the data and business rules? How do you retrieve and save an application's data? Where are the business rules kept? That is where JDBC and EJBs are used:

  • JDBC allows your program to access a relational database.

  • EJBs allow you to create modular, reusable software components that encapsulate business logic and data persistence.

The following sections describe these capabilities.

8.1.2 Data and Business Rules Layer

In the data and business rules layer of a web-based application, it isn't uncommon to see a combination of JDBC, JavaBeans, Java Data Objects (JDO), EJBs, and XML manipulating data and executing business rules. Several approaches can be used to manipulate data used in a web application:

  • JDBC used directly in a servlet

  • JDBC used in JavaBeans

  • OracleAS Toplink, OracleAS Business Components for Java, or another proprietary object-relational mapping tool that implements its solution as JavaBeans or EJBs

  • JDO

  • EJB

Several of these persistence technologies may also be combined and then used in a servlet. As you go through the options listed here, the complexity, programming required, and infrastructure required all increase.

8.1.2.1 JDBC used in servlets

You can use JDBC directly in your servlets. JDBC allows you to perform SQL Data Manipulation Language (DML), such as inserts , updates, deletes, and selects, along with stored procedure calls and Data Definition Language (DDL).

If you do use JDBC directly in your servlets, make sure to use prepared statements to prevent SQL injection.

With SQL injection , values entered into fields of your HTML form are actually additional SQL syntax. This maliciously added SQL syntax allows the perpetrator to select all values from a table or damage the validity of data in a table by erroneously inserting, updating, or deleting rows. SQL injection is possible if you use a JDBC Statement which allows you to perform dynamic SQL, but not if you use a PreparedStatement .


8.1.2.2 JDBC used in JavaBeans

If the size of your application requires multiple servlets to access the same information, you should consider consolidating your JDBC code in a JavaBean that represents a table in the database. Alternatively, use one of the other more complex solutions covered in the following sections.

If you create a JavaBean that represents a table in the database, you can add routines to select, insert, update, and delete entries as well as accessors and mutators. Accessors and mutators are methods that allow you to get and set column values. This level of abstraction is well suited for JSPs because you can then use JSP bean declarations instead of large embedded scriptlets in your JSPs to retrieve and save data.

8.1.2.3 Object-relational mapping tools

Object-relational (OR) mapping tools also allow you to use simple JavaBeans for data abstraction. These tools provide a framework that glues JavaBeans to a persistence layer such as a database. Oracle Application Server provides two OR mapping tools: OracleAS TopLink and OracleAS Business Components for Java. Object-relational mapping tools add value because they significantly reduce the amount of time it takes to create code for persisting data in a relational database.

8.1.2.4 Java Data Objects

Java Data Objects is a more recent public specification and standard for object-relational mapping. It can also be used in the same way as custom JavaBeans. JDO simplifies a lot of the coding required to make your own beans. However, using JDO comes with a price: you have to license a JDO implementation, learn how to use yet another technology, and do some special coding just for JDO.

8.1.2.5 Enterprise JavaBeans

If your application is one with high transaction volumes or a large user base, or one in which many servlets will need to share the information and business processes, Enterprise Java Beans are your best bet. EJBs provide a business process and data abstraction that treats each EJB as a distributed component that can be called by any program. EJBs provide services that ease management of transactions and deployment of an application.

EJBs allow you to more easily separate data from the business rules that may manipulate the data. This, in turn, leads to a more adaptable and longer-lived application. Once again, this comes at a price. As with JDO, you'll have to learn how to use yet another technology and do some special coding just for EJBs.

8.1.3 Control Layer

The control layer acts as a traffic cop between the presentation layer and the data and business rules layer. One important aspect of a web-based application is how to direct the response of an HTML form to another servlet (or other entity) in the presentation layer. One common approach is to hardcode the URL of the servlet that will process a form in the action attribute of the HTML <form> tag. This approach is referred to as model 1 programming . This works just fine, but it makes your application's modules programmatically dependent on each other.

An improvement on this approach is model 2 programming with which the same controller servlet is specified in the action attribute of every form. The controller servlet then forwards the HTML form's response on to the desired servlet. The desired servlet's URL is stored in a configuration file, as shown in Figure 8-2.

Figure 8-2. A model 2 controller
figs/oas_0802.gif

Struts is a configurable implementation of a model 2, Model-View-Controller (MVC) framework. You can find additional information about Struts at http://jakarta.apache.org/struts/.

8.1.4 Other Considerations for Web-Based Applications

You might also consider making sure that the output of your servlets is XHTML instead of HTML. XHTML is an XML implementation of HTML, one in which HTML documents are well- formed and valid XML documents. Using XML from the start gives you an advantage if you need to provide a presentation layer of your application to another device, such as a cell phone. In this case, your servlet's output can be forwarded to a filter servlet that transforms HTML (XHTML) to Wireless Markup Language for the handheld device. This process is possible because your HTML is actually XML. Chapter 10 describes XML in more detail.

After reading this overview of Java web-based application development, you may have realized that the most formidable part of web-based application development is the number of technologies you must master to build a user-friendly application. There are many good Java books out there that can help you. O'Reilly, for example, has books on each of these topics. You can find very well-written books on servlets, EJBs, Struts, and JavaScript at http://www.oreilly.com. Choosing a development tool that aids you in these technologies can also help tremendously.

   


Oracle Application Server 10g Essentials
Oracle Application Server 10g Essentials
ISBN: 0596006217
EAN: 2147483647
Year: 2004
Pages: 120

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net