2021-12-11 07:12


A Comparative Study of Web Application Design Models

Using the Java Technologies


The Servlet technology has been the most widely used technology for building scalable Web applications. In the events, there are four design models for developing Web applications using the Java technologies: Model 1, Model2, Struts, and JavaServer Faces (JSF). Model 1 employs a series of JSP pages; Model 2 adopts the Model-View-Controller pattern; Struts is a framework employing the Model 2 design model; and JSF is a new technology that supports ready-to-use components for rapid Web application development. Model 1 is not recommended for medium-sized and large applications as it introduces maintenance nightmare. This paper compares and evaluates the ease of application development and the performance of the three design models (Model 2, Struts, and JSF) by building three versions of an online store application using each of the three design models, respectively.

1 Introduction

Today, Web applications are the most common applications for presenting dynamic contents. There are a number of technologies for building Web applications, the most popular of which is the Servlet technology . This technology gains its popularity from its superiority over other technologies such as CGI and PHP .Servlets are cumbersome to develop, however, because sending HTML tags requires the programmer to compose them into a String object and send this object to the browser. Also, a minor change to the output requires the servlet to be recompiled. To address this issue, Sun Microsystems invented JavaServer Pages (JSP) . JSP allows HTML tags to be intertwined with Java code and each page is translated into a servlet. A JSP page is a servlet. However, compilation occurs automatically when the page is first requested. As a result, changing the output does not need recompilation. In addition, JSP enables the separation of presentation from the business logic through the use of JavaBeans and custom tag libraries. The norm now in developing Javabased Web applications is to use servlets along with JavaServer Pages.

In the later development, there are a number of design models for building servlet/JSP applications: Model 1, Model 2, Struts , and JSF . Model 1 and Model 2 were first mentioned in the early specifications of JSP. Model 1 strictly uses JSP pages, with no servlets, and Model 2 uses the combination of both servlets and JSP pages. The terms of Model 1 and Model 2 have been used ever since. Model 1 is suitable for prototypes and very small applications, and Model 2 is the recommended design model for medium sized and large applications.

As Model 2 gained more acceptances in the industry, an open source initiative to build the Struts Framework was initiated. Struts perfects Model 2 by providing the controller part of the Model-View-Controller of Model 2. In addition, Struts provides better page navigation management and several custom tag libraries for more rapid development. Despite its steep learning curve and the fact that it was never defined in any specification, Struts has been gaining popularity as the alternative to Model 2.

JavaServer Faces is built under the Java Community Process under JSR-127.Sun Microsystems proposed this technology in the hope that JSF will be the ultimate model for building Java Web applications. The most important feature of JSF is the availability of ready-to-use components such as extensible UI components, easy page navigation, input validators, data converters and JavaBeans management.

The problem facing servlet/JSP programmers are to choose the most appropriate design model. Clearly, JSF provides a better solution in regard to development time. However, some people are not sanguine to adopt this technology for fear of performance penalty due to the overhead of the JSF implementation.

We build three versions of an online store application named BuyDirect using Model 2, Struts and JSF. The parameters compared are the number of lines of code, the number of classes, and the performance measurement results. We investigate which of the design models allows the most rapid development process. We evaluate the performances of the applications built upon these models. We provide some suggestions to perfect the existing design models to make development more rapid.

The rest of the paper is organised as follows. Section 2 discusses the issues in Web development. Section 3 explains how the three design models address these development issues. Section 4 provides the details of the hardware and software used in these experiments. Section 5 presents the experiment results and analysis. Section 6 reviews the related work. Section 7 concludes by offering some suggestions to improve the existing design models.

2 Java Web Development Issues

All Java Web development uses the Servlet technology as the underlying technology. As such, all Java Web applications have certain issues that need to be addressed:

  • User Interface. The user interface is what the client browser renders as HTML tags. Any server-side component used in the application must be encoded into the corresponding HTML elements. Besides for displaying the content and data, the user interface is also responsible in receiving input from the user.
  • Input Validation. User input needs to be validated. There are two types of input validation, server-side and client-side. As the name implies, the server-side input validation is performed on the server after the input reaches the server. Client-side input validation is done on the browser, usually by using JavaScript or other scripting languages. The advantages of using client-side input validation are prompt response and reducing the server workload. The server-side input validation should always be performed re



    Servlet技术在建立可扩展性Web应用中是被应用最广泛的技术。在运用JAVA技术开发Web应用中有四种模型,分别是:Model 1、Model 2、Struts和JavaServer Faces(JSF)。Model 1使用一连串的JSP页面;Model 2采用了模型-视图-控制器(MVC)模式;Struts是一个采用了Model 2设计模型的框架;JSF是一种支持ready-to-use组件来进行快速Web应用开发的新技术。Model 1对于中等和大型的应用来说很难维护,所以不推荐使用。本文通过利用Model 2、Struts和JSF这三种模型分别构建三个不同版本的在线商店应用程序,来比较和评价这三种模型在应用程序开发和性能上的差异。


    当今,Web应用是一种展现动态内容的最普遍的方式。构建Web应用有许多种方法,其中最流行的是Servlet技术。这种技术的流行是因为它比CGI、PHP等其他技术更具优越性。然而Servlet对于开发来说还是麻烦的,因为它在传送HTML标签时需要程序员将他们组合成为一个字符串对象,再将这个对象传给浏览器。同样的,对于输出的一个很小的改动也要求Servlet被重新编译。基于这个原因,SUN公司发明了JavaServer Pages(JSP)技术。JSP允许HTML标签和Java代码混合在一起,每个页面将被转化为一个Servlet,一个JSP就是一个Servlet,而且编译将在JSP页面第一次被请求时自动进行,改变输出也不需要重新编译。

    另外,通过使用JavaBean和定制标签库,JSP能够将表示与业务逻辑相分离。 现今,基于JAVA的Web应用开发标准是将servlets与JSP结合在一起。随后,出现了许多种设计模型用来构建servlet/JSP应用:Model 1、Model 2、Struts]和JavaServer Faces(JSF)。Model 1和Model 2最早是在JSP规范中被提及的。Model 1只使用JSP而不使用servlet,Model 2则结合了JSP与servlet。Model 1和Model 2的使用是有条件的,Model 1适合与开发原型和非常小的应用,Model 2则是开发中型和大型应用推荐的设计模型。由于Model 2越来越被行业所接受,一个建立Struts框架的开源项目也因此被启动了。Struts通过为Model 2提供了模型-视图-控制器中的控制器来完善Model 2。另外,Struts提供了更好的页面导航管理机制和一些定制标签库,能够进行更快速的开发。尽管它学习难度大,并且,实际上它没有在任何的规范中被定义,但是它还是作为Model 2的一种替代获得了流行。JavaServer Faces是在JCP的JSR-127规范下被建立。Sun公司力推这项技术,希望它能够成为构建Java Web应用的最终模型。JSF最重要的特性是对ready-to-use组件的支持,比如:可扩展用户接口组件、简易的页面导航、输入验证、数据转换和JavaBean管理机制。servlet/JSP程序员面临的问题是选择最合适的设计模型。明显的,JSF在开发时间上提供了更好的解决方案。然而,有些人担心实施JSF的开销所带来的性能下降而不愿采用这种技术。

    我们使用Model 2、 Struts和JSF分别构建三个不同版本的在线商店应用,比较的参数是:代码的行数、类的数目和性能测试结果。





    所有的Java开发都使用Servlet技术作为基础技术。同样的,所有的Java Web应用都有一些问题需要解决:






    Model 2设计模型是基于模型-视图-控制器(MVC)设计模式的。正如Burbeck所解释的,在MVC中有三个主要模块:控制器,视图和模型。控制器作为应用的中心,所有的用户交互都要通过它。视图包含了应用的显示,而模型用来存储数据和封装应用的业务逻辑。随后,Struts框架提供了一个通用框架能够容易的构建Model 2应用。最主动的是同样使用MVC设计模式的JSF。在之后的章节,我们将来讨论这三种设计模型,并且说明每一种设计模型是怎样来解决之前提到的开发问题。

    3.1 Model 2

    一个基于Model 2设计模型的Java Web应用有一个作为控制部分的servlet(称为控制器servlet)。所有的请求首先被这个servlet处理,它通过RequestDispatcher对象将请求迅速的分派到合适的视图。在Model 2中视图是通过JSP页面来表现的。JavaBean作为Model 2 的模型部分,用来在应用中存储数据。除了存储数据,JavaBean还用来封装业务逻辑。每个HTTP请求都带有一个行为参数,用来指出哪个视图指派给这个请求。程序员必须在所有的JSP页面中为用户接口编写HTML标签代码,并且编写输入验证代码。此外,模型对象被单独的JSP页面所管理。

    3.2 Struts

    Struts框架是Model 2设计模型的一个改进。它提供了一个默认的控制器servlet,使得程序员不用非得去编写一个。Struts允许在应用配置文件(一个XML文件)中编写导航规则来减轻页面导航的任务,改变导航规则不需要重新编译。除了更简单的页面导航,Struts还提供了定制标签库来定义标签代表HTML元素。这些标签中的一些用来处理异常,而且Struts支持国际化,能够显示本地化的错误消息。就像Model 2一样,Struts使用JavaBean作为模型。此外,Struts程序员必须编写输入验证代码。

    3.3 JSF

    JSF同样使用了一个控制器servlet,称为FacesServlet。这个Servlet是整个JSF应用的唯一入口。JSF同样使用JSP页面作为它的视图,使用JavaBean作为模型对象。与Model 2和Struts不同,JSF提供了能够写入JSP页面的ready-to-use的用户接口组件。在JSF应用中的页面的调用,FacesServlet建立了一个组件树来显示JSP页面的请求。大部分的组件还能够触发事件,来构成JSF的事件驱动。对于页面导航,JSF使用了和Struts类似的方法,即允许在一个应用控制文件(XML文件)中定义导航规则。




    4.1 Servlet容器

    Java Web应用是在servlet容器中运行的,在应用中它一个引擎,用来处理接踵而来的请求资源的HTTP请求。在这个试验项目中,我们使用Tomcat-一个来自Apache软件基金会的开源的servlet容器,版本号是6.0。基本上,一个servlet容器通过执行下面的任务来处理一个servlet:

    • 创建一个HttpRequest对象
    • 创建一个HttpResponse对象
    • 传递HttpRequest、HttpResponse对象来调用Servlet接口的service方法

    4.2 测试用客户端


    4.3 硬件

    为了得到最大的性能度量精度,我们使用不同的计算机来进行应用的测试。运行应用的是一台XP计算机,它的配置如下:Intel Core 1GHz CPU 、1G RAM。测试用客户端是运行JMeter的Windows2000计算机,具体配置如下:Intel Core 1GHz CPU、 1G RAM。




    5.1 应用开发的难易程度

    就如表1显示的,应用Model 2设计模型需要花费最多的努力,使用Struts能够使问题减轻一点,使用JSF最省力。

    表1 应用中类的数目和代码行数

    Model 2 设计模型的特点是存在一个控制器servlet和多个JavaBean类(作为模型)及JSP页面(作为视图)。控制器类通过一系列的if语句来负责页面导航规则。Model 2程序员还必须为输入验证编写代码,在这个实验中是通过在内部编写许多定制标签库来完成的。在Model 2设计模型中的其它类是定制标签库,这些标签是用来负责输入验证和数据显示的。实际上,输入验证使用了590行代码,大约是所有代码的30%。

    在Struts应用中,控制器servlet是由框架提供的,所以Struts程序员不用编写它,节省了时间。然而,他仍然需要在应用控制文件中编写导航规则,这比编写一个servlet容易些,因为应用控制文件可以用文本编辑器编辑,不必编译。虽然Struts框架提供了错误处理机制,但仍然需要手工编写输入验证。用来进行输入验证的类的数量和代码的行数与Model 2应用的很相似。Struts中其他的类是Action类,用来调度请求到默认的控制servlet。


    5.2 性能度量


    5.2.1 查询操作


    图2 查询操作的性能比较

    在Model 2应用中,对于一个用户,服务器平均的响应时间是173毫秒,对于10个用户是919毫秒。在Struts应用中,相对地,其值是189毫秒和900毫秒,在JSF中则是210毫秒和932毫秒。响应时间随着并发用户数成比例的增加,说明了服务器仍然能够应付装载。

    Model 2应用开销最小,所以它的平均性能比Struts和JSF更好。然而,Struts的性能同Model2 的相同,这是因为服务器有足够的内存用来加载运行Str


原文和译文剩余内容已隐藏,您需要先支付 20元 才能查看原文和译文全部内容!立即支付