Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

Comparison of MVC implementation between J2EE and ASP.NET, Who is the best? Part 1

Rate me:
Please Sign up or sign in to vote.
4.27/5 (21 votes)
17 Mar 2011CPOL7 min read 145.3K   242   50   30
This article is a comparison of MVC implementation between J2EE and ASP.NET.

Comparison of MVC implementation between J2EE and ASP.NET, Who is the best? Part 1
 

Contents

So, what’s the agenda?
This is not for beginners
Overall Comparison without framework
The Sample for comparison
The model – Javabean in J2EE and .NET class in ASP.NET
The Controller – Servlet in J2ee and HttpHandler in ASP.NET
The mapping XML files – Web.xml in J2ee and Web.config in ASP.NET
The View – Servlet in J2ee and HttpHandler in ASP.NET
Summarizing the Final Comparison table
Source code
Next steps comparing using frameworks
ASP.NET MVC Interview Questions and Answers



So, what’s the agenda?

Some times back I was discussing about asp.net MVC tutorials 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.
 

images 1

 

This is not for beginners

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.


image 2
 

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.
 

image 3


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 void ProcessRequest(HttpContext
 context)
{
// In this will go the code for 
// connecting the model to the view.

}
}


In the controller we can get the data from request and response using in both technologies using the below code.
 

J2EE Taking data from the JSP form Taking data from the .aspx page
String username = 
(String)request.getParameter("username")
;
String password = 
(String)request.getParameter("password")
;
UserBean model = new UserBean();
User obj = new User();
obj.UserName = 
context.Request.Form["txtUser"].ToString(
);
obj.Password = 
context.Request.Form["txtPassword"].ToString( );


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.

J2EE checking if user is valid and redirecting ASP.NET checking if the user id valid and redirecting.
boolean isValidUser = model.isValid();
if(isValidUser)
{
request.setAttribute("welcome","Welcom
e"+username);
}
else 
nextJsp ="Error.jsp";
RequestDispatcher dispatch = 
request.getRequestDispatcher(nextJsp);
dispatch.forward(request,response);
if (obj.IsValid())
{

context.Session["Welcome"] = "welcome " + 
obj.UserName;


 

context.Response.Redirect("Welcome.aspx");
}
else
{

context.Response.Redirect("Error.aspx");

}


The mapping XML files – Web.xml in J2ee and Web.config in ASP.NET

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.

<servlet-mapping>

<servlet-name>
LoginServlet
</servlet-name>

<url-pattern>
/Login
</url-pattern>

</servlet-mapping>


Once the pattern is matched with the servlet name, we then need to map the servlet name with the servlet / controller class as shown in the below code snippet.

<servlet-name>
LoginServlet
</servlet-name>
<servlet-class>
com.questpond.controller.LoginServlet
</servlet-class>


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’.

<httpHandlers>
<add verb="POST" path="Login" type="MVCAspWithoutFramework.LoginHandler"/>
</httpHandlers>

Below is how the overall config file looks in both technologies.
 

J2EE Web.XML ASP.NET Web.config
    <servlet-name>LoginServlet</servlet-
name>
       <servlet-
class>com.questpond.controller.LoginServlet    
</servlet-class>
      </servlet>
      <servlet-mapping>
            <servlet-
name>LoginServlet</servlet-name>
           <url-pattern>/Login</url-pattern>
      </servlet-mapping>
<httpHandlers>
         <add verb="POST" path="Login"
type="MVCAspWithoutFramework.LoginHan
dler"/>

        </httpHandlers>


The View – Servlet in J2ee and HttpHandler in ASP.NET

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.
 

J2EE view index.jsp ASP.NET view index.aspx
<form action="Login" method="post">
              Username<input type="text"
name="username" value="" />
             <br/>
             Password<input type="password"
name="password" value="" />
            <br/>
            <input type="submit"
 value="Submit" />

       </form>
<form id="form1" runat="server"
action="Login" method=post>
      <div>

<asp:TextBox ID="txtUser"
runat="server"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server"
Text="Button" />

     </div>
     </form>


Summarizing the Final Comparison table
 

  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.
 


Source code

Get Source code for ASP.NET MVC  & J2EE MVC without framework.


Next steps comparing using frameworks

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.

Let’s see who wins, who is better ….

ASP.NET Interview Questions and Answers

For Further Reading do watch the below Interview Preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://www.questpond.com
India India

Comments and Discussions

 
SuggestionI would like to point out some small corrections in your article Pin
spprem8927-Jan-16 17:41
spprem8927-Jan-16 17:41 
GeneralMy vote of 1 Pin
robocodeboy4-Jul-14 22:59
robocodeboy4-Jul-14 22:59 
Question[My vote of 2] First of all... Who Cares? Pin
Dewey1-Nov-13 23:46
Dewey1-Nov-13 23:46 
QuestionMy Vote of 4 Pin
shielo6-Jan-13 17:24
shielo6-Jan-13 17:24 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey6-Jul-12 23:38
professionalManoj Kumar Choubey6-Jul-12 23:38 
GeneralMy vote of 5 Pin
Robert_Dyball27-Apr-11 19:08
professionalRobert_Dyball27-Apr-11 19:08 
GeneralMy vote of 5 Pin
Abhinav S9-Apr-11 3:58
Abhinav S9-Apr-11 3:58 
GeneralInteresting to read... Pin
SlimFast20001-Apr-11 23:01
SlimFast20001-Apr-11 23:01 
GeneralNice article, but ... Pin
Adrabi Abderrahim18-Mar-11 10:06
Adrabi Abderrahim18-Mar-11 10:06 
GeneralRe: Nice article, but ... Pin
Shivprasad koirala18-Mar-11 10:28
Shivprasad koirala18-Mar-11 10:28 
GeneralRe: Nice article, but ... Pin
Shivprasad koirala18-Mar-11 10:40
Shivprasad koirala18-Mar-11 10:40 
GeneralGood idea, we are waiting for more ... Pin
janpub18-Mar-11 9:53
janpub18-Mar-11 9:53 
GeneralMy vote of 1 Pin
mintxelas18-Mar-11 9:32
mintxelas18-Mar-11 9:32 
GeneralRe: My vote of 1 Pin
Shivprasad koirala18-Mar-11 10:21
Shivprasad koirala18-Mar-11 10:21 
GeneralRe: My vote of 1 Pin
mintxelas18-Mar-11 10:54
mintxelas18-Mar-11 10:54 
GeneralRe: My vote of 1 Pin
Shivprasad koirala19-Mar-11 20:53
Shivprasad koirala19-Mar-11 20:53 
GeneralGood article and the below comments are discouraging.... Pin
rajeshrisharma18-Mar-11 5:44
rajeshrisharma18-Mar-11 5:44 
GeneralRe: Good article and the below comments are discouraging.... Pin
Shivprasad koirala18-Mar-11 9:52
Shivprasad koirala18-Mar-11 9:52 
GeneralMy vote of 4 Pin
A Praveen Kumar18-Mar-11 0:29
A Praveen Kumar18-Mar-11 0:29 
GeneralRe: My vote of 4 Pin
Shivprasad koirala18-Mar-11 0:38
Shivprasad koirala18-Mar-11 0:38 
GeneralLooks Same Pin
A Praveen Kumar18-Mar-11 0:28
A Praveen Kumar18-Mar-11 0:28 
GeneralRe: Looks Same Pin
Shivprasad koirala18-Mar-11 0:37
Shivprasad koirala18-Mar-11 0:37 
GeneralMy vote of 4 Pin
RogerDW18-Mar-11 0:24
RogerDW18-Mar-11 0:24 
GeneralRe: My vote of 4 Pin
Shivprasad koirala18-Mar-11 0:39
Shivprasad koirala18-Mar-11 0:39 
General[My vote of 1] I think that you need to do more work on this article Pin
busbyam17-Mar-11 21:29
busbyam17-Mar-11 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.