SP指令是指:用于設置JSP頁面相關屬性的一個語法命令,例如:設置頁面編碼字符集、導入其他包等等。JSP中提供了三個指令,分別是:page指令、include指令、taglib指令。其中page指令用于設置JSP頁面屬性,include指令用于引入其他的JSP文件,taglib指令用于引入標簽庫。這一小節內容介紹include指令的使用。
include指令作用:將指定的文件引入到當前JSP頁面里面。include指令會將引入的文件內容嵌入到當前JSP頁面中的對應位置。
<%@ include file="文件的相對路徑" %>
案例代碼:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>這是HTML頭部</title>
</head>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 引入頭部文件 --%>
<%@ include file="header.jsp" %>
<body>
<div style="background-color: cadetblue">
這是正文內容區域
</div>
<%-- 引入底部文件 --%>
<%@ include file="footer.html" %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div style="background-color: antiquewhite;">
這是footer底部內容區域
</div>
啟動Tomcat容器,瀏覽器訪問http://localhost:8080/servlet/include.jsp,結果如下:
include指令的本質是什么呢???我們來查看下編譯之后的Java源代碼,找到上面include.jsp文件編譯之后的源文件,如下所示:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.5.98
* Generated at: 2024-02-25 05:06:41 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 com.gitcode.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class include_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
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("/footer.jsp", Long.valueOf(1708837593266L));
_jspx_dependants.put("/header.jsp", Long.valueOf(1708837593271L));
}
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages=new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes=null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory==null) {
synchronized (this) {
if (_el_expressionfactory==null) {
_el_expressionfactory=_jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager==null) {
synchronized (this) {
if (_jsp_instancemanager==null) {
_jsp_instancemanager=org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
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 java.lang.String _jspx_method=request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允許 GET、POST 或 HEAD。Jasper 還允許 OPTIONS");
return;
}
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');
out.write('\n');
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>這是HTML頭部</title>\r\n");
out.write("</head>");
out.write("\r\n");
out.write("<body>\r\n");
out.write("\r\n");
out.write("<div style=\"background-color: cadetblue\">\r\n");
out.write(" 這是正文內容區域\r\n");
out.write("</div>\r\n");
out.write("\r\n");
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("<div style=\"background-color: antiquewhite;\">\r\n");
out.write(" 這是footer底部內容區域\r\n");
out.write("</div>");
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 {
if (response.isCommitted()) {
out.flush();
} else {
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);
}
}
}
通過上面源代碼,可以看到,使用include指令引入的兩個文件,最終都會將兩個文件中的內容直接嵌入到當前include.jsp文件里面,如下所示:
所以include指令的本質就是將引入文件中的內容,直接拼接到當前JSP頁面的對應位置。這里也就會存在一個問題,引入的JSP文件中,不能存在和當前JSP頁面相同的變量名稱,因為變量名稱相同會導致編譯失敗。另外,使用include指令引入其他的JSP文件時候,只會生成訪問的那個JSP文件的源代碼,被引入的JSP文件不會生成對應的源代碼。
以上,就是include指令的使用及其本質。
今天就到這里,未完待續~~
1題. 編寫一個Filter,需要()
A. 繼承Filter 類
B. 實現Filter 接口
C. 繼承HttpFilter 類
D. 實現HttpFilter接口
正確答案為:B
第2題. 自定義標簽的配置文件放在________
A. WebRoot
B. lib
C. classes
D. WEB-INF
正確答案為:D
第3題. 有關會話跟蹤技術描述正確的是(多選)
A. Cookie是Web服務器發送給客戶端的一小段信息,客戶端請求時,可以讀取該信息發送到服務器端
B. 關閉瀏覽器意味著會話ID丟失,但所有與原會話關聯的會話數據仍保留在服務器上,直至會話過期
C. 在禁用Cookie時可以使用URL重寫技術跟蹤會話
D. 隱藏表單域將字段添加到HTML表單并在客戶端瀏覽器中顯示
正確答案為:ABC
第4題. 在J2EE中,重定向到另一個頁面,以下()語句是正確的
A. request . sendRedirect(“http :// www . svse . com . cn”);
B. request . sendRedirect();
C. response . sendRedirect(“http: // www . svse . com . cn”);
D. response .sendRedirect();
正確答案為:C
第5題. EL表達式,${10 mod3},執行結果為:
A. 10 mod 3
B. 1
C. 3
D. null
正確答案為:B
第6題. 自定義標簽的作用是
A. 編寫和使用方便
B. 規定是這樣的,如果不用,別人會說我們不專業
C. 可以減少jsp中的java代碼,將代碼與界面標簽分離,簡化前臺開發
D. 連數據庫
正確答案為:C
第7題. request.getRequestDispatcher().forward(request,response)稱之為
A. 流轉
B. 轉發
C. 重定向
D. 導航
正確答案為:B
第8題. 有關Servlet的生命周期說法正確的有 (多選)
A. Servlet的生命周期由Servlet實例控制
B. init()方法在創建完Servlet實例后對其進行初始化,傳遞的參數為實現ServletContext接口的對象
C. service()方法響應客戶端發出的請求
D. destroy()方法釋放Servlet實例
正確答案為:BCD
第9題. 在J2EE中,給定某Servlet的代碼如下,編譯運行該文件,以下陳述正確的是()。(選擇一項)
Public class Servlet1 extends HttpServlet{
Publicvoid init() throws ServletException{
}
Publicvoid service(HttpServletRequest request,HttpServletResponse response)
ThrowsServletException,IOException{
PrintWriterout=response.getWriter();
out.println(“hello!”);
}
}
A. 編譯該文件時會提示缺少doGet()或者dopost()方法,編譯不能夠成功通過
B. 編譯后,把Servlet1.class放在正確位置,運行該Servlet,在瀏覽器中會看到輸出文字:hello!
C. 編譯后,把Servlet1.class放在正確位置,運行該Servlet,在瀏覽器中看不到任何輸出的文字
D. 編譯后,把Servlet1.class放在正確位置,運行該Servlet,在瀏覽器中會看到運行期錯誤信息
正確答案為:B
第10題. 在Servlet中,response.getWriter()返回的是____________
A. JspWriter對象
B. PrintWriter對象
C. Out對象
D. ResponseWriter對象
正確答案為:B
第11題. 在web.xml中使用___________標簽配置過濾器
A. <filter>和<filter-mapping>
B. <filter-name>和<filter-class>
C. <filter>和<filter-class>
D. <filter-pattern>和<filter>
正確答案為:A
第12題. 自定義標簽的描述文件在web.xml中配置正確的
A. <taglib>
<tag-uri>bob-tld</tag-uri>
<tag-location>/WEB-INF/bob.tld</tag-location>
</taglib>
B. <tag>
<taglib-uri>bob-tld</taglib-uri>
<taglib-location>/WEB-INF/bob.tld</taglib-location>
</tag>
C. <jsp-taglib>
<taglib-uri>bob-tld</taglib-uri>
<taglib-location>/WEB-INF/bob.tld</taglib-location>
</jsp-taglib>
D. <jsp-config>
<taglib>
<taglib-uri>bob-tld</taglib-uri>
<taglib-location>/WEB-INF/bob.tld</taglib-location>
</taglib>
</jsp-config>
正確答案為:D
第13題. J2EE中,Servlet API為使用Cookie,提供了()類。
A. javax.servlet.http.Cookie
B. javax.servlet.http.HttpCookie
C. javax.servlet. Cookie
D. javax.servlet.http.HttpCookie
正確答案為:A
是Include.jsp文件
<!-- this is the Include.jsp -->
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>動態加載文件</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE=10 COLOR=blue>動態加載文件</FONT>
</CENTER>
<br>
<HR>
<br>
<font size=5>
<!-- 用jsp:include指令動態加載文件 -->
<jsp:include page="jspInsert.jsp">
<jsp:param name="name" value="jsp:include"/>
<jsp:param name="file" value="jspInsert.jsp"/>
</jsp:include>
</font>
</BODY>
</HTML>
下面是jspInsert.jsp文件
<HTML>
<BODY>
您好!這里我們用到了
<Font Color=Blue>
<%=request.getParameter("name")%>
</Font>
指令!
<BR>
我們在這里加載了文件:
<Font Color=Blue>
<%=request.getParameter("file")%>
</Font>。<br>
</HTML>
</BODY>
*請認真填寫需求信息,我們會在24小時內與您取得聯系。