JavaWeb-Jsp指令

本文详细介绍了JSP指令的使用,包括设置页面属性、跳转至错误页面的方法。通过实例展示了如何配置Web.xml来处理不同类型的错误,并讨论了%@include与<jsp:include>的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值