JavaWeb---總結(十六)JSP指令

來源:互聯網
上載者:User

標籤:cti   ppa   跳轉   amp   vold   style   dir   設定   nfa   

一、JSP指令簡介

  JSP指令(directive)是為JSP引擎而設計的,它們並不直接產生任何可見輸出,而只是告訴引擎如何處理JSP頁面中的其餘部分。

  在JSP 2.0規範中共定義了三個指令:

  • page指令

  • Include指令

  • taglib指令

  JSP指令的基本文法格式:<%@ 指令 屬性名稱="值" %>
  例如:

1 <%@ page contentType="text/html;charset=gb2312"%>

  如果一個指令有多個屬性,這多個屬性可以寫在一個指令中,也可以分開寫。
  例如:

1 <%@ page contentType="text/html;charset=gb2312"%>2 <%@ page import="java.util.Date"%>

也可以寫作:

1 <%@ page contentType="text/html;charset=gb2312" import="java.util.Date"%>
二、Page指令

  page指令用於定義JSP頁面的各種屬性,無論page指令出現在JSP頁面中的什麼地方,它作用的都是整個JSP頁面,為了保持程式的可讀性和遵循良好的編程習慣,page指令最好是放在整個JSP頁面的起始位置。例如:

  

JSP 2.0規範中定義的page指令的完整文法:

 1 <%@ page  2     [ language="java" ]  3     [ extends="package.class" ]  4     [ import="{package.class | package.*}, ..." ]  5     [ session="true | false" ]  6     [ buffer="none | 8kb | sizekb" ]  7     [ autoFlush="true | false" ]  8     [ isThreadSafe="true | false" ]  9     [ info="text" ] 10     [ errorPage="relative_url" ] 11     [ isErrorPage="true | false" ] 12     [ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] 13     [ pageEncoding="characterSet | ISO-8859-1" ] 14     [ isELIgnored="true | false" ] 15 %>

2.1、 page指令的import屬性

  在Jsp頁面中,Jsp引擎會自動匯入下面的包

  • java.lang.*

  • javax.servlet.*

  • javax.servlet.jsp.*

  • javax.servlet.http.*

  可以在一條page指令的import屬性中引入多個類或包,其中的每個包或類之間使用逗號(,)分隔

例如:

1 <%@ page import="java.util.*,java.io.*,java.sql.*"%>

上面的語句也可以改寫為使用多條page指令的import屬性來分別引入各個包或類

例如:

1 <%@ page import="java.util.Date"%>2 <%@ page import="java.io.*" %>3 <%@ page import="java.sql.*" %>
2.2、 page指令的errorPage屬性
  • errorPage屬性的設定值必須使用相對路徑,如果以“/”開頭,表示相對於當前Web應用程式的根目錄(注意不是網站根目錄),否則,表示相對於當前頁面

  • 可以在web.xml檔案中使用<error-page>元素為整個Web應用程式設定錯誤處理頁面。

  • <error-page>元素有3個子項目,<error-code>、<exception-type>、<location>

  • <error-code>子項目指定錯誤的狀態代碼,例如:<error-code>404</error-code>

  • <exception-type>子項目指定異常類的完全限定名,例如:<exception-type>java.lang.ArithmeticException</exception-type>

  • <location>子項目指定以“/”開頭的錯誤處理頁面的路徑,例如:<location>/ErrorPage/404Error.jsp</location>

  • 如果設定了某個JSP頁面的errorPage屬性,那麼在web.xml檔案中設定的錯誤處理將不對該頁面起作用。

2.3、使用errorPage屬性指明出錯後跳轉的錯誤頁面

比如Test.jsp頁面有如下的代碼:

 

1 <%@ page language="java" import="java.util.*" errorPage="/ErrorPage/error.jsp" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title>測試page指令的errorPage屬性</title>
5 </head>
6 <body>
7 <%
8 //這行代碼肯定會出錯,因為除數是0,一運行就會拋出異常    9 int x = 1/0;10 %>11 </body>12 </html>

 

  在Test.jsp中,page指令的errorPage屬性指明了出錯後跳轉到"/ErrorPage/error.jsp",error.jsp頁面代碼如下:1 <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>

2 <html>

3   <head>

4     <title>錯誤資訊友好提示頁面</title>

5   </head>

6   <body>

7           對不起,出錯了,請聯絡管理員解決!

8   </body>

9 </html>

 

運行結果如下:

  

2.4、在web.xml中使用<error-page>標籤為整個web應用設定錯誤處理頁面

例如:使用<error-page>標籤配置針對404錯誤的處理頁面

web.xml的代碼下

 

 

 1 <?xml version="1.0" encoding="UTF-8"?> 
2 <web-app version="3.0"
3    xmlns="http://java.sun.com/xml/ns/javaee"
4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7 <display-name></display-name>
8 <welcome-file-list>
9 <welcome-file>index.jsp</welcome-file>
10 </welcome-file-list>
11
12 <!-- 針對404錯誤的處理頁面 -->
13 <error-page>
14 <error-code>404</error-code>
15 <location>/ErrorPage/404Error.jsp</location>
16 </error-page>
17
18 </web-app>

404Error.jsp代碼如下: 1 <%@ page language="java" import="java.util.*"pageEncoding="UTF-8"%> 

2 <html> 

3   <head> 

4     <title>404錯誤友好提示頁面</title> 

5     <!-- 3秒鐘後自動跳回首頁 --> 

6     <meta http-equiv="refresh"content="3;url=${pageContext.request.contextPath}/index.jsp"> 

7   </head> 

8   <body> 

9     <img alt="對不起,你要訪問的頁面沒有找到,請聯絡管理員處理!" 

10    src="${pageContext.request.contextPath}/img/404Error.png"/><br/>

11     3秒鐘後自動跳回首頁,如果沒有跳轉,請點擊<ahref="${pageContext.request.contextPath}/index.jsp">這裡</a>12  </body>

13 </html>

當訪問一個不存在的web資源時,就會跳轉到在web.xml中配置的404錯誤處理頁面404Error.jsp,如所示:

  

2.5、關於在web.xml中使用<error-page>標籤為整個web應用設定錯誤處理頁面在IE下無法跳轉的解決辦法

  這裡需要注意的是,如果錯誤頁面比較小,那麼當訪問伺服器上不存在的web資源或者訪問伺服器出錯時在IE瀏覽器下是無法跳轉到錯誤頁面的,顯示的是ie自己的錯誤頁面,而在Firefox和google瀏覽器下(其他瀏覽器沒有測試過)是不存在注意的問題的。

我們可以通過下面的實驗來證明

  在web.xml中配置500錯誤時的錯誤友好提示頁面

1 <!-- 針對500錯誤的處理頁面 -->
2 <error-page>
3 <error-code>500</error-code>
4 <location>/ErrorPage/500Error.jsp</location>
5 </error-page>

500Error.jsp頁面的代碼如下:

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title>500(伺服器錯誤)錯誤友好提示頁面</title>
5 <!-- 3秒鐘後自動跳回首頁 -->
6 <meta http-equiv="refresh" content="3;url=${pageContext.request.contextPath}/index.jsp">
7 </head>
8 <body>
9 <img alt="對不起,伺服器出錯!"
10    src="${pageContext.request.contextPath}/img/500Error.png"/><br/>
11 3秒鐘後自動跳回首頁,如果沒有跳轉,請點擊<a href="${pageContext.request.contextPath}/index.jsp">這裡</a>
12 </body>
13 </html>

 

500Error.jsp頁面的位元組大小

 在IE8瀏覽器下的運行結果:

  在IE下訪問Test.jsp出現500錯誤後,顯示的是ie自己的錯誤頁面,而不是我們定製的那個500錯誤頁面,而在google和Firefox下卻是可以正常跳轉到我們自己定製的那個500錯誤頁面的,如所示:

很多人遇到這個問題,而解決這個問題的辦法有兩種:

1、修改IE瀏覽器的設定(不推薦)

  操作步驟:在IE【工具】->【Internet選項】->【進階】中勾掉【顯示友好http錯誤提示】

  

  經過這樣的設定之後,訪問伺服器出錯後就可以直接跳轉到我們定製的500錯誤頁面了,如所示:

  

  這種做法需要修改用戶端瀏覽器的配置,不推薦這樣的方式。

2.不修改IE瀏覽器的設定下確保定製的錯誤頁面的大小>1024位元組

  修改500Error.jsp,多添加一些內容,讓頁面的位元組數大一些,修改後的500Error.jsp的代碼如下:

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <html>
3 <head>
4 <title>500(伺服器錯誤)錯誤友好提示頁面</title>
5 <!-- 3秒鐘後自動跳回首頁 -->
6 <meta http-equiv="refresh" content="3;url=${pageContext.request.contextPath}/index.jsp">
7 </head>
8 <body>
9 <img alt="對不起,伺服器出錯了,請聯絡管理員解決!" 10    src="${pageContext.request.contextPath}/img/500Error.png"/><br/>
11 3秒鐘後自動跳回首頁,如果沒有跳轉,請點擊<a href="${pageContext.request.contextPath}/index.jsp">這裡</a>
12 </body>
13 </html>

 

  也就多加了幾個中文,讓500Error.jsp多了幾個位元組,500Error.jsp現在的位元組數如下:

  

  在IE下訪問,當伺服器出錯時,就可以正常跳轉到500Error.jsp這個定製的錯誤頁面了,如所示:

  經過測試,當定製的錯誤頁面的size=617bytes時,在IE8下已經可以跳轉到定製的錯誤頁面了,其他版本的IE瀏覽器沒有經過測試,不過為了保險起見,定製的錯誤頁面的size最好超過1024bytes。

2.6、使用page指令的的isErrorPage屬性顯式聲明頁面為錯誤頁面

  如果某一個jsp頁面是作為系統的錯誤處理頁面,那麼建議將page指令的isErrorPage屬性(預設為false)設定為"true"來顯式聲明這個Jsp頁面是一個錯誤處理頁面。

例如:將error.jsp頁面顯式聲明為錯誤處理頁面

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%> 
2 <html>
3 <head>
4 <title>錯誤資訊友好提示頁面</title>
5 </head>
6
7 <body>
8           對不起,出錯了,請聯絡管理員解決!
9 </body>
10 </html>

 

  將error.jsp頁面顯式聲明為錯誤處理頁面後,有什麼好處呢,好處就是Jsp引擎在將jsp頁面翻譯成Servlet的時候,在Servlet的 _jspService方法中會聲明一個exception對象,然後將運行jsp出錯的異常資訊儲存到exception對象中,如下所示:

  

  由於Servlet的_jspService方法中聲明了exception對象,那麼就可以在error.jsp頁面中使用exception對象,這樣就可以在Jsp頁面中拿到出錯的異常資訊了,如下:

  

  如果沒有設定isErrorPage="true",那麼在jsp頁面中是無法使用exception對象的,因為在Servlet的_jspService方法中不會聲明一個exception對象,如下所示:

  

  

  Jsp有9大內建對象,而一般情況下exception對象在Jsp頁面中是擷取不到的,只有設定page指令的isErrorPage屬性為"true"來顯式聲明Jsp頁面是一個錯誤處理頁面之後才能夠在Jsp頁面中使用exception對象。

三、include指令

  在JSP中對於包含有兩種語句形式:

  1. @include指令

  2. <jsp:include>指令

3.1、 @include指令

  @include可以包含任意的檔案,當然,只是把檔案的內容包含進來。

  include指令用於引入其它JSP頁面,如果使用include指令引入了其它JSP頁面,那麼JSP引擎將把這兩個JSP翻譯成一個servlet。所以include指令引入通常也稱之為靜態引入。

文法:<%@ include file="relativeURL"%>,其中的file屬性用於指定被引入檔案的路徑。路徑以“/”開頭,表示代表當前web應用。

include指令細節注意問題:

  1. 被引入的檔案必須遵循JSP文法。

  2. 被引入的檔案可以使用任意的副檔名,即使其副檔名是html,JSP引擎也會按照處理jsp頁面的方式處理它裡面的內容,為了見明知意,JSP規範建議使用.jspf(JSP fragments(片段))作為靜態引入檔案的副檔名。

  3. 由於使用include指令將會涉及到2個JSP頁面,並會把2個JSP翻譯成一個servlet,所以這2個JSP頁面的指令不能衝突(除了pageEncoding和導包除外)。

 include指令使用範例:

  建立head.jspf頁面和foot.jspf頁面,分別作為jsp頁面的頭部和尾部,存放於WebRoot下的jspfragments檔案夾中,代碼如下:

head.jspf代碼:

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <h1 style="color:red;">網頁頭部</h1>

foot.jspf代碼:

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <h1 style="color:blue;">網頁尾部</h1>

  在WebRoot檔案夾下建立一個IncludeTagTest.jsp頁面,在IncludeTagTest.jsp頁面中使用@include指令引入head.jspf頁面和foot.jspf頁面,代碼如下:

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <title>jsp的Include指令測試</title>
6 </head>
7
8 <body>
9 <%--使用include標籤引入引入其它JSP頁面--%>
10 <%@include file="/jspfragments/head.jspf" %>
11 <h1>網頁主體內容</h1>
12 <%@include file="/jspfragments/foot.jspf" %>
13 </body>
14 </html>

 

運行結果如下:

  

  我們查看一下jsp引擎將IncludeTagTest.jsp翻譯成IncludeTagTest_jsp類之後的原始碼,找到Tomcat伺服器的work\Catalina\localhost\JavaWeb_Jsp_Study_20140603\org\apache\jsp目錄下找到IncludeTagTest_jsp.java,如所示:

  

開啟IncludeTagTest_jsp.java,裡面的代碼如下所示:

package org.apache.jsp;

 

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

import java.util.*;

import java.util.*;

import java.util.*;

 

public final class IncludeTagTest_jsp extends org.apache.jasper.runtime.HttpJspBase

    implements org.apache.jasper.runtime.JspSourceDependent {

 

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

 

  private static java.util.List _jspx_dependants;

 

  static {

    _jspx_dependants = new java.util.ArrayList(2);

    _jspx_dependants.add("/jspfragments/head.jspf");

    _jspx_dependants.add("/jspfragments/foot.jspf");

  }

 

  private javax.el.ExpressionFactory _el_expressionfactory;

  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

 

  public Object getDependants() {

    return _jspx_dependants;

  }

 

  public void _jspInit() {

    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();

    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());

  }

 

  public void _jspDestroy() {

  }

 

  public void _jspService(HttpServletRequest request, HttpServletResponse response)

        throws java.io.IOException, ServletException {

 

    PageContext pageContext = null;

    HttpSession session = null;

    ServletContext application = null;

    ServletConfig config = null;

    JspWriter out = null;

    Object page = this;

    JspWriter _jspx_out = null;

    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\">\r\n");

      out.write("<html>\r\n");

      out.write("  <head>\r\n");

      out.write("  \r\n");

      out.write("    <title>jsp的Include指令測試</title>\r\n");

      out.write("  \r\n");

      out.write("  </head>\r\n");

      out.write("  \r\n");

      out.write("  <body>\r\n");

      out.write("    ");

      out.write("\r\n");

      out.write("<h1 style=\"color:red;\">網頁頭部</h1>\r\n");

      out.write("\r\n");

      out.write("    <h1>網頁主體內容</h1>\r\n");

      out.write("    ");

      out.write("\r\n");

      out.write("<h1 style=\"color:blue;\">網頁尾部</h1>\r\n");

      out.write("\r\n");

      out.write("  </body>\r\n");

      out.write("</html>\r\n");

    } catch (Throwable t) {

      if (!(t instanceof 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);

      }

    } finally {

      _jspxFactory.releasePageContext(_jspx_page_context);

    }

  }

}

  可以看到,head.jspf和foot.jspf頁面的內容都使用out.write輸出到瀏覽器顯示了。

3.2、總結@include指令

  使用@include可以包含任意的內容,檔案的尾碼是什麼都無所謂。這種把別的檔案內容包含到自身頁面的@include語句就叫作靜態包含,作用只是把別的頁面內容包含進來,屬於靜態包含。

3.3、jsp:include指令

  jsp:include指令為動態包含,如果被包含的頁面是JSP,則先處理之後再將結果包含,而如果包含的是非*.jsp檔案,則只是把檔案內容靜態包含進來,功能與@include類似。後面再具體介紹

JavaWeb---總結(十六)JSP指令

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.