JavaWeb-Jsp指令

4.Jsp指令

JSP指令用來設置整個JSP頁面相關的屬性,如網頁的編碼方式和腳本語言。

<%@ page args…%>

跳轉自定義的錯誤頁面。

<%@ page errorPage="error/500.jsp" %>

出現錯誤跳轉404頁面。

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.8
  Time: 下午 5:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--自定義錯誤的頁面--%>
<%@ page errorPage="error/500.jsp" %>
<html>
<head>
    <title>Jsp2</title>
</head>
<body>
<%
    int i = 1/0;<%--邏輯性錯誤--%>
%>
</body>
</html>

輸出:

在這裏插入圖片描述

多錯誤頁面跳轉:

Jsp2.jsp頁面:

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.8
  Time: 下午 5:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--自定義錯誤的頁面--%>
<%--<%@ page errorPage="error/500.jsp" %>--%>
<html>
<head>
    <title>Jsp2</title>
</head>
<body>
<%
    int i = 1/0;
%>
</body>
</html>

Web.xml頁面:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <error-page>
        <error-code>404</error-code><--指明錯誤處理爲404類型的-->
        <location>/error/404.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code><--指明錯誤處理爲500類型的-->
        <location>/error/500.jsp</location>
    </error-page>
</web-app>

效果:

直接訪問Jsp2.jsp

在這裏插入圖片描述

跳轉到一個根本不存在的頁面:

在這裏插入圖片描述

<%@ include file=""%>

創建頭頁面和尾頁面:

common/Header.jsp;Footer.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.8
  Time: 下午 9:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Header</h1>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Footer</h1>

Jsp3.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 元
  Date: 2020.6.8
  Time: 下午 9:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Jsp3</title>
</head>
<body>
    <%--<%@include%>會將多個頁面合成一個,以一個單獨的頁面展示出來--%>
    <%@include file="/common/header.jsp"%>
    <h1>網頁主體……</h1>
    <%@include file="/common/footer.jsp"%>

    <hr>

    <%--Jsp標籤
        <jsp:include>會拼接多個頁面,但是此例子中還是3個頁面。
        靈活性相對較強。
    --%>
    <jsp:include page="common/header.jsp"/>
    <h1>還是網頁主體……</h1>
    <jsp:include page="common/footer.jsp"/>

</body>
</html>

輸出效果:

在這裏插入圖片描述

格式小結:

<%@ page args...%>
<%@ include file=""%>
<%--<%@include%>會將多個頁面合成一個,以一個單獨的頁面展示出來--%>
<%@include file="/common/header.jsp"%>
<h1>網頁主體……</h1>
<%@include file="/common/footer.jsp"%>

<hr>

<%--Jsp標籤
    <jsp:include>會拼接多個頁面,但是此例子中還是3個頁面。
        靈活性相對較強。
        --%>
<jsp:include page="common/header.jsp"/>
<h1>還是網頁主體……</h1>
<jsp:include page="common/footer.jsp"/>

《成功的花》——冰心
成功的花,
人們只驚羨她現時的明豔!
然而當初她的芽兒,
浸透了奮鬥的淚泉,
灑遍了犧牲的血雨!

參考文獻

《【狂神說Java】JavaWeb入門到實戰》

視頻連接

2020.06.08

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