Example to explain JSP Model2 architecture (top) _jsp programming

Source: Internet
Author: User
There have been a lot of related introductions lately, and JSP is becoming an excellent Java technology that can be used to create dynamic Web applications. There are countless reasons why Java programmers love JSP. Some people like the way it brings the "write, use everywhere" approach to interactive pages; others appreciate it because it is easy to learn and enables them to use Java as a server-side scripting language. But most of all because of a common cause-the best thing about using JSP is that it helps you effectively separate the expression from the content. In this article, I delve into the following questions: How to use the JSP Model 2 architecture to get the best expression-content separation effect. This model can also be seen as an implementation of the universal MVC design pattern on the server side. Note that before you read this article, you need to be familiar with basic JSP and servlet programming, as there will be no grammatical rules involved in the text.

What's wrong with servlets?
Although JSP can do very well in dynamic Web content services and separation content and expression, there are still people who doubt why the Servlets should be thrown aside. In fact, the role of Servlets is beyond doubt. They do a good job on server-side processing and, because they already have a solid foundation, will remain. In fact, structurally speaking, you can think of JSP as a high-level abstraction of servlets, as an extension of the Servlet2.1 API. However, you should not abuse servlets, they are not applicable to everyone. For example, if Web page designers can easily write JSP pages with traditional HTML or XML tools, Servlets is better suited to developers of background programs because Servlets is usually written in the integrated development environment (IDE) In general this approach requires more advanced programming skills. When using Servlets, even developers must be cautious to ensure that there is no close connection between expression and content. To do this, you can usually use a third-party HTML package (such as Htmlkona) to mix the code. But even this approach, though it offers some flexibility through simple screen transformations, does not help you avoid the change in the expression format itself. For example, if your expression format changes from HTML to DHTML, you must make sure that the language pack adapts to the new format. Assuming a worst-case scenario, if a package is unavailable, you may have to be busy writing expressions in dynamic content, which can be exhausting. So, how to solve this problem? Next you will see that using JSP combined with Servlets technology is a good way to build an application system.

Different systems.
The early JSP specification proposed two ways to build an application using JSP technology. These two methods are called JSP Model 1 and JSP Model 2, respectively, in terms of the difference in the location of the batch request. In the Model 1 system, as shown in Figure 1, the JSP page responds to the request alone and returns the processing results to the customer. There is still a separation of expression and content, because so data access is done by the bean. Although the Model 1 system is ideal for simple applications, it does not meet the needs of complex, large-scale applications. Arbitrarily using model 1 without any choice can result in a large number of script fragments or Java code embedded in the JSP page, especially when the volume of requests to be processed is large. While this may not be a big problem for Java programmers, it's a real problem if JSP pages are developed and maintained by web designers-usually the norm for developing large projects. Fundamentally, it will lead to ambiguous roles and unclear responsibilities, causing unnecessary trouble to project management.


Figure 1:jsp Model 1 architecture

Text: BROWSER: browser; Request: Response: Response; Application server: Application server; Enterprise servers/data Sources: Enterprise Server/Data source.

Model 2 architecture, as shown in Figure 2, is a way to implement dynamic content services by using JSP and Servlets together. It absorbs the outstanding advantages of the two technologies, and uses JSP to generate the content of the expression layer, so that servlets complete the deep processing task. Here, Servlets acts as the controller, manages the processing of requests, creates the beans and objects that JSP pages need to use, and, depending on the actions of the user, passes that JSP page to the requester. Notably, there is no processing logic within the JSP page, it is only responsible for retrieving objects or beans that were originally created by servlets, and extracting dynamic content from the servlet to insert a static template. In my view, this is a representative approach, it clearly separates the expression and content, clear the definition of roles and developers and web Designers division of labor. In fact, the more complex the project, the greater the benefits of using the Model 2 architecture.


Figure 2:jsp Model 2 Architecture

Text: instantlate: instantaneous delay; Controller, View, model are the controllers, attempts, models in the MVC design pattern respectively, others are the same as Figure 1.

To further illustrate the concept of Model 2 architecture, let's look at an example that is implemented 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.