JSP包含有兩種方式:靜態包含和動態包含。下面我們將通過JSP運行的特點(轉換爲servlet再執行)來比較二者的區別。
一、靜態包含 <%@include file=“url”%>
我們先寫一個正常的小項目,來看下靜態包含是如何運行的。
測試一:
top.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=UTF-8">
<title>top</title>
</head>
<body>
<h2>這是TOP頁面</h2>
</body>
</html>
footer.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=UTF-8">
<title>footer</title>
</head>
<body>
<h2>腳頁</h2>
</body>
</html>
index.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=UTF-8">
<title>首頁</title>
</head>
<body>
<%@include file="top.jsp"%>
<h1>這是首頁</h1>
<%@include file="footer.jsp"%>
</body>
</html>
在index頁面中使用的是靜態包含,將top和footer兩個頁面包含進來,現在運行一下,看臨時文件夾中會產生幾個Java文件
從這可以看出,運行這個項目只會生成一個Java文件,說明運行這個項目相當於只運行一個頁面,點開Java文件看源碼:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.41
* Generated at: 2016-09-28 14:18:14 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
_jspx_dependants.put("/top.jsp", Long.valueOf(1475071806944L));
_jspx_dependants.put("/footer.jsp", Long.valueOf(1475071854827L));
}
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
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=UTF-8\">\r\n");
out.write("<title>首頁</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t");
out.print(request.getContextPath() );
out.write('\r');
out.write('\n');
out.write(' ');
out.write("\r\n");
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=UTF-8\">\r\n");
out.write("<title>top</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<h2>這是TOP頁面</h2>\r\n");
out.write("</body>\r\n");
out.write("</html>");
out.write("\r\n");
out.write("\t<h1>這是首頁</h1>\r\n");
out.write("\t");
out.write("\r\n");
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=UTF-8\">\r\n");
out.write("<title>footer</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<h2>腳頁</h2>\r\n");
out.write("</body>\r\n");
out.write("</html>");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
從這個源碼可以看出,index是將兩個JSP文件先包含進來,再運行。
結論1:靜態包含是先將頁面代碼包含進來,再運行。
測試2:
結論1中得出,靜態包含是先包含代碼進來再運行,既然先包含代碼,那麼包含的代碼之間會有衝突嗎?下面我們再測試一下,包含的頁面之間代碼是否會衝突。只要讓代碼產生衝突,最簡單的就是讓JSP的指令不一樣就可以了。下面我們就只改動top.jsp頁面代碼,將指令中的編碼格式改一下:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>footer</title>
</head>
<body>
<h2>腳頁</h2>
</body>
</html>
重新運行index.jsp頁面:
運行之後會發現報錯了,報錯的信息是:
/footer.jsp (line: 1, column: 2) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=UTF-8, new: text/html; charset=GBK)
結論2:因爲靜態包含是先將頁面代碼包含進來,再運行,所以頁面之間相同標籤屬性配置不同的話會有衝突。至少兩個jsp頁面的<%@page contentType=“text/html;charset=utf-8”%>應該保持一致。同理,包含的jsp頁面與被包含的jsp頁面最好沒有重複的html標籤。否則會發生覆蓋現象。
測試3:
靜態包含時,能否傳遞參數呢?這種時候傳遞參數,一般大家都會想到地址傳參:<%@include file="top.jsp?i=9"%>,這樣是否能正常運行呢?答案是不能!!!因爲此時的頁面是直接被包含進來的,這樣設置參數傳遞是沒有任何意義的。甚至是不能通過編譯!
測試4:
既然是直接把代碼包含進來,那麼服務器通過響應請求傳遞過來的參數,是否能夠被兩個頁面共用呢?下面寫一個簡單的地址傳參進行驗證。
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="index.jsp?i=9">首頁</a>
</body>
</html>
在其單個頁面中輸出該i值,看是否能輸出9:
結論3:三個頁面都能通過request接收到test.jsp頁面傳來的值,說明這三個或頁面是共享一個request的。
關於JSP動態包含,請閱讀 JSP的靜態包含和動態包含詳細講解 (二)
CSDN博客地址:http://blog.csdn.net/jdjdndhj 歡迎關注!
本文出自 “程序猿的故事” 博客,轉載請與作者聯繫!