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