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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章