• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

MVC Architecture

January 4, 2011 By j2eereference Leave a Comment

Lets first find out why we need MVC architecture ?

The following JSP highlights some of the more common mistakes made by develors when writing an application. Here we are fetching employee details from the Database and displaying the result of the query in  the JSP

<@page import=”java.sql.*” %>

<table border=1>

<tr>

<td>EmpId </td>
<td>Emp Name</td>

</tr>

<%

Connection conn= null;
ResultSet rs=null;
Statemetnt stmnt=null;
try
{

Clas.forName(“com.mysql.jdbc.Driver”);

conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306”, “userID”, “passwd”); 

StringBuffer query= new StringBuffer();
query.append(” select emp_id ,emp_name from”);
query.append(” employees”);

stmnt=conn.createStatement();
rs=stmnt.executeQuery(query.toString());
While(rs.next())
{

out.println(“<tr>”);

out.println(“<td>”);
out.println(rs.getString(“emp_name”)
out.println(“</td>”);

out.println(“<td>”);
out.println(rs.getString(“emp_id”));
out.println(“</td>”);

out.println(“</tr>”);

}

}
catch(SQLException sqe)
{
out.println(“Got SQL Exception”+ sqe.getMessage());
}
finally
{
if(stmnt !=null) try{ stmnt.close(); } catch(SQLException sqe){}
if(rs!=null) try{ r.close(); } catch(SQLException sqe){}
if(conn!=null) try{ conn.close(); } catch(SQLException sqe){}

}

%>

</table> 

In the above Example Presentation we can find the bellow issues

  1.  
    1. Business and data layers are not separated.
    2. Exposing the Database details like Db Name , userId , Password are embedded in the code.
    3. Expose the structure of Tables.
    4. In future if you want to change  your DB Name, DB server ,DB from mySQL to Oracle  etc. we need to make sure that all the database touch points were visited and updated.

These issues can be handle if we use MVC Architecture

The MVC pattern has three key components:

1) The Model Component

Responsible for the business domain state knowledge

2) The View Component

Responsible for a presentation view of the business domain

3) The Controller Component

Responsible for controlling flow and state of the user input

The MVC Model 

Depending on the type of architecture of your application, the model portion of the MVC  pattern can take many different forms. In a two-tier application, where the web tier interacts directly with a database, the model classes may be a set of regular Java objects. These objects may be populated manually from a result set returned by a database query or they can even be instantiated and populated automatically by an Object-to-Relational Mapping (ORM) framework like Hibernate 

The MVC View

 The views within the web tier MVC pattern typically consist of HTML and JSP pages.HTML pages are used to serve static content, while JSP pages can be used to serve bot static and dynamic content..  

The MVC Controller 

The controller portion of the web tier MVC design is generally a Java servlet. The Controller

 A web tier application performs the following tasks:

 1. Intercepts HTTP requests from a client.

2. Translates the request into a specific business operation to perform.

3. Either invokes the business operation itself or delegates to a handler.

4. Helps to select the next view to display to the client.

5. Returns the view to the client.

 

Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.