1. 程式人生 > >javaWeb學習日記_21:JSP指令

javaWeb學習日記_21:JSP指令

JSP有20個指令,但是一般的指令都沒有用,常用的有三個指令:page,include,taglib。

1. page

--> 最複雜:<%@page language="java" info="xxx"...%>
  (1) pageEncoding和contentType:

  •      pageEncoding:它指定當前jsp頁面的編碼,只要不說謊,就不會有亂碼!在伺服器要把jsp編譯成.java時需要使用pageEncoding!
  •     contentType:它表示新增一個響應頭:Content-Type!等同與response.setContentType("text/html;charset=utf-8");

    > 如果兩個屬性只提供一個,那麼另一個的預設值為設定那一個。
    > 如果兩個屬性都沒有設定,那麼預設為iso
  (2)import:導包!可以出現多次
  (3)errorPage和isErrorPage

  •      errorPage:當前頁面如果丟擲異常,那麼要轉發到哪一個頁面,由errorPage來指定
  •     isErrorPage:它指定當前頁面是否為處理錯誤的頁面!當該屬性為true時,這個頁面會設定狀態碼為500!而且這個頁面可以使用9大內建物件中的exception!

實現異常丟擲

錯誤程式碼丟擲a.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="errorPage.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
<%
	int n = 10 / 0;

	pageContext.setAttribute("aaa", "AAA");
	String xxx = (String)pageContext.getAttribute("aaa");
%>
  </body>
</html>

接收錯誤程式碼處理:errorPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'errorPage.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
<h1>哈哈~出錯了!</h1>

  </body>
</html>

 web.xml內配置錯誤芒果碼

<error-page>

      <error-code>404</error-code>
      <location>/error/errorPage.jsp</location>
      </error-page>
      <error-page>
        <error-code>500</error-code>
        <location>/error/errorPage.jsp</location>
      </error-page>
      <error-page>
        <exception-type>java.lang.RuntimeException</exception-type>
        <location>/index.jsp</location>
      </error-page>


   (4)autoFlush和buffer<瞭解>
     > autoFlush:指定jsp的輸出流緩衝區滿時,是否自動重新整理!預設為true,如果為false,那麼在緩衝區滿時丟擲異常!
     > buffer:指定緩衝區大小,預設為8kb,通常不需要修改!
   (5)isELIgnored(瞭解):是否忽略el表示式,預設值為false,不忽略,即支援!
   (5)基本沒用:
     > language:指定當前jsp編譯後的語言型別,預設值為java。
     > info:資訊!
     > isThreadSafe:當前的jsp是否支援併發訪問!
     > session:當前頁面是否支援session,如果為false,那麼當前頁面就沒有session這個內建物件!
     > extends:讓jsp生成的servlet去繼承該屬性指定的類!

2. include --> 靜態包含

  •   與RequestDispatcher的include()方法的功能相似!
  •   <%@include%> 它是在jsp編譯成java檔案時完成的!他們共同生成一個java(就是一個servlet)檔案,然後再生成一個class!
  •    RequestDispatcher的include()是一個方法,包含和被包含的是兩個servlet,即兩個.class!他們只是把響應的內容在執行時合併了!

程式碼演示:

hel.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hel.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
<%
	String name="zhangSan";
	String pagepath = "lo.jsp";
%>
<%@include file="lo.jsp" %>
  </body>
</html>

lo.jsp

<%
	out.print(name);
%>

在編譯前伺服器會將兩個檔案合拼生成一個java檔案而不是生成兩個java檔案

  作用:把頁面分解了,使用包含的方式組合在一起,這樣一個頁面中不變的部分,就是一個獨立jsp,而我們只需要處理變化的頁面。
3. taglib --> 匯入標籤庫
  * 兩個屬性:
    > prefix:指定標籤庫在本頁面中的字首!由我們自己來起名稱!
    > uri: 指定標籤庫的位置!
    > <%@taglib prefix="s" uri="/struts-tags"%> 字首的用法<s:text>