admin 管理员组

文章数量: 1086019


2024年4月24日发(作者:学校介绍ppt模板)

外文文献资料

A Comparative Study of Web Application Design Models

Using the Java Technologies

Abstract.

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

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 regardless

the presence of client-side validation because there is no guarantee the user browser's

scripting feature is being on and malicious users can easily work around client-side

validation.

 Model Objects. Model objects in Java-based Web applications are in the forms of

JavaBeans. Model objects make up the Model part of the MVC based design model. A

model object can be used to bind a component value to be used at a later stage. In

addition, it can encapsulate business logic required for processing.

 Page Navigation. Almost all Web applications have multiple pages that the user can

navigate from one to another. All MVC-based design models use a servlet as the Controller

part. This servlet also acts as the sole entry point to the application. Which page to be

displayed after the current request is determined by the value of a specified request

parameter. Managing page navigation is critically important.

3 Web Application Design Models

The Model 2 design model is based on the Model-View-Controller (MVC) design

pattern. As explained by Burbeck , there are three main modules in MVC, the Controller, the

View, and the Model. The Controller acts as the central entry point to the application. All

user interactions go through this controller. The View contains the presentation part of the

application, and the Model stores data or encapsulates business logic of the application. In

the later development, the Struts Framework provides a common framework to easily build

Model 2 applications. Then, the last initiative is the JavaServer Faces, which also employs

the MVC design pattern.

In the following sections, we discuss these three design models and explain how each

design model addresses the development issues specified in the previous section.

3.1 Model 2

A Java Web application that is based on the Model 2 design model has one

servlet(called the Controller servlet) that serves as the Controller part. All requests are first

handled by this servlet, which immediately dispatches the requests to the appropriate

views using RequestDispatcher objects. Views in the Model 2 design model are

represented by JSP pages. To store data, a Model 2 application uses JavaBeans, which are

the Model part of the application. In addition to storing data, the JavaBeans also

encapsulate business logic. Each HTTP request carries an action parameter that indicates

which view to dispatch this request to. The programmer must code the HTML tags for user

interface in all JSP pages in the application and write input validation code. In addition, the

model objects are managed by individual JSP pages.

3.2 Struts

The Struts Framework is an improvement of the Model 2 design model. It provides a

default Controller servlet so that the user does not have to write and compile one. Struts

alleviates the task of page navigation by allowing navigation rules to be present in its

application configuration file (an XML document). Changes to the navigation rules do not

require recompilation of a Java servlet class. In addition to easier page navigation, Struts

provides custom tag libraries that define tags representing HTML elements. One of these

tags is used for error handling and Struts is therefore capable of displaying localized error

messages in support for internationalization. Struts applications use JavaBeans as their

models, just like the Model 2 design model. In addition, Struts programmers have to write

their own input validation code.

3.3 JSF

JSF also employs a controller servlet that is called FacesServlet. This servlet is the only

entry point to a JSF application. JSF also uses JSP pages as its views and JavaBeans as its

model objects. Unlike Model 2 and Struts, however, JSF provides ready-to-use user

interface components that can be written on JSP pages. Upon an invocation of a page of a

JSF application, the FacesServlet constructs a component tree that represents the JSP page

being requested. Some of the components can also trigger events, making JSF

event-driven. For page navigation, JSF uses an approach similar to Struts, i.e., by allowing

navigation rules to be defined in an application configuration file (again, an XML

document).

What distinguishes a JSF application from non-JSF servlet/JSP application is that JSF

applications are event-driven. The user interface of a JSF application is one or many JSP

pages that host Web components such as forms and input boxes. These components are

represented by JSF custom tags and can hold data. A component can be nested inside

another, and it is possible to draw a tree of components. Just as in normal servlet/JSP

applications, you use JavaBeans to store the data the user entered.

4 Function Environment

The software and hardware details for our experiments are described below.

4.1 The Servlet Container

A Java Web application runs in a servlet container, which is the engine that processes

the incoming HTTP requests for the resources in the application. For this research project,

we use Tomcat, an open source servlet container from the Apache Software Foundation.

The version we use is lly, a servlet container processes a servlet by performing

the following tasks:

- Creating the HttpRequest Object

- Creating the HttpResponse Object

- Calling the service method of the Servlet interface, passing the HttpRequest and

HttpResponse objects.

4.2 Testing Clients

For performance testing, we emulate multiple users using JMeter 1.9 , also from the

Apache Software Foundation. JMeter allows the user to choose the number of threads to

perform testing. Each thread emulates a different user. JMeter also lets us choose how

many times a test will be done. To test a Web application using JMeter, you direct requests

to certain IP address, context path, and port number. You can also specify request

parameters to be included in each HTTP request. As the output, JMeter notifies the

response time of the server in milliseconds for a test. From the response time, we derive

the number of hits/seconds the server is capable of serving.

4.3 Hardware

We use different computers for running the applications and for testing, so as to

obtain maximum performance measurement accuracy. The computer running the

application is a XP machine having the following hardware specifications: Intel Core 1GHz

CPU with 1G RAM. The computer running the testing clients is a Windows 2000 machine

running JMeter. The computer has the following specifications: Intel Core 1GHz CPU with

1G RAM.

5 Results

We obtain experimental results in two categories: the ease of development and

performance. The ease of development category compares the number of classes and the

number of lines of code. These numbers indicate how easy it is to develop an application

by following a certain design model. An application with the fewer number of classes or

the number of lines of code indicates that the application is relatively easier to build. The

application with the more number of classes indicates that the application takes more time

to develop.

The performance measurement results are obtained by comparing two operations.

The Search operation is the most common operation in such an application,and the

Browse operation.

5.1 Ease of Application Development

As Table 1 shows, it takes the most effort to implement the Model 2 design model.

Using Struts alleviates the problem a bit, and the best saving in the development comes if

one uses JSF.

Table 1. The number of classes and the number of lines for the applications under

study

The Model 2 design model is characterised by the presence of a Controller servlet and

a number of JavaBeans classes (as the Model) and JSP pages (as the Views). The Controller

servlet is responsible for page navigation rules that employ a series of if statements. Model

2 application programmers must also code for the input validation that in this research is

implemented inside a number of custom tag libraries. The other classes in the Model 2

design model are custom tag library and the tag library descriptors responsible for input

validation and data display. In fact, input validation takes 590 lines of code, or almost 30%

of the total amount of code.

In the Struts application, the Controller servlet is provided by the framework, therefore

a Struts programmer saves time for not having to write one. However, he/she still needs to

write page navigation rules in the Application Configuration file, which is easier than

writing a servlet because the Application Configuration file can be edited using a text

editor and no compilation is necessary. Input validation must still be done manually, even

though the Struts Framework provides an error handling mechanism. The number of

classes and the number of lines of code for input validation are almost similar to the Model

2 application. In Struts, the other classes are Action classes to which the default Controller

servlet dispatches requests.

In JSF input validation comes free through the availability of validator component. As a

result, a JSF application developer can skip this task. In addition, page navigation takes the

same course as Struts, i.e. by utilising an Application Configuration file. The other classes in

JSF are a ContextListener, an ActionListener, and a Database utility class.

5.2 Performance Measurement

For each operation, we measure the server response time (in milliseconds) for 1 to 10

concurrent users. The number of users is specified by setting the number of threads in

Jmeter. Each test is conducted 10 times and the average is taken. Each operation is

discussed further is the following sub-sections.

5.2.1 Search Operation

The Search operation whose name or description matches the keyword. There is one

SQL SELECT statement performed. Figure 2 compares the three versions of applications for

the Search operation.

Fig. 2. The performance comparison for the Search operation

For the Model 2 application, the average server response time for one user is 173 ms

and for 10 users is 919 ms. For the Struts application, these numbers are 189 ms and 900

ms, respectively. For the application built using JSF, the average server response time is 210

ms for one user and 932 ms for 10 users. The increase of the response time is proportional

to the increase of the number of concurrent users, which means that the server is still able

to cope with the load.

The Model 2 application has the least overhead, therefore the average performance

should be better than the Struts and JSF applications. However, the Struts application

performs as well as the Model 2 application. This is because the server has enough

memory to load all Struts libraries required to run Struts. Also, note that page navigation

rules in Struts are loaded and stored in an object called ActionMapping. Therefore, given

an action request parameter, the next page of navigation is obtained through a look-up.

On the other hand, the Model 2 application uses a series of if statements to find the next

page of navigation, given the action request parameter.

The JSF application performs slightly worse than the other applications in almost all

numbers of concurrent users. This could be due to the time taken by the JSF

implementation to construct a component tree for each page requested. However, the

difference in server response time between JSF and other applications is not that

significant.

5.2.2 Browse Operation

The Browse operation,like the Search operation, there is one SQL SELECT statement

performed. Figure 3 gives the test results for this operation.

Fig. 3. The performance comparison for the Browse operation

On average, the Model 2 application performs the best because it has the least

overhead. The average server response time is 111 ms for one user and 899 ms for 10 users.

The Struts application has comparable performance, with one user average server

response time of 180 ms and 10 user response time of 920 ms. The JSF lacks a bit behind

the two applications with these numbers being 190 and 1009 ms respectively. The increase

of the server response time is proportional to the increase of the number of concurrent

users, which means the server is able to serve those users well. The average performance

measurement results of the Browse operation are very similar to the ones for the Search

operation because the database operations of both operations are also similar.

6 Related Work

Compare the performance of database-based Web applications using Java servlets,

PHP version 3, and Common Gateway Interface (CGI). After a series of benchmark tests that

performs data retrieval from a MySQL database, find that the solution of Java servlets

with persistent database connection has the best performance. PHP3 using persistent

database connections performs fairly well when compared to the CGI solution,also

mention the advantages of using Java servlets. According to these authors. Java servlets

are an excellent choice to meet the requirement of e-commerce (such as online shopping)

applications and are able to handle client requests in a highly interactive mode.

Comparing PHP 4, Java servlets, and Enterprise JavaBeans. Measure the performance

of these three architectures using two applications. Study reveals that PHP4 is more

efficient than Java servlets, and the EJBs perform even worse than servlets. However, note

that servlets, being part of the Java solution, provides the flexibility of being able to be

ported to another system with a different operating system.

7 Conclusion

We find that it is most rapid to build Web applications using JSF. Model 2 applications

are the least rapid but give the best performance. Struts applications sit in the middle of

the other two design models in both comparisons.

We make some suggestions that could improve the Servlets technology in general and

enhance the performance of applications based on both design models.

 Struts. Struts is not based on any specification and there is no documentation that

discusses its internal working. Therefore, it is hard to know what have been implemented

and what could be improved.

 The Servlets Technology. The Servlet 2.3 Specification does not define any caching

mechanism. There is no mention of caching in the upcoming Servlet 2.4 Specification

either. Despite the dynamic nature of the content of a Web application, some contents do

not change very often. For example, the categories of products that a user can browse in

an online store application probably only change once in a month. If those semi-static

contents must be generated from the database every time they are requested, a lot of

programming resources will be wasted. Servlet programmers get around the absence of

caching by writing an object that caches certain content. However, since there is no

standard for caching, many programmers write the same piece of code again and again.

 Model main drawback is that the page navigation rules are hard-coded in the

Controller servlet. This means any minor change to the program flow will require the

Controller servlet to be re-compiled. The solution to this problem is to provide a mapper

that reads the page navigation rules when the application starts. The code could be

conveniently written in the init method of the Controller servlet. This method is only

executed once, i.e. the first time the servlet is loaded into memory. If the properties file

needs to be re-read every time it changes, the programmer can check the timestamp of

the properties file for each request, and compares it with the previous read of this file. If

the timestamp is more current than the previous read, the mapper can be re-constructed.

This feature can be enabled and disabled by using an initial parameter in the Context

object. At the development phase, this feature should be enabled. At deployment, this

feature should be off. The use of the properties file to store the page navigation rules also

makes it possible to avoid a series of if statements in the Controller Servlet, which can be

time-consuming for every request. Instead, a HashMap can be used, with action request

parameters as keys and the next JSP pages as values. The other disadvantage of this design

model is the absence of standard components for input validation and user interface.

However, this has been solved in JSF.

 JSF. JSF provides solutions to common problems in Web development, such as page

navigation management, UI components and input validators. However, because this

technology is still very young, there are not too many UI components available, forcing

programmers to combine JSF with non-JSF servlets/JSP pages. JSF is event-driven. JSF

programmers determine the behavior of a JSF application by writing event listeners, just

like those listeners in a Swing application. In JSF version 1.0, there are currently two types

of events that can be triggered: ActionEvent and ValueChangedEvent. However, this is

good enough to provide sufficient level of interactivity between the application and its

users. Adding more types of events will definitely make JSF more appealing.

References

[1].Burbeck, S., Applications Programming in Smalltalk-80: How to use

Model-View-Controller(MVC),/users/smarch/st-docs/,

1987.

[2].Cecchet, E., Chanda A., Elnikety S., Marguerite J., Zwaenepoel W.: Performance

Comparison of Middleware Architectures for Generating Dynamic Web Content.

Proceeding of the 4th International Middelware Conference, 2003.

[3].Cecchet, E., Marguerite, J., and Zwaenepoel, W.: Performance and Scalability of

EJB Applications. Proceedings of OOPSLA’02, 2002.

[4].Java Servlet 2.3 and JavaServer Pages 1.2 Specification (JSR-053),

/aboutJava/communityprocess/final/jsr053/.

[5].Java Servlet 2.4 Specification (Proposed Final Draft 3),

/aboutJava/communityprocess/first/jsr154/.

[6].JavaServer Faces Technology, /j2ee/javaserverfaces/.

[7]. JBoss EJB server, .

[8].JMeter, /jmeter/.

[9].JOnAS: Java Open Application Server, /jonas.

[10].Sun Microsystems, Comparing Methods for Server-Side Dynamic

Content,/products/jsp/, 2000.

[11].The Apache Software Foundation, .

[12].The Struts Framework, /struts/.

[13].Wu, A., Wang, H., and Wilkins, D.: Performance Comparison of Alternative

Solutions for Web-To-Database Applications. Proceedings of the Southern Conference on

Computing, the University of Southern Mississippi, 2000.

基于Java技术的Web应用设计模型的比较研究

摘要

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这三

种模型分别构建三个不同版本的在线商店应用程序,来比较和评价这三种模型在应用程序开发和性

能上的差异。

1.绪论

当今,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分别构建三个不同版本的在线商店应用,比较的参数是:代

码的行数、类的数目和性能测试结果。

我们研究哪种设计模型能够进行最快速的开发。我们评估基于这些模型的应用的性能。我们提

供一些建议来完善现有的设计模型,使得开发速度更快。

文章的余下部分组织如下:

第二部分讨论了Web开发中的问题,第三部分解释了三个设计模型如何来解决这些开发问题,

第四部分详细介绍了试验所需要的硬件和软件,第五部分给出试验的结果并进行分析,第六部分回

顾相关工作,第七部分得出结论并提出了一些改进现有设计模型的建议。

开发中的问题

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

题需要解决:

1.用户接口是在客户浏览器上呈现出的HTML标签。任何在应用中使用的服务器端组件都必须

被编码成为正确的HTML标签。除了显示内容和数据外,用户接口还负责接收用户的输入。

2.用户的输入必须要验证。输入的验证有两种类型:服务器端和客户端。顾名思义,服务器端

的输入验证是在输入的数据到达服务器后,在服务器上进行的。客户端的输入验证是在浏览器上完

成的,一般使用JavaScript或其他脚本语言。利用客户端进行输入验证的好处是反应迅速,而且能

够减轻服务器的负载。无论是否存在客户端输入验证,服务器端的输入验证是一定要执行的,因为

无法保证用户的浏览器的脚本特性被打开,而且恶意用户能够很轻松的绕过客户端的验证。

3.在基于Java的Web应用中模型对象是以JavaBean的形式出项的。模型对象组成了基于MVC

设计模型的模型部分。一个模型对象能够用来绑定一个组件的值,以备下阶段使用。此外,它能够

封装需要执行的业务逻辑。

4.大多数的Web应用都包含多个页面,能够使用户从一个页面跳转到另一个页面。所有的基于

MVC的设计模式都使用Servlet作为控制部分。这个Servlet也作为进入应用的唯一入口。当前的请

求之后哪个页面会被显示取决于请求参数的具体值。管理页面导航是至关重要的。

应用设计模型

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

文件)中定义导航规则。

JSF应用与非JSF的servlet/JSP应用的不同在于JSF是事件驱动的。JSF的用户接口是一个或多个

JSP页面,用来服务诸如表单和输入框等Web组件。这些组件以JSF定制标签的形式出现而且能够保

存数据。一个组件能够嵌套在另一个组件中,有可能形成一个组件的树状结构。在普通的servlet/JSP

应用中,使用JavaBean来存储用户输入的数据。

4.运行环境

以下是我们实验的软硬件详细描述。

4.1 Servlet容器

Java Web应用是在servlet容器中运行的,在应用中它一个引擎,用来处理接踵而来的请求资

源的HTTP请求。在这个试验项目中,我们使用Tomcat-一个来自Apache软件基金会的开源的

servlet容器,版本号是6.0。基本上,一个servlet容器通过执行下面的任务来处理一个servlet:

 创建一个HttpRequest对象

 创建一个HttpResponse对象

 传递HttpRequest、HttpResponse对象来调用Servlet接口的service方法

4.2 测试用客户端

为了进行性能测试,我们使用同样来自Apache软件基金会的JMeter1.9来模拟多用户。JMeter

允许用户选择一定数量的线程来执行测试。每个线程模拟了一个不同的用户。JMeter来能允许我们

选择完成一次测试的测试次数。使用JMeter来测试一个Web应用,你只要直接向某个IP地址、上下

文路径和端口号发出请求。你也可以将请求参数包含在HTTP请求中。对于一个测试,JMeter将服

务器的响应时间以毫秒的形式输出。从响应时间,我们能够得到服务器执行服务的每秒命中次数。

4.3 硬件

为了得到最大的性能度量精度,我们使用不同的计算机来进行应用的测试。运行应用的是一台

XP计算机,它的配置如下:Intel Core 1GHz CPU 、1G RAM。测试用客户端是运行JMeter的

Windows2000计算机,具体配置如下:Intel Core 1GHz CPU、 1G RAM。

5.结论

我们得到两类实验结果:开发的难易程度和性能。开发的难易程度比较了类的数目和代码的行

数。这些数目显示了遵从某个设计模型来开发一个应用的难易程度。一个应用包含较少的类和较少

的代码行数说明应用相对而言更容易构建。一个应用包含较多的类说明应用需要花费更多的时间来

开发。

性能度量结果是通过比较这两个操作来获得的。查找操作是一个应用中最普通的操作以及浏览

操作。

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。

在JSF中,提供使用验证组件而不用编写输入验证,所以JSF应用开发人员可以跳过这个任务。

此外,页面导航和Struts相同,都是使用一个应用控制文件。在JSF中,其他的类是ContextListener、

ActionListener和数据库工具类。

5.2 性能度量

对于每一个操作,通过1-10个并发用户,我们来测量服务器的响应时间(以毫秒形式)每个

用户在JMeter中设定了一定数量的线程。每次测试进行10次,取平均值。每个操作在下面的章节中

讨论。

5.2.1 查询操作

查询操作,其名称或描述与关键字匹配。它将执行一个SQL的SELECT语句。图2比较了三个版

本的应用的查询操作。

图2 查询操作的性能比较

在Model 2应用中,对于一个用户,服务器平均的响应时间是173毫秒,对于10个用户是919

毫秒。在Struts应用中,相对地,其值是189毫秒和900毫秒,在JSF中则是210毫秒和932毫秒。响

应时间随着并发用户数成比例的增加,说明了服务器仍然能够应付装载。

Model 2应用开销最小,所以它的平均性能比Struts和JSF更好。然而,Struts的性能同Model2

的相同,这是因为服务器有足够的内存用来加载运行Struts需要的Struts库。同时,应该注意到Struts

中的页面导航规则是加载和存储在一个叫ActionMapping的对象中。因此,给出一个请求参数,导

航到下个页面是通过查找得到的。另一方面,给出一个请求参数,Model 2应用是利用一系列的if

语句来得到下一个页面的导航。

在几乎所有的并发用户数字中,JSF应用的性能都稍逊于其他应用。这可能是由于JSF将时间消

耗在为每个请求构建一颗组件树上。但JSF的服务器响应时间与其他应用差距并不明显。

5.2.2 浏览操作

三个版本的浏览操作,如同查询操作,将执行一个SQL SELECT语句。图3这个操作的测试结果。

图3 浏览的性能比较

平均来说,Model 2应用的性能最好,因为它具有最少的开销。对于是一个用户服务器的平均

响应时间是111毫秒,10个用户是899毫秒。Struts应用有着同样的性能,对于是一个用户服务器的

平均响应时间是180毫秒,10个用户是920毫秒。JSF则稍落后与其他两个应用,相对地,它的时间

是190毫秒和1009毫秒。响应时间随着并发用户数成比例的增加,意味着服务器能很好地服务这些

用户。浏览地平均性能测量结果同查询操作很相似,这是因为这两种数据库操作类似。

6.相关工作

使用servlet、PHP(版本 3)、通用网关接口(CGI)来比较基于数据库的Web应用的性能。

经过执行从MySQL数据库返回数据的一系列的基准测试,发现Java servlet使用持久数据库连接的

方案性能最佳。PHP3使用持久数据库连接相对于CGI方案而言性能相当不,也同样提到了使用Java

servlet的优势。根据这些作者,Java servlet是迎合电子商务(比如在线商店)需求的最佳选择,

而且能够在高交互式模式中处理客户请求。

比较PHP4、Java servlet、和EJB。使用两个应用来测试了这三种结构,研究显示了PHP4比Java

servlet更有效,而EJB的性能比servlet差。然而,注意到作为Java方案的一部分的servlet提供了适

应性,能够移植到不同操作系统的其他系统上。

7.结论

我们发现使用JSF构建Web应用最迅速;Model 2最慢但性能最好;Struts应用在计较中处于其

他两种设计模型中间。

我们提出了一些建议,用来大体上改善Servlet技术和增强基于这些设计模式的应用的性能。

 Struts没有基于任何规范,也没有文档来讨论它的内部工作方式。因此,很难知道哪些实现

了以及哪些改善了。

 Servlet技术,Servlet2.3规范没有定义任何的缓存机制,也没有在即将到来的Servlet2.4中

提及。尽管Web应用的内容具有动态性,但有写内容也不是经常去改变了。比如,在在线商店的应

用中,用户可以浏览的商品种类可能每隔一个月才变动一次。如果这些半静态的内容必须每次在需

要的时候从数据库中获取,将浪费大量的编程资源。Servlet程序员通过编写一个对象缓存某些内容

来实现缓存。然而,只要没有缓存标准,许多程序员将反复的写相同的代码断。

 Model 2主要的缺点是页面导航规则在控制器servlet中是硬编码的。这意味着程序流程的任

何微小的改动就需要控制器servlet重新编译。解决这个问题的方法是在应用启动时,提供一个映射

器来读取页面导航规则。在控制器servlet的init方法中很容易添加代码。这个方法只执行一次,就

是在servlet首次被载入内存的时候。如果属性文件在每次改动后需要重新读取,程序员可以在每次

请求时检查属性文件的时间戳,将它于先前读入的文件比较。如果它比之前读入的文件的时间戳更

新,则重新构建映射器。这个特性可以在上下文对象的初始化参数中开启或者关闭。从开发的角度

来讲,这个特性应该开启。在部署时,这个特性应该关闭。使用属性文件来存储页面导航规则还可

能避免在控制器中的一连串的if语句,它将在每次请求时消耗时间。可以使用一个HashMap,将请求

参数作为Key值,将跳转页面作为Value值。这个设计模型的另一个缺陷是缺少标准的输入验证组件

和用户接口组件,而这些将在JSF中得到解决。

 JSF解决了开发Web应用中的大部分问题,比如:页面导航管理、用户接口组件和输入验证

组件。然而,由于这项技术仍然很年轻,还没有太多的用户接口可用,强迫了程序员将JSF和非JSF

的servlets/JSP页面相结合。JSF是事件驱动的,JSF程序员通过编写事件监听来决定JSF应用的行为,

就像Swing应用中的监听器一样。在JSF 1.0版本中,通常有两类事件能够被触发:ActionEvent和

ValueChangedEvent,但这已经能在应用和用户之间提供足够好的交互性了。

参考文献

[1].Burbeck, S., Applications Programming in Smalltalk-80: How to use

Model-View-Controller (MVC), /users/smarch/st-docs/,

1987.

[2].Cecchet, E., Chanda A., Elnikety S., Marguerite J., Zwaenepoel W.

Performance Comparison of Middleware Architectures for Generating Dynamic Web

Content. Proceeding of the 4th International Middelware Conference, 2003.

[3].Cecchet, E., Marguerite, J., and Zwaenepoel, W.: Performance and Scalability of

EJB Applications. Proceedings of OOPSLA’02, 2002.

[4].Java Servlet 2.3 and JavaServer Pages 1.2 Specification (JSR-053),

/aboutJava/communityprocess/final/jsr053/.

[5].Java Servlet 2.4 Specification (Proposed Final Draft 3),

/aboutJava/communityprocess/first/jsr154/.

[6].JavaServer Faces Technology, /j2ee/javaserverfaces/.

[7].JBoss EJB server, .

[8].JMeter, /jmeter/.

[9].JOnAS: Java Open Application Server, /jonas.

[10].Sun Microsystems, Comparing Methods for Server-Side Dynamic Content,

/products/jsp/, 2000.

[11].The Apache Software Foundation, .

[12].The Struts Framework, /struts/.

[13].Wu, A., Wang, H., and Wilkins, D.: Performance Comparison of Alternative

Solutions for Web-To-Database Applications. Proceedings of the Southern Conference on

Computing, the University of Southern Mississippi, 2000.


本文标签: 应用 模型 使用 页面 用户