This article introduces the JSP and JavaBean models. Many people may not know about the JSP and JavaBean models. It does not matter. After reading this article, you will certainly have a lot of GAINS. I hope this article will teach you more.
Web dynamic interaction Implemented Using JSP/Servlet mainly uses JSP and JavaBean models as well as JSP and JavaBean + Servlet models. This article describes the two models and compares their advantages and disadvantages.
I. JSP and JavaBean Models
The model structure is shown in. It is called the JSP and JavaBean models. The working principle is: when a browser sends a request, JSP receives the request and accesses JavaBean. To access the database or backend server, Use JavaBean to connect to the database or backend server and perform corresponding processing. JavaBean submits the processed result data to JSP. After JSP extracts the results and reorganizes them, the HTML page is dynamically generated and returned to the browser. The user obtains the interaction result from the page displayed in the browser.
The JSP and JavaBean models take full advantage of JSP technology's ease of developing dynamic web pages. The tasks at the page display layer are undertaken by JSP, but they also contain the content at the transaction logic layer, javaBean is mainly responsible for the transaction logic layer and data layer. JSP and JavaBean models rely on several JavaBean components to implement specific application functions and generate dynamic content. The biggest feature is simplicity.
Ii. JSP and JavaBean + Servlet Models
Shows the architecture of JSP and JavaBean + Servlet models, which are called JSP, JavaBean, and Servlet models. It is a design Model based on the Model View Controller Model and View and Controller), that is, the MVC Model. This Model divides JSP program functions into three layers: Model) layer, View) layer, and Controller layer ). The Model layer implements business logic, including the core functions of Web applications, and is responsible for storing data related to applications. The View layer is used for display of user interfaces. It can access data at the Model layer, however, the data cannot be changed. The Controller layer is mainly responsible for the control relationship between the Model and View layers.
In specific implementation, JavaBean serves as the model layer, Servlet serves as the control layer, and JSP serves as the view layer. Each layer has the following functions:
1. Use JavaBean as the Model layer to implement specific application logic and functions.
2. As the Controller layer, Servlet is responsible for handling HTTP requests, including:
◆ Check and convert the input data.
◆ Access the database through JavaBean.
◆ Initialize the an or object to be used on the JSP page.
◆ Decide to switch to the JSP based on the different branches and results in the processing.
3. JSP is used as the View layer to generate interfaces returned after interaction. It obtains the object or JavaBean generated by the Servlet through information sharing, extracts data from it, and inserts the data into the HTML page.
The working principle of this model is that all requests are sent to the Controller Servlet. The Servlet receives requests and distributes them to the corresponding JSP page for response based on the request information. At the same time, the Servlet also generates the corresponding JavaBean object based on the JSP requirements and transmits it to the JSP. JSP can call a method directly or use the custom tag of UseBean to obtain data in the JavaBean.
This design mode achieves interactive processing through the cooperation of Servlet and JavaBean, and achieves the separation of presentation layer, transaction logic layer and data.
Iii. Comparison of the two models
From the above two models, the overall structure of JSP and JavaBean models and JSP and JavaBean + Servlet models is clear and easy to implement. Their basic idea is to separate the presentation layer, transaction logic layer, and data layer. This hierarchical design facilitates system maintenance and modification. The main differences between the two models are as follows:
1. The main control part of the processing process is different. JSP and JavaBean models use JSP as the master part to effectively link user requests, JavaBean and responses. JSP and JavaBean + Servlet models use Servlet as the master part to effectively link user requests, JavaBean and responses.
2. The degree of separation between the presentation layer, transaction logic layer, and data layer is different. JSP and JavaBean + Servlet models have better separation effects than JSP and JavaBean models. When the transaction logic is complex, with many branches, or involves multiple JavaBean components, JSP and JavaBean models often cause a large number of scripts or Java code to be embedded in JSP files. Especially in the development of large projects, because the page design and logic processing are undertaken by different professionals, if a considerable part of the JSP processing logic and page description are mixed together, this may lead to unclear division of labor, which is not conducive to independent development and maintenance of the two parts, affecting project construction and management. In the JSP and JavaBean + Servlet models, the Servlet processes HTTP requests, and the JavaBean performs transaction logic processing. The JSP is only responsible for generating webpages, so the hybrid problem of the presentation layer is relatively light, it is suitable for professionals of different specialties to independently develop various levels of functions in Web projects.
3. Different requirements for Dynamic Interaction processing. When the transaction logic is more responsible, with many branches, or involves many JavaBean components, since JSP and JavaBean + Servlet models have clearer page performance and clearer development module division than JSP and JavaBean models, JSP and JavaBean + Servlet models are suitable. However, JSP and JavaBean + Servlet models need to write Servlet programs. The tools required by Servlet programs are Java integrated development environments, and the programming workload is large. For simple interaction processing, JSP and JavaBean models are used. JSP is mainly developed using HTML tools, and then a small amount of Java code can be inserted to achieve dynamic interaction. In this case, it is more convenient to use the JSP and JavaBean models.
JSP and JavaBean models and JSP and JavaBean + Servlet models are both useful for developing Web applications. Of course, Web applications that implement dynamic interaction are not limited to these two models. In the actual Web application development process, you need to select an appropriate model based on the system characteristics, customer needs, and processing logic characteristics, and strive to make the system results of the entire application more reasonable, to achieve different interactions.
- Detailed analysis of JSP and BEAN
- Servlet and JSP experience
- Call jsp el expressions with parameter Methods
- Detailed description of JSP development environment Configuration
- Introduction to JSTL