Some times back I was discussing MVC with one of my Java friends. The talk ended up with a fight trying to prove how one technology is better than other in implementing MVC. For whatever reasons good or bad I was trying to prove that Microsoft technology is the best but…hmm,aaahh and ooohh.

The fact is both the technologies have their own way of doing things. So I ended up writing this comparison article which talks about the differences between how MVC is implemented in J2EE as compared to ASP.NET.

To do full justice in terms of comparison I have divided the article in two parts. In the first part we will compare MVC implementation without using framework and tools. In the second part I will bring up the comparison of MVC using j2EE struts as compared to ASP.NET MVC visual studio template.

So as we all know in MVC the first hit comes to the controller and the controller binds the view with the model and sends it to the end user. Model, view and controller form the three core pillars for MVC. From 30,000 feet level (That number makes every architect feel good…) below is how these three modules are implemented in each of these technologies:-

For the above example we will create a simple class called as “User”, this class will have two properties “Username” and “Password”. The client, which for now the controller will set these two properties can call the “IValid” function to check if the user is valid or not.

In J2EE the model is nothing but the Java bean class , in .NET its a simple class with setter and getters. Below is the sample code for the same.

The next important part is the controller. The controller forms the heart of MVC.

To create a controller in J2EE we create a class which inherits from ‘HttpServlet’ class. The logic of the controller is written in the “processrequest” method. You can access the request and response object using the ‘HttpServletRequest’ class and ‘HttpServletResponse’ class.

To create a controller in ASP.NET we implement the “IHttpHandler” class and override the “processrequest” with the controller logic. Below is the simple code of how the controllers are implemented in both the technologies. To Access the request and response object we need to use the context class in ASP.NET while in J2EE its available as the part of function with different objects as shown in the below code snippet.

We then call the “isValid” function of the model and redirect to welcome page or to the home page depending on if he is a valid user or not. To redirect in J2EE we use the “RequestDispatcher” class and to redirect in ASP.Net we use the “response.redirect” function.

Now that we have created the controller, we need to map the actions or the URL pattern with the controller. In other words when someone sends an action or URL pattern as “Login” we need to ensure that it invokes the appropriate controller.

Mapping of pattern / action to the controller in both technologies is done by using a configuration XML file. In J2EE this configuration file is called as the “Web.xml” file and in ASP.NET it’s called as “Web.config”.

In J2EE in web.xml we first need to map which URL pattern maps with which servlet. For instance you can see in the below web.xml code snippet we have mapped the Login pattern with LoginServlet servlet name.

In ASP.NET the controller or the handler is mapped with the URL pattern using the web.config file. Below is a web.config file simple XML file code snippet which shows how the Login URL pattern is mapped with the httphandler ‘Loginhandler’.

The next important part is the view. The view is nothing but a simple page with the form tag and action having the URL pattern. You can see how the index.jsp and the index.aspx have the action to Login URL pattern. This URL pattern is then mapped in the web.xml and web.config file to the appropriate controller.

This article compared MVC implementation in both technologies with out framework, in the next article we will see how MVC differs when frameworks are used. Struts is the most popular framework in J2EE for MVC and the Microsoft VS MVC template is the most used way if implementing MVC in Microsoft.

Comments and Discussions

Where is the comparision? conclusion? Maybe you should title it "How to implement a naive MVC patten in both Java and ASP.NET"

You have indicated that it is not for beginners yet the code is extremly simple.

1) Why are not use auto get set properties, as .net supports them and it would make the code easier to read and shorter.
2) You are implementing your controller as a http handler. Do you plan to have one of these for each controller, that is really bad. Think about the load you would put on the server. A http handler is touched on every request weather it is used or not.
3) Why are you using sessions? You seem to set a variable but never referance it. Also are you really going to store a piece of text like that in a session?
4) Why use ASP.Net Server controllers and yet not using something similar for the J2EE. If you are attempting to show that ASP.NET has server controls that make it easier to create form then you should say that. If anything using the server controls adds complexity to ASP.NET view for no good reason.
5) You need to at least indcate that you are escaping the values submitted from the form.

I would like to see the second article in this series as I am c# dev not a java dev and I keen to see the difference.

Where is the comparision? conclusion? Maybe you should title it "How to
implement a naive MVC patten in both Java and ASP.NET"

There is a conclusion table at the end

busbyam wrote:

You have indicated that it is not for beginners yet the code is extremly simple.

I am not explaining MVC basics , I am straight on the code. A java guys would not understand .NET code and vice versa...So i said not really really for guys who just started patterns.

busbyam wrote:

) Why are not use auto get set properties, as .net supports them and it would
make the code easier to read and shorter.

I agree but the main concentration of article is not on the best way to write set and get properties.

busbyam wrote:

You are implementing your controller as a http handler. Do you plan to have one
of these for each controller, that is really bad. Think about the load you would
put on the server. A http handler is touched on every request weather it is used
or not.

This comparison is with out framrwork. Second how if you are using MVC you can to go via a controller. The same code which will be excuted in the UI get executed on the handler. So its just passing the concerns from the UI to the controller. I do not think load is factor here.

busbyam wrote:

Why are you using sessions? You seem to set a variable but never referance it.
Also are you really going to store a piece of text like that in a session?

This tutorial is not for best practices. The point of this tutorial is when any developers looks at patterns he should get a clear idea how each technology will approach them

busbyam wrote:

Why use ASP.Net Server controllers and yet not using something similar for the
J2EE. If you are attempting to show that ASP.NET has server controls that make
it easier to create form then you should say that. If anything using the server
controls adds complexity to ASP.NET view for no good reason.

Did not get isn't the servlet the controller.....Server conttrols are difficult in MVC until you use the MVC template framework.

busbyam wrote:

You need to at least indcate that you are escaping the values submitted from the
form.

????? Escaping values..

busbyam wrote:

would like to see the second article in this series as I am c# dev not a java
dev and I keen to see the difference.

Yes i will.....But after writing so much and you expecting me to continue second article , do i deserve a 1 Will be great ful if you rethink on the vote...Encourages authors like me to write more...