So, what's the agenda?
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.
You can watch our Java and J2EE design pattern videos on various topics like Model view controller, front controller, intercepting filter, factory patterns and lot more. Do not miss my .NET design pattern videos which covers 24 patterns including MVC, MVP and MVVM.

Well this article is definitely not for beginners, in case you are looking for MVC fundamental articles you can see my MVC implementation using core Http Handler @MVC using HttpHandler ASP.NET
Overall Comparison without framework
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:-
J2ee ASP.NET Model Simple Java bean classes with setters and getters. Simple .NET class with business logic with setter and getters. Controller Controller is implemented using HttpServlet class. Controller is implemented using the HttpHandler class. View Simple JSP pages. Simple ASPX page.
Below is simple pictorial representation of how things actually look like.

The Sample for comparison
In order to do a proper comparison we have taken a common example. In this example we will do the following:
- When the user comes to the web site, the first page he will see is the Index page. So if it's ASP.NET he will see index.aspx page, if its j2EE he will see index.jsp page.
- Index page is nothing but a simple page which takes username and password and sends a login command to the controller.
- Once the controller gets the login command, he creates the object of the model and checks if the user is proper or not.
- If the user is proper he sends them to welcome view page or else he redirects them to the error view page.

The model ...Javabean in J2EE and .NET class in ASP.NET
Let's start with the simplest part of MVC i.e. model.
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.
J2EE Model using Javabean ASP.NET Model using .NET class public class UserBean
{
public UserBean()
{
this.username = "user";
this.password = "pass";
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String
username)
{
this.username = username;
}
public boolean IsValid
(String username, String password)
{
return this.username.equals(username)
&& this.password.equals(password);
}
}
public class User
{
public string UserName
{
set
{
_strUserName = value;
}
get
{
return _strUserName;
}
}
public string Password
{
set
{
_strPassword = value;
}
get
{
return _strPassword;
}
}
public bool IsValid()
{
if (_strUserName == "user"
&& _strPassword == "pass")
{
return true;
}
else
{
return false;
}
}
}
The Controller .... Servlet in J2ee and HttpHandler in ASP.NET
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.
J2EE Controller using Servlet ASP.NET controller using HttpHandler public class LoginServlet extends
HttpServlet
{
protected void
processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException
{
// In this will go the code for
// connecting the model to the view.
}
}
public class LoginHandler :
IHttpHandler, IRequiresSessionState
{
public voidProcessRequest(HttpContext
context)
{
// In this will go the code for
// connecting the model to the view.
}
}
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.
You can watch our Java and J2EE design pattern videos on various topics like Model view controller, front controller, intercepting filter, factory patterns and lot more. Do not miss my .NET design pattern videos which covers 24 patterns including MVC, MVP and MVVM.
Overall Comparison without framework
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:-
The model ...Javabean in J2EE and .NET class in ASP.NET
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.
public class UserBean
{
public UserBean()
{
this.username = "user";
this.password = "pass";
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String
username)
{
this.username = username;
}
public boolean IsValid
(String username, String password)
{
return this.username.equals(username)
&& this.password.equals(password);
}
}
public class User
{
public string UserName
{
set
{
_strUserName = value;
}
get
{
return _strUserName;
}
}
public string Password
{
set
{
_strPassword = value;
}
get
{
return _strPassword;
}
}
public bool IsValid()
{
if (_strUserName == "user"
&& _strPassword == "pass")
{
return true;
}
else
{
return false;
}
}
}
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.
public class LoginServlet extends
HttpServlet
{
protected void
processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException
{
// In this will go the code for
// connecting the model to the view.
}
}
public class LoginHandler :
IHttpHandler, IRequiresSessionState
{
public voidProcessRequest(HttpContext
context)
{
// In this will go the code for
// connecting the model to the view.
}
}

Former memberPosted Mar 21, 2011, 2:52 AM
Shiv Sir ; no one can explain in better way than you. Thank you for this article