From servlet to JSP, from model1 to model2

Source: Internet
Author: User
Servlet appearance

Servlet technology and JSP technology are two main technologies used to develop Web applications using Java language. In 1996, Sun released Servlet technology for the first time to solve the performance problems in Web applications. When the servlet is first requested by the user, it is loaded into the memory and will remain in the memory. Subsequent requests to the same servlet will no longer need to instantiate the servlet class, this mechanism greatly improves the speed of Web applications.

However, servlet is not so perfect. When people write servlet, they find that all HTML output code is encapsulated in the string object, and then use the print method of the out object to display it to users, this increases the difficulty of coding, and it is very troublesome to maintain it. Even if a slight change is made, you need to re-compile the servlet.

The emergence of JSP and Related Technologies

Sun realized this problem and proposed JSP technology. JSP allows Java code and HTML tags to be mixed together to simplify page development. You do not need to re-compile the page when modifying the page. If the original JSP changes during the first request, the code will be re-compiled automatically, if there is no change, the existing instance is loaded directly.

But the problem comes one after another:

L mixing Java and HTML code (logic and display) makes the program hard to read and maintain.

L it is difficult to reuse code in JSP, which is contrary to the object-oriented idea.

L coding in JSP ide does not support this very well

L Testing becomes difficult

Logic and display are not advocated by people. As early as jsp1.0 norms, programmers are encouraged to Use Javabean to encapsulate the business logic code, thus separating the display and business. But things are not as beautiful as you think. In JSP, the naming conventions must be observed for the methods in the JavaBean, so sometimes the names of a method are very lengthy and difficult to remember. To achieve easier separation between code and HTML, jsp1.1 defines several user-defined tag libraries, which are more flexible and easy to use than JavaBean.

However, the old problem is solved, and the new problem arises: the custom tag library is difficult to write, and the custom tag in jsp1.1 has a very complex lifecycle.

Later, people began to add some specific common functions to related labels. These tag libraries are compiled into several library files, collectively referred to as jstl (assumerver pages standard tag libraries, JSP standard tag library ).

Although the technology such as JavaBean and jstl can separate the business logic from the display code, many developers still mix the logic with the displayed code in a convenient or short-sighted manner.

Model1 Design Model

So far, the first design model for developing Web applications in Java is called model1. In short, it only uses JSP instead of servlet to mix Page code and logic code. From a JSP page to another JSP page is completed through the link in the page.

The problem with this mode is mentioned above. It may be easier for small projects to develop. However, for large projects, development is miserable, because this architecture is not conducive to the division of labor between web designers and web developers: developers need to participate in page development, it also needs to be involved in the encoding of the business logic. It is even more difficult to maintain. The logic and display code are mixed, and the link between pages is through the link (this means that once the Page name changes, any other page that uses this page must be changed, this is a nightmare for developers.

Model2 Design Model

So the second design model emerged, which we call model2. This model is another name of the Design Pattern of MVC (Model-View-controller, Model-View-Controller. An application developed based on the model2 model consists of three main components: model, view, and controller.

In the model2 model, all pages have a common entry point, usually using a servlet or filter (in which struts1 uses a servlet, while struts2 uses a filter) to act as a controller. The Controller is responsible for receiving changes from user input and controlling the model and view. The view is responsible for displaying application information; the model encapsulates the data and business logic of the application. In such an application, every HTTP request must be directed to the Controller, and the information in the URI of each request may tell this control which actions need to be called. The Controller checks Each URI to determine which actions should be called. It also saves the action object in a place that can be accessed from the view, so that the server value can be displayed on the browser. Finally, the controller uses the requestdispatcher object to pass the request to the view (that is, the corresponding JSP page), and then the definition tag in the JSP page displays the content of the action object.

Entire Development Process

Originally:The development of technology is built up by problems.

The following will demonstrate how to use Servlet and filter as controllers to implement the model2 design model. In fact, it can be seen as the demo of struts1 and struts2.

Related Article

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.