JavaServer Faces vs. ASP.NET WebForms

JavaServer Faces vs. ASP.NET WebForms


Mike Richardson - ALTERthought, Inc.


Introduction:
Every year, the Microsoft and Java development communities have been attempting to develop methods to enhance developer productivity. Whether you are using the Microsoft Application Blocks for .NET or a number of the Jakarta frameworks like Struts and Ant, they have been designed to increase productivity and decrease the rate of development. Although .NET and Java have introduced many different frameworks, they are all striving for the same goal. That is, to bind the gap between rich client interfaces such as Swing and WinForms with web development paradigms. This gap has been created as a result of the HTTP protocol. Because the web is a stateless paradigm, it becomes very difficult to create rich user interfaces that can be bound easily to application events. For example, when developing in Swing and WinForms, it is a simple task to create UI components on a form and write code which reacts to events initiated by the user. This is why most highly interactive user interface applications are written in WinForms or Swing and not something which relies on the stateless nature of HTTP, like JSP or ASP.NET. With the introduction of ASP.NET WebForms and JavaServer Faces, this gap is beginning to close.
The purpose of this article is two-fold. First, I will explain how JSF and ASP.NET each implement their own set of UI event-driven web functionality even though they are both bound by the HTTP protocol. Second, I will discuss the differences in both technologies and their respective benefits and disadvantages.
ASP.NET and WinForms:
Although ASP.NET is a very loose implementation of the model-view-controller design pattern, it does follow several design patterns which make for an extremely productive, event-driven environment. For example, ASP.NET implements the Page controller pattern with its code-behind concept. Each page is composed of HTML and XML markup and a code-behind file which contains event handlers. By simply adding one line of code to the source file (a delegate binding), it is possible to "wire-up" any event to a control. ASP.NET 2.0, although it adds a host of new controls and security concepts, still follows the same design model. This model simplifies coding greatly, as developers can create web applications similar to classic Visual Basic programs. ASP.NET WebForms can be created by dragging controls onto forms and writing event handlers for these controls. The mechanism by which ASP.NET binds events is through a special class called a delegate. A delegate is a type-safe function pointer which allows the developer to call methods from user initiated actions. ASP.NET does all of this magic behind the scenes. The ASP.NET runtime is responsible for extracting event input from the WebForm to the server-side via HTTP, and then calling the appropriate developer specified method. The following illustrates creating a component and binding an appropriate event handler:
<input type=button id=”Button” value="test"
runat=server />
Button.ServerClick += new EventHandler(OnBlue);
All that is left to the developer is to implement the OnBlue method. The rest is handled by the framework and the ASP.NET runtime.
Recall that every .aspx page actually implements the Page object. A Page object contains zero or many controls, all of which render themselves as HTML code. If you are a control developer, you know that controls simply perform some type of logic, and then return HTML to the response stream. Basically, a Page object forms a logical control tree, and then asks each control to render itself. Each control then calls the corresponding render method and spits out HTML for any type of supported device or browser. The ultimate response stream contains all the HTML and JavaScript required to create the page output, including event binding information.
In ASP.NET, all events are initiated on the server-side, not the client side. It is important to understand that these server-side events are generated as part of the post-back sequence and are issued through the standard HTTP POST mechanism. As part of the HTTP payload, ASP.NET passes additional information with each POST request when server-side events are issued. In particular, there are two hidden fields on every .aspx page - the first contains an event target, and the second is populated with the event argument. When the POST is processed on the server, ASP.NET checks the contents of the hidden fields and fires events issued by the control whose ID is in the hidden field. This is how the HTTP protocol can be used to issue server-side events. This server-side event paradigm brings a familiar programming model to developers who are used to working with controls that issue events and render their current state, but may not be familiar with the disconnected nature of the HTTP protocol. Almost all of this functionality is hidden from the user - one of the design goals of both WebForms and JavaServer Faces, as I will demonstrate shortly.
JavaServer Faces:
Java Server Faces (JSF) is a user interface framework for Web applications using Java technology. JSF has been designed to ease the burden of developing and maintaining applications that run on Java application servers and render their user interfaces back to a target client. JSF leverages existing, standard UI and web-tier concepts without limiting developers to a particular markup language, protocol, or client device. JSF is designed to output markup text to any client device, as well as use existing web protocols such as HTTP. JSF includes two main components:
? Java APIs for user interface component management, state management, event handling, input validation, page navigation, internationalization, and accessibility.
? A JSP custom tag library for expressing the JSF user interface within a JSP page.

As will be illustrated, JSF is based on the Servlet and JSP technologies, making the development process more rapid by providing the features mentioned above.
A JSF application looks like any other Java Servlet/JSP application. It contains a deployment descriptor, JSP pages, custom tag libraries, and static resources. JSF applications contain JSP pages that host JSF UI components, such as command buttons, tables, input boxes, and most other popular UI widgets. Binding events to these controls is a simple process, much like ASP.NET.
What does JSF hope to solve?
In introducing what JSF hopes to solve, an analogy might be helpful. In a Java GUI application, such as Swing, there are a set of standard GUI components and an entire infrastructure for tweaking and extending the functionality. A Swing developer works with the concept of a Component - an element of the user interface, such as a panel, button, list, or table. Perhaps the most important Swing concept is that of events. For example, when a button is clicked, an event is generated. In Swing, it is easy to respond to these user interface events and write the corresponding response code. Similar to ASP.NET, JSF introduces a much different problem. In order to figure out, for example, that a button was clicked, the programmer or tool must analyze the HTTP request and try to determine what has happened. This is not an ideal situation and many frameworks have been created to encapsulate this functionality. The developer shouldn’t have to work with HTTP directly to figure out whether a button, image, or hyperlink has been clicked, or how these pieces fit together to create a response stream. What if Java programmers had a user interface development environment similar to Swing or even Visual Basic 6.0? This is the problem JSF aims to solve.
Event Binding in JSF:
In terms of development, JSF is remarkably similar to ASP.NET WebForms. Although the specification defines two different roles for developing JSF apps, I will not focus on them here. Suffice it to say that a developer can simply create UI widgets on a JSP form. In addition, it is easy to bind events to these components and react to them in the business layer. The server will take care of which component fired which event and leave it up to the developer to create the business logic, thereby increasing development time.
As I described earlier, a JSF application looks like any other Servlet/JSP application. The user interface of a JSF application consists of JSP pages which host JSP components, such as forms, input boxes, and buttons. These components are represented by JSF custom tags and can hold data. In fact, on the back end, a JSF page is actually represented by a component tree that will be rendered on the client in a top-down fashion, with each component writing out its own HTML to the output stream. Just as in most other web and Java frameworks, JavaBeans are used to store the data a user entered on the form.
A JSF application works by processing events triggered by JSF UI components on the page. These events are caused by user interactions. For example, when a user clicks on a UICommand button, a particular event is fired. The JSF programmer will decide what to do when an event is fired. This is done by writing “event listeners”. In other words, like ASP.NET, a JSF application is fully event-driven. By implementing two types of listeners (ActionListeners and ValueChangedListeners), developers can bind events to controls in the same way as ASP.NET. The following code illustrates an example of binding an event to a JSF command button component.
<%@ taglib uri=”http://java.sun.com/jsf/html ” prefix=”h” %>
<%@ taglib uri=”http://java.sun.com/jsf/core ” prefix=”f” %>
<h:command_button id=”submitButton” label=”test” commandName=”submit”>
<f:action_listener type=”com.alterthought.MyActionListener”/>
</h:command_button>
By writing a class which implements the javax.faces.event.ActionListener interface, events can be bound to controls over the HTTP protocol. Similar to ASP.NET, the id of the control and all the event information is packed into the HTTP payload. The processing cycle on the server, although beyond the scope of this article, is responsible for calling the appropriate developer created method. In essence, this creates the notion of even-driven, rich user interface programming.
As I have illustrated, Microsoft and Sun have lead us into a new era in Internet application development. The time is now gone when HTML pages were produced manually using scripting code intermixed with HTML. We will now be creating applications based on a real event-driven programming model and component paradigm.
Now that I have explained the basics of each technology, the rest of this article will focus on the differences between ASP.NET WebForms and JavaServer Faces. JSF is still in draft and many changes are being made, so this may make some of my assertions a little out of date.
Architectural differences:
As I have illustrated, there are many similarities between WebForms and JSF. Each technology hides the intricacies of the HTTP protocol and event binding. However, there are some key differences between the two technologies:
? ASP.NET and Web Forms support a single templating mechanism; JSF defaults to JSP, but can support alternative template mechanisms as well, such as Velocity.
? Web Forms components support encoding and decoding directly; JSF components can encode and decode themselves directly, but the framework also has extensive support for separate RendererKits and Renderers, which means that the look and feel for an application is pluggable, as long as the layout of the components is constant. RenderKits also allow the same components to support multiple devices easily -- just develop a new RenderKit -- the component itself doesn't have to worry about it.
? Web Forms applications use the Page Controller  pattern -- each page is a class that posts back to itself to handle events. This is much like the VB-style form-based approach (also made popular by Borland's Delphi). JSF, by contrast, uses a more strict MVC approach to event handling -- application events are handled by Action classes (much like Struts), and component events are handled by listeners (traditional JavaBeans event handling). JSF can, however, support the WebForms-style of development as well (the technical details, however, are quite different). For example, a JSF application can bind a control to an event in a backing bean simply by declaring it in the component’s action attribute. The details are hidden by the FacesServlet and FacesContext, but the results are the same as if an ActionListener were implemented.

? Web Forms has extensive support for integration with client-side scripting languages (such as JavaScript); JSF currently has no such explicit support. This means that it's currently easier to write components that use a lot of client-side scripting in Web Forms (and the existing components make heavier use of scripts as well, such as the calendar control).
? WebForms come with a host of controls such as DataGrids and DataLists that are used to bind directly to data sources. No such controls exist in JSF.

Summary:
As I have illustrated, Microsoft ASP.NET and JavaServer Faces bring event-driven programming to the web. They both are responsible for abstracting the necessary intricacies involved in web programming and its stateless nature. Although most web applications still rely on JavaScript, these two technologies make creating rich user interfaces a much easier, and more importantly, faster task.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ASP.NETJava Web的异同点如下: 异同点: 1. 两者都是用于开发Web应用程序的技术。 2. ASP.NET是由微软公司开发的,而Java Web是由Sun Microsystems(现在是Oracle)开发的。 3. ASP.NET使用C#或VB.NET等语言进行编程,而Java Web使用Java语言进行编程。 4. ASP.NET使用IIS(Internet Information Services)作为Web服务器,而Java Web使用Tomcat或其他Java Web服务器。 5. ASP.NET使用ASP.NET MVC框架进行开发,而Java Web使用Spring MVC框架进行开发。 6. ASP.NET使用ASP.NET Web Forms进行开发,而Java Web使用JavaServer Faces(JSF)进行开发。 7. ASP.NET的开发工具是Visual Studio,而Java Web的开发工具是Eclipse或NetBeans等。 8. ASP.NET的性能比Java Web更好,但Java Web的跨平台性更好。 9. ASP.NET的学习曲线较陡峭,而Java Web的学习曲线相对较平缓。 10. ASP.NET的开发成本较高,而Java Web的开发成本相对较低。 总之,ASP.NETJava Web都有各自的优缺点,开发者可以根据项目需求和自身技术水平选择适合自己的技术。 ### 回答2: ASP.NETJava Web都是常见的Web开发技术,它们有很多相似之处,也有很多不同的地方。 首先,相似之处: 1. 都是基于服务器端的Web开发技术; 2. 都可以实现动态网站,支持集成数据库; 3. 都有丰富的第三方工具和库,可以帮助快速开发,提高开发效率; 4. 都支持多种模式的Web应用程序,包括MVC、Web Forms等。 接下来,我们来看看它们的不同之处: 1. 语言不同:ASP.NET主要使用C#或VB.NET开发,而Java Web则使用Java语言开发; 2. 平台不同:ASP.NET是微软的技术,只能运行在Windows平台,而Java Web则是跨平台的技术,可以运行在各种操作系统上; 3. 框架不同:ASP.NET有自己的框架,如.NET Framework、ASP.NET Core等,而Java Web有Tomcat、Spring、Struts等开发框架; 4. 性能不同:由于ASP.NET使用的是微软的技术,所以在运行速度和性能上可能会比Java Web慢一些,但这也有很多因素影响,如服务器的配置等; 5. 学习成本不同:由于ASP.NET是微软的技术,比较单一,所以对于已经掌握C#或VB.NET的开发者来说,学习成本比Java Web要低一些;而Java Web学习成本较高,因为需要掌握Java语言本身和一些常用的框架。 总的来说,ASP.NETJava Web各有优缺点。选择哪种技术要根据具体的开发需求和开发人员的技能水平来进行考虑。 ### 回答3: ASP.NETJava Web是两种常见的Web应用程序开发框架。虽然它们目的都是为了创建动态Web应用程序,但是它们具有一些显著的异同。 1. 语言选择:ASP.NET特别为C#和VB.NET编写,Java Web则是为Java语言设计的。 2. 平台:ASP.NET只能在Windows操作系统中使用;Java Web不仅局限于Windows操作系统,而且可以在Linux等其他操作系统上使用。 3. 应用程序类型:ASP.NET适用于创建较小的Web应用程序,而Java Web适用于大型企业应用系统。 4. 应用程序的安全性:Java Web的安全性较高,它具有强大的安全性功能,如框架的安全API和强大的用户身份验证机制,ASP.NET也提供安全功能,但Java相对而言更加强大。 5. 性能:Java Web通常具有更好的性能,因为它可以利用Java虚拟机和垃圾回收机制的优势提高应用程序性能。 6. 对象模型:Java使用标准对象模型,ASP.NET则使用服务器控件模型。 7. 数据库的访问:Java Web使用Java Persistence API中的Hibernate进行对象和关系映射,而ASP.NET使用自己的提供程序和实体框架。 总的来说,ASP.NETJava Web根据应用程序的需要,选择一个开发框架更加合适,因此,需要开发人员仔细考虑选项,以确保选择最适合其需要的框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值