Jsp 6—— page指令

index.jsp(contentType,pageEncoding,import)

<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>
<%@page import="java.util.Date" %>
<%@page import="java.util.*,java.text.SimpleDateFormat" %>

<%--
    关于JSP的指令:
        1、指令的作用,是指导JSP的翻译引擎如何翻译JSP代码。
        2、JSP中共三个指令:
            * page            页面指令
            * include        包含指令
            * taglib        标签库指令【以后讲】
        3、指令的使用语法格式:
            <%@指令名  属性名=属性值  属性名=属性值.....%>
        3、关于JSP的page指令,page指令中常用的属性:
            * contentType        设置JSP的响应内容类型,同时在响应的内容类型后面也可以指定响应的字符编码方式
            * pageEncoding        设置JSP响应时的字符编码方式
            
            * import            组织导入
            
            * session            设置当前JSP页面中是否可以直接使用session内置对象
            
            * errorPage            错误页面
            * isErrorPage        是否是错误页面
            
            * isELIgnored        是否忽略EL表达式【后期讲】
--%>

<%
    Date nowTime = new Date();
%>

<%=nowTime %>
<br>
<%
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
%>
<%=sdf.format(nowTime) %>

 index2.jsp(session)

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

<%=session %>
 --%>

<%--
    关于page指令中的session属性:
        - session="true"
            * 表示在当前JSP中可以直接使用内置对象session
            * 程序执行的时候获取当前的session会话对象,若获取不到则新建session对象
            
        - session="false"
            * 表示在当前JSP中不能直接使用内置对象session
            * 但是有一些业务可能要求在当前JSP页面中获取当前的session对象,没有获取到则不新建session对象,此时需要编写以下程序
            
        - 若session这个属性没有指定,默认值就是session="true"
        
--%>
<%@page contentType="text/html; charset=UTF-8" session="false"%>
<%
    HttpSession session = request.getSession(false);
%>
<%=session%>

 index3.jsp

<%@page contentType="text/html; charset=UTF-8" errorPage="/error.jsp"%>

<%-- 
    关于page指令的errorPage属性:
        当前JSP页面处错之后,要跳转的页面路径,需要使用该属性指定。 
--%>
<%
    String s = null;
    s.toString();
%>

 

 error.jsp

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

<html>
    <head>
        <title>错误页面</title>
    </head>
    <body>
        <img src="./images/error.gif" />
    </body>
</html>

<%--
    关于page指令中的isErrorPage属性:
        - isErrorPage = "false" 表示内置对象exception无法使用【缺省情况下是false】
        - isErrorPage = "true"  表示内置对象exception可以使用
 --%>
<%-- 使用内置对象exception打印异常堆栈追踪信息 --%>
<%-- exception引用指向了抛出的异常 --%>
<%
    exception.printStackTrace();
%>

error.gif

转载请注明出处:https://www.cnblogs.com/stu-jyj3621
原文地址:https://www.cnblogs.com/stu-jyj3621/p/14379656.html