[Chinese Version] Explain the architecture of JSP Model 2 (explore the MVC design pattern)-1

Source: Internet
Author: User
JSP is becoming an excellent Java technology that can be used to create dynamic web applications. Program . There are countless reasons why Java programmers love JSP. Some people like the "one-time writing, everywhere" method it brings to interactive pages, while others appreciate it because it is easy to learn, in addition, they can use Java as a server-side scripting language. But most of them are for the same reason-the biggest advantage of using JSP is that it can help you effectively separate expressions and content. In this article, I have made an in-depth discussion of the following issues, that is, how to use the JSP Model 2 architecture to obtain the best expression-content separation effect. This model can also be seen as an implementation of the general MVC design pattern on the server side. Note that before reading this article, you need to be familiar with basic JSP and Servlet programming, because this article will not involve any syntax rules.

What is the problem with servlets?

Although jsp can do a good job in dynamic web content service and separation of content and expression, some people may doubt why should we leave servlets aside? In fact, the role of servlets is unquestionable. They are excellent in server-side processing and will be retained because they have a solid foundation. In fact, in terms of structure, you can regard JSP as a kind of advanced abstraction of servlets, just like an extension of servlet 2.1 API. However, you should not abuse servlets. They are not applicable to everyone. For example, if web designers can easily write JSP pages using traditional HTML or XML tools, servlets is more suitable for developers of background programs, because servlets is usually written in an integrated development environment (IDE)-in general, This method requires more advanced programming skills. When using Servlets, developers must exercise caution to ensure that the expressions and content are not closely related. To achieve this, you can use a third-party HTML package (such as htmlkona) to mixCode. However, even though this method makes some flexibility through simple screen transformations, it cannot help you avoid the changes to the format itself. For example, if your expression format changes from HTML to DHTML, you must make sure that the language packs are adapted to the new format. Suppose the worst case, if a package is unavailable, you may have to be busy writing expressions in dynamic content, which will put you exhausted. So how can we solve this problem? Next, you will see that the combination of JSP and servlets technology is a good way to build an application system.

Different systems

Early JSP specifications proposed two methods to build applications using JSP technology. The two methods are called JSP Model 1 and JSP Model 2 respectively. The essential difference between them is that the location of batch requests is different. In the model 1 system, as shown in figure 1, the JSP page responds to the request and returns the result to the customer. There is still a separation between expression and content. For this reason, data access is completed by bean. Although the Model 1 system is very suitable for the needs of simple applications, it cannot meet the implementation of complex large applications. When Model 1 is randomly used, a large number of script fragments or Java code are embedded in the JSP page, especially when the amount of requests to be processed is large, the situation is more serious. Although this may not be a big problem for Java programmers, however, If JSP pages are developed and maintained by web designers, which is usually the specification for developing large projects, this is indeed a problem. Fundamentally, the definition of roles and unclear assignment of responsibilities will lead to unnecessary troubles in project management.

Figure 1: JSP Model 1 Architecture

Text in the figure: browser; Request: request; Response: Response; Application Server: Application Server; enterprise servers/data sources: Enterprise Server/data source.

The architecture of model 2, as shown in figure 2, is a method that combines JSP and Servlets to implement dynamic content services. It draws on the outstanding advantages of the two technologies, uses JSP to generate the content of the expression layer, and enables Servlets to complete in-depth processing tasks. Here, servlets acts as the controller and is responsible for managing request processing, creating beans and objects required for JSP pages, and passing the JSP page to the requester according to the user's action. Note that there is no processing logic in the JSP page; it is only responsible for retrieving objects or beans originally created by Servlets, and extracting dynamic content from Servlet and inserting static templates. In my opinion, this is a representative method. It clearly separates expressions and content, clarifies the definition of roles, and the division of labor between developers and web designers. In fact, the more complex the project is, the greater the benefit of using the Model 2 architecture.

Figure 2: JSP Model 2 Architecture

Text in the figure: instantlate: instant latency; controller, view, and model are controllers, attempts, and models in the MVC design mode; others are the same as Figure 1.

To further clarify the concept of model 2 architecture, let's look at an example of using it: an online music store called "music without borders.

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.