程序员的知识教程库

网站首页 > 教程分享 正文

《JSP》第08节:JSP三大指令之Page指令(二)

henian88 2024-08-14 18:30:44 教程分享 22 ℃ 0 评论

JSP指令是指:用于设置JSP页面相关属性的一个语法命令,例如:设置页面编码字符集、导入其他包等等。JSP中提供了三个指令,分别是:page指令、include指令、taglib指令。其中page指令用于设置JSP页面属性,include指令用于引入其他的JSP文件,taglib指令用于引入标签库。这一小节内容介绍page指令的使用。

1.1、基础语法

JSP中page指令的语法规则如下所示:

<%@ page 属性名称="属性值" %>

注意:一个page指令中,可以有多个【属性名称=属性值】,也可以多次使用page指令

1.2、指令属性

page指令中,提供了很多个属性,常见的属性有这几个:contentType、pageEncoding、errorPage、isErrorPage、import、language、session、isELIgnored,下面我们就介绍每一个属性的作用。

这一小节介绍errorPage、isErrorPage、session、isELIgnored四个属性的作用。

(1)session

session属性作用:设置当前JSP页面中是否可以使用session对象,取值:true、false。默认是true,设置成false,那么当前JSP页面里面就不能使用session对象。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page session="true" %>

<html>
<head>
    <title>JSP指令之Page</title>
</head>
<body>
    <h3>JSP指令之Page</h3>
    <%
        // 使用session对象
        Object username = session.getAttribute("username");
        System.out.println(username);
    %>
</body>
</html>

如果设置session="fasle",那么在JSP页面中使用session,编译会报错,如下所示:

这是因为,当我们设置session="false"的时候,JSP编译之后,对应的java源代码中,都不会定义session变量,来看下session设置成true和false两种情况下,对应的源代码如下图所示:

(2)isELIgnored

isELIgnored属性作用:这个属性的作用是设置当前JSP页面是否忽略EL表达式,取值:true、false,默认值是true。

  1. false表示当前页面不忽略EL表达式,那么也就是说,当前JSP页面支持EL表达式;
  2. 设置成true之后,那么当前JSP页面中如果使用了EL表达式的内容,此时会被当作普通字符输出。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="true" %>

<html>
<head>
    <title>JSP指令之Page</title>
</head>
<body>
    <h3>JSP指令之Page</h3>
    使用EL表达式获取参数: ${"输出EL表达式内容"}
</body>
</html>

运行结果如下所示:

当我们设置成isELIgnored="false"的时候,再次访问jsp页面,此时结果如下所示:

(3)isErrorPage

isErrorPage属性作用:指定当前JSP页面是否作为错误处理界面,取值:true、false,默认值是false。设置成true之后,那么当其他的JSP页面发生报错的时候,通过errorPage属性,就会转发到这个错误页面。

注意:isErrorPage属性一般是和errorPage属性结合使用的

(4)errorPage

errorPage属性作用:指定当前JSP页面的错误页面地址,一般是和isErrorPage属性结合使用。errorPage设置的相对路径,源代码上就是转发到对应的错误页面。

  • 首先创建一个error.jsp页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定当前页面是错误页面 --%>
<%@ page isErrorPage="true" %>
<html>
<head>
    <title>JSP错误显示页面</title>
</head>
<body>
  <h3>
    Sorry,你访问的页面报错啦!!!
  </h3>
</body>
</html>
  • 接着再创建一个page.jsp页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 指定错误页面的路径地址 --%>
<%@ page errorPage="error.jsp" %>
<html>
<head>
    <title>errorPage属性</title>
</head>
<body>
    <%-- 模拟报错 --%>
    <%
        int i = 10 / 0;
    %>
</body>
</html>

启动Tomcat容器,浏览器访问http://localhost:8080/servlet/page.jsp,结果如下所示:

查看page.jsp编译之后对应的源代码,可以看到有一个handlePageException()方法,这个方法就是处理JSP页面异常的。

点进去查看源代码,如下所示:

private void doHandlePageException(Throwable t) throws IOException, ServletException {
    if (this.errorPageURL != null && !this.errorPageURL.equals("")) {
        this.request.setAttribute("javax.servlet.jsp.jspException", t);
        this.request.setAttribute("javax.servlet.error.status_code", 500);
        this.request.setAttribute("javax.servlet.error.request_uri", ((HttpServletRequest)this.request).getRequestURI());
        this.request.setAttribute("javax.servlet.error.servlet_name", this.config.getServletName());

        try {
            this.forward(this.errorPageURL);
        } catch (IllegalStateException var3) {
            this.include(this.errorPageURL);
        }

        Object newException = this.request.getAttribute("javax.servlet.error.exception");
        if (newException != null && newException == t) {
            this.request.removeAttribute("javax.servlet.error.exception");
        }

        this.request.removeAttribute("javax.servlet.error.status_code");
        this.request.removeAttribute("javax.servlet.error.request_uri");
        this.request.removeAttribute("javax.servlet.error.servlet_name");
        this.request.removeAttribute("javax.servlet.jsp.jspException");
    } else if (t instanceof IOException) {
        throw (IOException)t;
    } else if (t instanceof ServletException) {
        throw (ServletException)t;
    } else if (t instanceof RuntimeException) {
        throw (RuntimeException)t;
    } else {
        Throwable rootCause = null;
        if (t instanceof JspException || t instanceof ELException || t instanceof javax.servlet.jsp.el.ELException) {
            rootCause = t.getCause();
        }

        if (rootCause != null) {
            throw new ServletException(t.getClass().getName() + ": " + t.getMessage(), rootCause);
        } else {
            throw new ServletException(t);
        }
    }
}

从上面源代码就可以看到,有一个this.forward()方法,这个方法就是转发的作用。到此,page指令的常用属性都介绍完啦。

今天就到这里,未完待续~~

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表