The confusion of the MVC framework

Source: Internet
Author: User

Now many beginners and programmers, are flocking to learn the web development of the Treasure-level framework: Struts2,spring,hibernate. It seems that these frameworks have become a person who is proficient in Java, will write the only factual standard of the program and the necessary basis for job search.

However, if you ask these programmers during the interview, why do you want to learn these frameworks? What exactly is the nature of these frameworks? Few people seem to be able to give me a very satisfying answer. Because they are learning to learn, to work for the study, rather than in the real in-depth understanding of a framework. In fact, all people should think about the question: Why learn the framework? What exactly did the framework bring to me? Next, we use login as the simplest example to look at different times, how we write Web programs.

Many years ago, we wrote this program.

Many years ago, it was a poor age, if we were to use Java to do some dynamic interaction on the Web page. A lot of people will tell you a technique called JSP. When I was still very confused about Java, I was told that JSP is a good thing, it can write Java code inside the HTML code to complete the logic.

HTML code

<%
     String name = Request.getparameter ("name");
     String Password = request.getparameter ("password");

     Userhandler Userhandler = new Userhandler ();
     if (userhandler.authenticate (name, password)) {
%>
<p> Congratulations, login success </p>
<%
      } else {
%>
<p> Sorry, Login failed </p>
<%
      }
%>

As a JSP, it can receive login requests sent from other JSPs and process them. In this way, we do not need any additional configuration files, and do not need any framework to help to complete the logic.

Later, we gave up writing logic on the page.

Later, as the program was written more and more, we found that there were a number of problems with the way in which Java code was written in HTML code to complete the logic:

1.Java code is cluttered and poorly readable because it is mixed in an HTML environment. A JSP file can sometimes become dozens of k, or even hundreds of K. To find a piece of logic, often unable to locate.

2. Write code is very confused, do not know where the code should be written, and do not know if someone has already implemented similar functions, where to reference.

3. Suddenly, a demand has changed. So, everyone began to replace the whole head, but also cautious, for fear of the other people's logic changed.

4. The logic handler needs to maintain the lifecycle itself, which cannot be uniformly supported for many modules such as database transactions, logs, and so on.

At this point, if there is a product, it will be able to extract the Java code on the page, so that the page as little as possible to appear Java code, how good. So many people start using a servlet to handle those business logic.

Java code

public class Loginservlet extends HttpServlet {/

    * (non-javadoc)
     * @see Javax.servlet.http.httpservlet#dopost ( Javax.servlet.http.HttpServletRequest, Javax.servlet.http.HttpServletResponse)
     * * *
    @Override
    protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
        String message = NULL;
        RequestDispatcher dispatcher = Req.getrequestdispatcher ("/result.jsp");
        String name = Req.getparameter ("name");
        String Password = req.getparameter ("password");
        
        Userhandler Userhandler = new Userhandler ();
        if (userhandler.authenticate (name, password)) {Message
            = "Congratulations, login succeeded";
        } else {message
            = "Sorry, Login Failed";
        }
        
        req.setattribute ("message", message);
        Dispatcher.forward (req, resp);
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.