Some reasons for developement of MVC Framework
Introduction to Servlet
Servlet technology and JavaServer Pages (JSP) are the main technologies for developing Java web applications.
In 1996 , When Servlet was introduced by Sun Microsystems , Servlet technology was considered superior to the reigning Common Gateway Interface (CGI) because servlets stay in memory after responding to the first requests so no re-instantiation is required and hence better response time as compared to CGI..
Problem with servlet
The problem with servlets is it is very cumbersome and error-prone to send HTML tags to the browser because HTML output must be enclosed in Strings, like in the following code.
PrintWriter out = response.getWriter();
out.println("<html><head><title>Testing</title></head>");
out.println("<body style=\"background:#ffdddd\">"); ...
This is hard to program. Even small changes in the presentation, such as a change to the background color, will require the servlet to be recompiled.
Introduction of JSP
So sun introduce JSP(Java Server Pages),which allows Java code and HTML tags to intertwine in easy to edit pages.Changes in HTML output require no recompilation.
A Java code fragment in a JSP is called a scriptlet.
Even though mixing scriptlets and HTML seems practical at first thought, it is actually a bad idea for the following reasons:
Problem with JSP
• Interweaving scriptlets and HTML results in hard to read and hard to maintain applications.• Writing code in JSPs diminishes the opportunity to reuse the code.
• Of course, you can put all Java methods in a JSP and include this page from other JSPs that need to use the methods. However, by doing so you're moving away from the object-oriented paradigm. For one thing, you will lose the power of inheritance.
• It is harder to write Java code in a JSP than to do it in a Java class. Let's face it, your IDE is designed to analyze Java code in Java classes, not in JSPs.
• It is easier to debug code if it is in a Java class.
• It is easier to test business logic that is encapsulated in a Java class.
• Java code in Java classes is easier to refactor.
In fact, separation of business logic (Java code) and presentation (HTML tags) is such an important issue.
No comments:
Post a Comment