当前位置:首页 > Web开发 > 正文

JSP静态包含和动态包含的区别

2024-03-31 Web开发

jsp动态包含和静态包含实现的内容一样,但是实现的方式不同。

jsp静态包含<%@ include file="" %>


创建include1.jsp和include2.jsp

  include1.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ include file="include2.jsp"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> this is include page 1 </body> </html>

  include2.jsp代

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> this is include page 2 </body> </html>

运行结果:

  如果是静态包含,则只会生成一个java文件

  

技术图片

  原因:因为include1.jsp要把include2.jsp内容完全引用,所有include1.jsp会和include2.jsp合成一个jsp,然后翻译成.java文件,最后编译运行。

jsp动态包含<jsp:include page=""></jsp:include>

创建Newfile1.jsp和NewFile2.jsp

Newfile1.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:include page="/NewFile2.jsp"></jsp:include> this is newFile1 </body> </html>

Newfile2.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> this is newFile2 </body> </html>

运行结果:

如果是动态包含,,则会生成两个.java文件

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/41324.html