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
-
- Business and data layers are not separated.
- Exposing the Database details like Db Name , userId , Password are embedded in the code.
- Expose the structure of Tables.
- 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.
Leave a Reply