The difference between include instruction and include action element in jsp
In short:
- The introduced resource and the original resource of the include action element can be executed independently, and the include instruction needs to be merged
- The include action element run contains and only contains the run results (only the org.apache.jasper.runtime.JspRuntimeLibrary.include function is called), and the include instruction compilation contains and contains the source code (all the code of the entire page is added when jsp is converted to Java file (the content output with out.write)
The include action element is similar to the include method of the requestDispatcher
Example (I use eclipse):
Main code:
include directive:
include.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>include</title> </head> <body> <%Thread.sleep(5000); %> include.jsp Chinese in Li<br /> </body> </html>
dynamicInclude.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>dynamicInclude</title> </head> <body> dynamicInclude.jsp Chinese in Li<br /> <jsp:include page="include.jsp" flush="true" /> </html>
Page source code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>dynamicInclude</title> </head> <body> dynamicInclude.jsp Chinese in Li<br /> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>include</title> </head> <body> include.jsp Chinese in Li<br /> </body> </html> </html>
include action element:
include.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> //Welcome, the time is: </body> </html>
date.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% out.println(new java.util.Date().toLocaleString()); %> </body> </html>
It can be seen from the files after compilation
Include directive:
After the inclusion, there are more jsp.java:
static { _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1); _jspx_dependants.put("/date.jsp", Long.valueOf(1526108987260L)); } out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.println(new java.util.Date().toLocaleString()); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); out.write("\r\n");
include action element:
Dynamicinclude · jsp.java has increased after inclusion:
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "include.jsp", out, true);
Conclusion:
The Include instruction adds all the code of the entire page (output with out.write) when jsp is converted to Java file
The Include action element only calls the org.apache.jasper.runtime.JspRuntimeLibrary.include function
The source code of both web pages is similar, which is to add the content of the included page (all html)