Monday, February 25, 2008

ASP.Net and NetBeans JavaServer Faces (JSF) comparison from a .Net Developers perspective

Both have server controls (tags in JSF) that encapsulate and enhance standard html elements and form elements.
Code-behind == Backing bean (aka Managed Bean)
They both have code-behind. JSF calls it backing beans. The difference is that backing beans are just plain old java objects (POJO). This means that they are just regular classes. They don't have to inherit from any class or interface.
Another difference is that Code-behind and the .aspx files are linked together by a page directive in the .aspx. Visual Studio does this automatically for you unless you change it manually or rename a file. In JSF, this linking is done in the faces-config.xml.
Event Handler (in Code-behind) == Event Handler (in Backing bean)
Both provide event handler for user actions such as button clicks, selections, etc. Event handlers in ASP.Net always return void, but in JSF they return a string or null. Null means just navigate to the current page (a postback essentially). Any other value is is basically the name of an action that determines what navigation is taken. This is the same model as the new MVC that is being worked on in .Net currently. With JSF the class that handles the event needs to implement the ActionListener, ValueChangedListener, etc.
Postback vs. MVC
While Postback is a special case of MVC navigation as described above, JSF also allows for true MVC processing. This means that all page navigation is also done in one place. This is nice when you need to make navigation type changes. This means all requests come through a controller, and then the Model (data store) pulls the data, and the View (user interface) is rendered for that data. JSF has a powerful feature called RenderKit that allows the rendering to differ based on what medium is accessing the app. For example, if I access a page with my cell phone or computer I can get a different interface. The only change is the View, the Model and Controller stay the same. How cool is that!
ASP.Net Life Cycle vs. JSF Life Cycle
JSF has 6 lifecycle phases for a request. Restore View, Apply request values, Process validations, Update model values, Invoke the application, Render the response.
.ASPX vs. .JSF
In ASP.Net pages have the file extension and the url is also .aspx. In JSF the url is different from the file name. The filename ends in .jsp and the url has .jsf instead. By default, a knowledgeable user could access your pages with .jsp extension, but we really want all requests to go through the .jsf extension so we configure our application this way typically. However, there are exceptions to this. You can also configure your application to process .jsp pages that have a /faces/ before them in the url. The key here is that JSF allow you to define the convention for specifying which JSP pages are handled by the JSF servlet and which are not. BTW, not all .jsp pages are JSF pages, that is the reason for needing the difference.
Validation Summary == Message Group
In ASP.Net in general validation can handled by validators, and errors reported back to the user using Validation Summary. JSF, has similar items. However, in JSF, the messages are added to the FacesContext. This makes it easy to add custom validation logic back to the user also. The FacesContext shows up in the Message Group. This allows for validation to occur at the Business Logic Layer more easily.
ASP.Net Validator controls vs. JSF Standard Validators
JSF has similar validators like ASP.Net, but not as many, though you can create custom ones or just add validation code to backing bean. JSF includes DoubleRangeValidator, LengthValidator, and LongRangeValidator. ASP.Net in contrast has RequiredFieldValidator, RangeValidator, RegularExpressionValidator, CompareValidator, CutomValidator. The number of validators don't match, but much of the same validation checks can be performed on both platforms.
ASP.Net Form Data Conversion vs. JSF Converters
ASP.Net requires that you convert string form values to data types that the database (model) uses. For example, if you have a textfield that only accepts integers on a ASP.Net web form you need to get the text from the control, do data conversion to an appropriate type such as an int and then store it to the database, etc. In JSF, the same thing is needed, but it can be done using the UI using things called Converters. These converters can use number and date formats much like Excel to convert and format data. While, the effect is the same, the way that the conversion is done is different. There are converters for just about every datatype.


Anonymous said...

Good post, thanks

Brent V said...

I am glad you found it help.

Anonymous said...

Nice comparison. I'm using both in my company.
In your opinion, may I ask what do you think is the best in terms of performance & OOP architecture, JSF or ASPX ?? Thanks

Brent V said...

Performance is very subjective as you know. I find that for applications that are not used very often take too long to start up when using Java. I do a lot of departmental solutions and the time for JSF, Tomcat, etc to start up can take a while in comparison to a ASP.NET application starting up. Once they are actually started, I don't see a big difference in performance.

As far as OOP architecture, I have not analyzed the two of them, but my general feel is that Java applications tend to be more Object Oriented versus ASP.NET tends to be more event driven, and fewer objects (that I write).

They both have their stengths and weaknesses. I like them both.

Anonymous said...

Hi Brent,

Thank you. I've save your website in my bookmark. I like your blog.

Brent V said...

I love hearing that others enjoy my blog. Thank you so much for your feedback. It give me incentive to keep sharing.

Francis said...

Thanks a lot for this! I just started looking into the Java world recently and been looking for that kind of comparison between the two worlds. Very helpful!

Brent V said...


Thank you for the feedback. I'm so glad you found this post useful.

FYI, the transition is pretty easy if you use NetBeans. If you use Eclipse, it is not difficult, but the IDE does not impose .NET like standards and instead allows you to decide what you want to do.