In-depth understanding of the MVC three-tier architecture and detailed explanation of the practical application of Filter

1. MVC three-tier architecture

1.1 What is MVC

MVC is model model, view view, Controller controller.

M represents the business model, V represents the user interface, and C represents the controller. The purpose of using MVC is to separate the implementation code of M and V so that the same program can use different expressions.

1.1.1 Workflow of MVC Architecture Program

(1) The user sends a request to the server by viewing the page, which can be a form request, a hyperlink request, an Ajax request, etc.
(2) After receiving the request, the server controller parses the request, finds the corresponding model, and processes the user request
(3) After the model is processed, submit the processing result to the controller
(4) After receiving the processing result, the controller finds the response view page to be sent back to the client according to the processing result. After the page is rendered (populated with data), it is sent to the client.

MVC architecture before 1.2

The user directly accesses the control layer, and the control layer can directly operate the database: servlet->CRUD (database addition, deletion, modification and query operation code)->database


The previous MVC architecture is not conducive to maintenance, and the program is very bloated. The servlet will be responsible for the implementation of many functions, such as processing request responses, view jumping, processing JDBC, business code and logic code, etc.


Adding a layer to the architecture can solve the problem, as follows:

1.2 MVC three-tier architecture


  • Business processing: business logic (service)
  • Data persistence layer: CRUD (DAO layer)


  • Display data
  • Provide links, initiate requests, such as a link, from link, etc.


  • Accept the user's request: req, get the user's request parameters, Session information, etc.

  • Hand it over to the business layer to process the corresponding code.

  • Control the jump of the view: the process is as follows:

    The user initiates a login request - > the controller accepts the user's login request - > processes the user's request, such as obtaining the user's login parameters (username,password, etc.) - > gives it to the business layer to process the login business (judge whether the user name and password are correct) - > the Dao layer queries whether the user name and password are correct - > the database

2. Filter Filter

A filter refers to a "filter", which is a filter between the client and server resource files.

Before accessing resource files, a series of filters are used to modify and judge requests, and requests that do not meet the rules are intercepted or modified in the middle; responses can also be filtered, intercepted or modified.

Role: used to filter the data of the website.

  • Handling Chinese garbled characters
  • login verification etc.

2.1 The actual combat process

1. Create a new idea project, select Maven to create a new project, do not select a template, and the project name is Filtergo.

2. Go to the project name folder, choose to add a new framework, choose the web framework, and the version is 4.0. The current directory is as follows:

3. Use the filter Filter process:

①Guide package:

Import Servlet dependencies:


Import JSP dependencies:


Import the dependencies of JSTL expressions:


Import standard dependencies:


Connect to the database:


②: Write a filter:

1. Create a new interface class: com.zhu.servlet. Inherit implements, inherit class: Filter

Note: You must not choose the wrong one, you must choose the serclet in the following figure:

2. Rewrite 3 methods, the methods are described as follows:

Initialization method: init.

Destruction method: destroy().

The main thing is to rewrite the doFilter method, as follows, we first write an output Chinese, if there is no wrong guess, it will output garbled characters, we first configure and register the url and the corresponding mapping.

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    servletResponse.getWriter().write("Hello, enterprising pig!");

url and corresponding mapping:


Configure the web container—tomcat

3. Create a new Java class named showservlet that inherits doGet and doPost.

4. Visit the url, and it should output garbled characters without any accident:

5. Do we set the character encoding, this case can handle a small part of the character encoding:


If there are a thousand servlet s now, it is necessary to write a thousand set character encodings, which is obviously not reasonable. The use of this time is the role of Filter.

6. First write a character encoding setting for Filter:

servletResponse.getWriter().write("Hello, enterprising pig!");

7.Chain is a chain.

Its role is to keep our request going. If there is no such chain, the program will be intercepted and stopped when it runs here.

System.out.println("before execution------------");
System.out.println("after execution------------");

8. Configure filtering


Observe the above code, /school/* means that all paths after school will be filtered.

So in order to distinguish them, I designed the following route registration and mapping, as follows:



The above path /school/s1 and path /s1 both point to the Filter class written before.

9. Access the path and observe the test results as follows:

10. It can be seen that when we visit the school path, the characters will be filtered, which avoids the problem of garbled characters, and is also very efficient and fast.

2.2 Summary

1. When the project starts, initialization is performed, and you can wait for the appearance of the filter object at any time.

2. When the web service is closed, the destroy operation is performed.

Tags: architecture mvc filter servlet

Posted by nakins on Sat, 03 Sep 2022 21:38:07 +0530