Objects and the Web
Total Page:16
File Type:pdf, Size:1020Kb
focusengineering Internet software Objects and the Web Alan Knight, Cincom Systems Naci Dai, ObjectLearn pplying software engineering principles, particularly object-ori- ented techniques, to the Web can be difficult. Many current Web technologies lend themselves to—or even encourage—bad prac- A tices. Scripting and server-page technologies can encourage cut- and-paste reuse, direct-to-database coding, and poor factoring. Component models such as the Component Object Model (COM) and Enterprise Java- Beans (EJB) seek to construct building blocks for application assembly, but in doing so they sacrifice many of the advantages of objects. XML emphasizes technology-independent reuse and sharing the need for these techniques, we examine of content, data, and messaging but at the some representative Web technologies and expense of encapsulation and the associa- the issues they present in naive use. We de- tion of behavior with state, which is central scribe a layered, OO architecture, based on to OO. the Model-View-Controller (MVC) pattern, Scripting languages, common on the which can overcome these issues to produce Good design Web, are often optimized for rapidly creat- large, well-structured systems. ing simple functionality rather than for practices are Motivations increasingly modular construction of large programs. Also, such languages typically lack the rich If we consider scripts from an OO and important in Web development environments of general-pur- layering perspective, the most immediate development. Here, pose languages. Some Web developers even problem is that a single script has responsi- the authors apply deliberately disregard software engineering bilities spanning several layers (see the “Def- such practices principles. They argue that if we’re just initions” sidebar for an explanation of our using a framework writing scripts, they don’t merit the kind of terminology). The script must for layered engineering techniques—object or other- architectures based wise—we apply to “real” systems. I Accept input on the Smalltalk GUI However, software engineering and OO I Handle application logic development pattern techniques are gaining importance in Web I Handle business logic of Model-View- development as Web applications become I Generate output (presentation logic) Controller. more complex and integrated with tradi- tional server-side applications. To motivate This couples all the layers together, making 0740-7459/02/$17.00 © 2002 IEEE March/April 2002 IEEE SOFTWARE 51 Authorized licensed use limited to: Universidad Federal de Pernambuco. Downloaded on June 18,2010 at 23:56:58 UTC from IEEE Xplore. Restrictions apply. Definitions Here we define our terminology and goals. In the main text, ing business functionality. This code should be entirely unaware we present a layered architecture, implemented using both scripts of the presentation being used. We also refer to business ob- and server pages. Most of these techniques can be applied to jects, which implement the business logic. In a complex appli- any technology in these categories, but where the details of a cation, business logic is likely to be the largest component and specific technology are important, we use servlets and Java- can include code that accesses external systems such as data- Server Pages as representative technologies. Both, while nomi- bases, external components, and other services. In the Model- nally Java specifications, can easily be applied to other lan- View-Controller framework, this corresponds to the model. guages, and implementations in both Smalltalk and C++ are commercially available. Presentation This layer contains code and noncode resources (such as Layered architecture HTML text and images) used to present the application. It typi- A layered architecture is a system containing multiple, cally contains little code—code concerned only with formatting strongly separated layers, with minimal dependencies and in- and presenting data. An example of this in a Web context is a teractions between the layers. Such a system has good separa- server page’s code fragments that print values into a dynami- tion of concerns, meaning that we can deal with different areas cally generated Web page. In the Model-View-Controller of the application code in isolation, with minimal or no side ef- framework, this corresponds to the view. fects in different layers. By separating the system’s different pieces, we make the software adaptable so that we can easily Scripts change and enhance it as requirements change. The layers we Many basic Web technologies can be grouped together in are concerned with here include input, application logic, busi- the category of scripts—small programs that perform HTTP pro- ness logic, and presentation. cessing. This term encompasses, among others, compiled CGI programs, files of scripting language code (such as Perl, Input Python, Ruby, and VBScript), and Java servlets. While there are The input layer contains the code concerned with processing significant differences among these technologies, all of them and syntactically validating input. In a Web context, this pro- have the fundamental characteristic of being programs that ac- cessing and syntactically validating input includes HTTP input cept an HTTP request and send back a response. In basic us- parsing and extracting parameters from an HTTP request. In the age, each script is stateless and independent of all others. An Model-View-Controller framework, this corresponds to the input important distinction within this category is whether scripts can controller. share memory with other scripts (for example, servlets) or are entirely independent (for example, CGI programs). Shared Application logic memory allows more effective use of system resources through The application logic code is concerned with the applica- pooling, and a simpler programming model through persistent tion’s overall flow. We often refer to it as the glue layer, sepa- states at the cost of a more complex infrastructure. rating business logic from input and presentation logic and managing the interface between the two. This requires some Server pages knowledge of both layers. For example, this layer would be in- An alternative mode of HTML scripting is that of an HTML volved in converting between presentation-level inputs and out- page annotated with small amounts of code. Many scripting lan- puts as strings and the corresponding business object messages guages support this kind of facility in addition to pure scripts, and or state. This layer might also manage a multipage Web inter- representative examples include JavaServer Pages (JSP), Mi- action as a sequence of steps. In the Model-View-Controller crosoft’s Active Server Pages, PHP, and Zope. There are also vari- framework, this corresponds to the application controller. ations on this approach in which the pages are annotated with application-specific HTML or XML tags, and developers can spec- Business logic ify code to be run when these tags are encountered. Examples of The business logic code is concerned only with the underly- this include JSP bean and custom tags and Enhydra’s XMLC. it harder to modify or test any particular Accepting input aspect in isolation. In addition, there are When accepting input, a script receives ei- significant issues related to handling these ther the raw HTTP input stream or a mini- responsibilities, as described below. For mally parsed representation of it. HTTP sup- server pages used alone, the same issues ap- ports several different mechanisms for ply (because we can consider a server as a passing parameters (encoding into the URL, script with some additional embedded query values, or form data), and all of these HTML), and mixing code with the HTML pass the data as simple strings. Each script also presents code management and debug- must know or determine the parameter-pass- ging issues. ing mechanism, convert the parameters to 52 IEEE SOFTWARE March/April 2002 Authorized licensed use limited to: Universidad Federal de Pernambuco. Downloaded on June 18,2010 at 23:56:58 UTC from IEEE Xplore. Restrictions apply. appropriate types, and validate them. This debug inside complex generated code. For causes code duplication between scripts. these reasons, it is a good idea to minimize the amount of code in server pages and to keep It is a good idea Handling application logic application logic out of the pages. Another issue, which affects both input to minimize the and application logic, is the lack of informa- Handling business logic amount of code tion hiding when accessing request and ses- Because all of the code is grouped to- in server pages sion data. The script must retrieve input data gether, it is difficult to isolate the business from the request by name. HTTP is a state- logic from the other layers, particularly ap- and to keep less protocol, so data used in multiple scripts plication logic. Furthermore, unless we can application logic must be either stored in a session identified run portions of the scripts separately, it is with the user or reread from an external data impossible to test the business logic (or any out of the pages. source in each script requiring the data. For of the other layers) independently. example, if a script passes login information With server pages, business logic presents as form data, the code to store that informa- the same issues that we discussed for appli- tion in the session might be cation logic. We can very quickly have too much code in the pages, and even pages password = request.getParameter with minimal code are difficult to manage (“passwordField”); and debug. decrypted = this.decode (password); Generating output request.getSession().putValue In producing output, simple scripts mix (“password”,decrypted); the HTML encoding of the result with the dynamic data. This couples the page’s look Both storage in the session and storage in and feel with the other layers. Changing the an external data source are effectively global Web site’s look or adapting the application to in scope, and the application accesses them in multiple output devices becomes extremely dictionary-like fashion using strings as keys.