From the MVC mode, Servlet, JSP compilation principle, respectively, take you to analyze which layer of the MVC mode the Servlet component belongs to?


Preface

In today's class, I happened to hear that several students were discussing such a question: Which layer of the MVC pattern does Servlet belong to? When it comes to this issue, it will examine your familiarity with the MVC pattern. In this article, we will discuss the principles of MVC mode, Servlet and JSP respectively. Which layer does Servlet belong to?

Insert picture description here


1. What is the MVC pattern in memory?

First of all, let's recall this classic software design framework-the MVC pattern.

Please pay attention to the difference between software design patterns and software design frameworks here : software design patterns refer to the classic 23 design patterns: creation, behavior, and structure; software design frameworks are MVC, SSH, SSM, etc.

MVC (Model VIew Controller), short for model, view, and controller, is a classic software design framework that also provides support for HTML, CSS, and JavaScript.

1.1 Introduction to Model, View and Controller components

The relationship and functions of the overall component types of the classic MVC pattern are shown in the following figure. Let's introduce the component types and relationships respectively:

Insert picture description here

  1. Model (model) is used to process the data logic part of the application, that is, the business model. Used to represent the core of the application, such as: database record fields, responsible for accessing data in the database.
  2. View (view) is the part of the application that handles data display, that is, the user interface. Usually the view is created by relying on the data of the model, and the view must be updated synchronously when the model changes.
  3. Controller (controller) is the part of the application that handles user interaction. Responsible for reading data from the view, controlling user input, and sending data to the model.

1.2. Clarify the difference between View and Controller components

Now from the introduction of the MVC pattern, we can clarify the difference between View and Controller : View is responsible for data display, and Controller is responsible for data and user interaction.

2. What is Servlet?

After introducing the MVC pattern, what is a servlet? What kind of component is a servlet? What role does it play?

2.1, Servlet component definition

Servlet is a server-side program written in Java language, which is called and executed by the server-side, and is a Java class written in accordance with Servlet's own specifications. The servlet can process the HTTP request from the client and return the response.

2.2. The Servlet component is in the Controller layer?

In layman's terms, Servlet is a Java class that defines methods for receiving user requests, calling business classes, and sending response views . Now, do you think Servlet seems to belong to the Controller layer?

3. The teacher said that the Servlet belongs to the View layer!

Q: At this time, someone might ask the question? Why did our teacher say that Servlet is in the View layer? What you said is wrong!
In other words: Why did our teacher say that JSP is in the Controller layer? What you said is wrong!

A: Don't worry, let's go down and analyze the principle of JSP. We put these two issues at the end as a summary.

4. What is JSP?

To analyze the role of Servlet, we have to talk about JSP, what is JSP? Everyone is familiar with the basic definition of JSP, so I won't repeat it.

4.1, Servlet is a variant of JSP

  1. JSP embeds Java code and specific changed content into static pages, and uses static pages as templates to dynamically generate other parts.
  2. When the JSP file is running, the compiler will compile the embedded Java code into the Servlet code written in Java. The JSP instruction controls how to generate the Servlet through the JSP compiler, and then the Java code is compiled into an intermediate bytecode file ending in .class Finally converted to binary machine code.
  3. We use JSP built-in instructions to replace out.write();the output used in Java (ie Servlet) , saving a lot of code and enabling page interaction.

For more detailed JSP compilation principles, I suggest you read my previous blog post- Use Eclipse to view the process of JSP compilation to generate Servlet and analyze the principles of JSP compilation .

From this we come to the next conclusion: JSP is Servlet, and conversely, Servlet is JSP . Are you having a problem again?

Q: You are confusing me. What role is Servlet?

Five, the role of Servlet is defined according to the application scenario

In our project development, a complete MVC design framework may contain many components or combine other components at the same time. We will introduce them one by one from different demand scenarios.

5.1. MVC design framework under development

The structure and functions of the MVC pattern components frequently used in the project are shown in the following figure: The
Insert picture description here
above figure is further subdivided in our classic MVC structure:

  • The View layer is responsible for the display of data. Including what we can see using JSP and HTML to achieve the interface.
  • The Cotroller layer is responsible for controlling the interaction between data and users. Including the Servlet we mentioned above.
  • The Model layer is responsible for the processing of data logic. Including entity class POJO (encapsulated object data), business layer Service (processing data logic information), persistence layer Dao (data CRUD) and other components.

Note : During development, you must follow the development specifications and framework specifications, realize interaction through Servlet, perform logical processing through business layer Service, and perform data query through persistence layer Dao. Don't confuse the order and function of each component, otherwise there is no point in using a framework.

5.1, the role of different components in the complete MVC design framework

In the complete MVC design framework as shown in the figure above, strictly speaking, Servlet belongs to the Controller layer and is responsible for interaction with users , while our common business layer Service, persistence layer Dao, and entity class JavaBean all belong to the Model layer. Of .

5.2. The teacher said that the Servlet belongs to the View layer! !

A: Okay, let's answer this question.
Now you need to go back and look at the content of 4.1 above-the essence of JSP is Servlet, which is Java class, and Servlet is also JSP. It is not a big mistake to say that Servlet is in the View layer. But strictly speaking, the main function of JSP is to realize the display of data information, and a small part of the function is used to realize the interactive processing of data information, so we usually classify Serlet as Java code as the Controller layer according to the code specification. understand?

5.3. The role of Servlet in a pure JSP project

If you are looking at the pure "JSP Tutorial", you may directly use JSP to hard-connect the database without adding other components, then you can say at this time: Servlet belongs to the View layer component. Because simply using JSP can realize simple addition, deletion, modification and checking, and no Java class files are created.


to sum up

This article leads you to review the classic MVC software design framework, distinguish the component positioning of JSP and Servlet, and make it clear that Servlet is in the Controller layer in MVC, and it can also be in the View layer in non-strict MVC mode. Through the positioning of the component role, a deeper understanding and mastery of the principles of Servlet and JSP, and the classic design framework of MVC. In our subsequent framework study, all the principles are developed around this foundation. The framework only provides us with a convenient tool, and the foundation must be firmly laid.

Insert picture description here


Thank you for your support. I am Bailu, a program ape who works tirelessly. I hope this post can help everyone, and welcome everyone's one-click three-connection! If you have any questions, suggestions or supplements, you can leave a message at the bottom of the post to help more people!
More information WeChat search public accountWDeerCode code circle

Guess you like

Origin blog.csdn.net/qq_22695001/article/details/109364528