In the development process of Java Web, Servlet was first responsible for the development of display layer (page). Later, because JSP has more advantages in the development of page layer, it comprehensively replaced Servlet and acted as the development of page layer.
However, after the emergence of JSP, the design architecture of Java Web appeared a chaotic and immature period, that is, the so-called JSP Model 1, JSP Model 1.2 and immature JSP Model 2 period, and the mature Model 2 period (as a distinction, it can be called MVC mode). In addition to MVC model, other models have been eliminated in the industry, but it does not rule out that there are still unprofessional company teams who are conservative and incomplete. However, for learners, they can understand and make a comparison in order to clearly understand why MVC mode is better. MVC mode will be explained in the next article.
Let's complete a table showing the data of all students in the above article as an example to explain these three modes. The renderings are as follows:
JSP Model 1
The architecture diagram of model 1 is shown in the figure below. JSP is not only responsible for page processing, but also responsible for business logic processing. If you have PHP development experience, you will find that Java Web development was similar to PHP development mode at that time. This is also the reason why I concluded that PHP would not have a good development future when I was engaged in PHP development website more than ten years ago. Because PHP is still using the development model that the Java Web has long abandoned.
The advantage of model 1 is that the knowledge category is stipulated in JSP, and the knowledge involved by developers is narrower, flexible and convenient. However, the development of complex systems will cause confusion and is not conducive to communication and maintenance.
The code in the previous article is as follows:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.sql.*"%> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% //Use reflection to generate MySQL driver class objects Class.forName("com.mysql.jdbc.Driver"); //Establish a connection with the database: url, user, pwd Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456"); //Create SQL statement executor Statement stat = conn.createStatement(); //Execute SQL statement to get result set (select statement will get result set) ResultSet rs = stat.executeQuery("select * from student"); //Processing results //Note that we should flexibly use the characteristics of JSP script fragments and flexibly build a table with HTML %> <!-- Note that the HTML Instead of using CSS Decoration, which is not in line with the specification. It will be corrected later when decorating the page --> <table border=1> <tr> <th>ID</th> <th>full name</th> <th>Gender</th> <th>Age</th> </tr> <% //Loop build tr while (rs.next()) { %> <tr> <td><%=rs.getInt("id")%></td> <td><%=rs.getString("name")%></td> <td><%=rs.getString("gender")%></td> <td><%=rs.getInt("age")%></td> </tr> <% } //Close resources such as connections rs.close(); stat.close(); conn.close(); %> </table> </body> </html>
JSP Model 1.2
The architecture diagram of model 1.2 is shown in the figure below. It is an improvement of model 1 based on the idea of decoupling. By adding JavaBeans, JavaBeans (Java classes) are responsible for business logic processing, realizing the separation of page and business logic.
Because JSP access to JavaBean s is involved, an entity class Student should be built to facilitate method calls and transfer data as return values.
The package and class results are as follows:
code:
Student.java:
package entity; public class Student { private int id; private String name; private String gender; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
StudentDao.java:
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import entity.Student; public class StudentDao { public List<Student> search() { List<Student> list = new ArrayList<>(); Connection conn = null; Statement stat = null; ResultSet rs = null; // Use reflection to generate MySQL driver class objects try { Class.forName("com.mysql.jdbc.Driver"); // Establish a connection with the database: url, user, pwd conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456"); // Create SQL statement executor stat = conn.createStatement(); // Execute SQL statement to get result set (select statement will get result set) rs = stat.executeQuery("select * from student"); // Processing results while (rs.next()) { Student stu = new Student(); stu.setId(rs.getInt("id")); stu.setName(rs.getString("name")); stu.setGender(rs.getString("gender")); stu.setAge(rs.getInt("age")); list.add(stu); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Close resources correctly try { if (rs != null) { rs.close(); } if (stat != null) { stat.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; } }
show2.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*"%> <%@ page import="entity.*,dao.*"%> <!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> <% //The method of calling Java in JSP is the same as calling Java method in Java. StudentDao stuDao = new StudentDao(); List<Student> list = stuDao.search(); 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>
JSP Model 2 immature period
The architecture diagram of model 2 is shown in the figure below. Java developers re-examine the existing technology and find that Servlet is not good for nothing. It can act as a controller, that is, it is responsible for the scheduling between requests sent by the client and classes and pages.
However, the immaturity of this model is that JSP and JavaBean can still interact directly. Readers can find the historical traces of that period through the sql tags of JSTL and the action elements of JSP.
Even at present, many company teams are still doing such non-standard things, that is, JSP directly accesses Java classes, which is called "flexible", but this actually gives up the natural advantages of Java. That is, preciseness, especially attention should be paid to it.
JSP Model 2 in this mature period is chaotic to call, and there are even multiple entries. We don't take code examples. Our next article will directly enter the learning of mature and standardized MVC mode.