First, declare that MVC and three-tier architecture are two different concepts. But it is to realize the principle of single function and realize the decoupling of modules. The two of them are not in conflict, but can coexist.
The two architecture technologies are introduced below:
MVC
MVC is a Model, View and Controller, which generally realize the division of responsibilities from the technical level. Taking the traditional Java Web as an example, View can be implemented by JSP, JSTL, other template engines and HTML, CSS and JS, Controller is implemented by Servlet, Struts and Spring MVC, and Model is implemented by Java. Of course, it can contain more tools Framework and other technologies.
In the industry, now almost no one calls "JSP Model 2 model", but MVC model or MVC pattern. In modern enterprise development, the realization of standardized MVC pattern is the basic principle.
In actual development, there is almost no form of JSP calling JavaBean (that is, JSP and Servlet introduced in the previous article call JavaBean). However, there is a non-standard thing, that is, browsers can directly access JSP pages.
This situation usually occurs in small projects where developers are lazy or nonstandard. Regular projects must strictly follow the specifications, that is, requests must access the Controller and then be forwarded to the JSP page. In order to restrict the behavior that JSP cannot be accessed directly, the industry even strictly requires that JSP files can only be stored in the WebContent/WEB-INF folder Only Java calls inside the server are allowed, and access through Http requests is not allowed. This avoids accessing JSP pages directly through the browser.
The code practice is still to complete the page shown in the figure below, query data from the database and display it.
The project architecture is as follows. Add a new controller package, create a StudentController in it, and create a show3.jsp page.
StudentController.java:
package controller; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.StudentDao; import entity.Student; @WebServlet("/stu") public class StudentController extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { StudentDao stuDao = new StudentDao(); List<Student> list = stuDao.search(); //Put the queried Student object list into the request scope request.setAttribute("list", list); //Forward to jsp request.getRequestDispatcher("show3.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }
show3.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*"%> <%@ page import="entity.*"%> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <table border=1> <tr> <th>ID</th> <th>full name</th> <th>Gender</th> <th>Age</th> </tr> <% //Get the list of student objects from the request scope List<Student> list = (List<Student>) request.getAttribute("list"); for (Student stu : list) { %> <tr> <td><%=stu.getId()%></td> <td><%=stu.getName()%></td> <td><%=stu.getGender()%></td> <td><%=stu.getAge()%></td> </tr> <% } %> </table> </body> </html>
visit http://localhost:8080/testWeb03/stu , the student data can be displayed normally.
Three tier architecture
Different from MVC, the three-tier architecture is not very different from technology, but divides java code into levels according to large functions. (if it is an interface system developed by Swing, each of the three layers applies Java language).
There is no concept of "layer" at the Java language level, which is realized through "package". That is, the "package" is used to sort the classes dealing with a kind of things together. It is customary in the industry to divide them into three layers, namely view+controller, business logic (service) and data access (dao). dao is the abbreviation of data access object.
As shown in the figure:
Generally, the controller package and JSP are classified as the view display layer, but it can also be considered as a four-tier architecture, that is, there is an additional control layer. That is, it does not have to be divided into three layers. For example, our MVC case has only the view display layer and data access layer, but a two-tier architecture, because our project is still relatively simple, so it is not necessary to use three layers.
No matter how many layers, there is the concept of "layer". Packages are only inductive classification, but layers are divided into layers. Under the hierarchical structure, there are such rules:
- The upper layer calls the lower layer
- Cannot be called across layers
- The next layer cannot access the previous layer
Taking our current two-tier architecture as an example, the StudentServlet under the contraller package in the view layer accesses StudentDao, and StudentDao accesses the database. It is hierarchical and has a clear division of labor.
Later, we will use MVC mode + two-tier architecture for project development. When the project reaches a certain complexity, it will evolve into a three-tier architecture.