JavaWeb学习心得之JSP指令
程序员文章站
2022-05-29 10:39:37
...
一、JSP指令概述
JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何输出,而只是告诉引擎如何处理JSP页面中的其余部分。
JSP定义了3个指令:page、include和taglib。
基本语法:<%@ 指令 属性名=“值” %>
举例:<% @ page contentType="text/html;charset=gb2312" %>
注意:如果有多个属性,可以写在一个指令中,也可以分开写。
二、Page指令
作用:page指令用于定义JSP页面的各种属性,它作用于整个JSP页面。
JSP规范中page指令的完整语法:
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true | false" ]
[ buffer="none | 8kb | sizekb" ]
[ autoFlush="true | false" ]
[ isThreadSafe="true | false" ]
[ info="text" ]
[ errorPage="relative_url" ]
[ isErrorPage="true | false" ]
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
[ pageEncoding="characterSet | ISO-8859-1" ]
[ isELIgnored="true | false" ]
%>
1.page指令的import属性
作用:导入类或包,多个类或包使用逗号分隔。
<%@ page import="java.util.*,java.io.*,java.sql.*"%>
作用:指明出错后跳转的页面。
注意:
- errorPage属性必须使用相对路径,如果以“/"开头,表示相对于当前web应用程序的根目录,否则表示相对当前页面
- 可以在web.xml文件中使用<error-page>元素为整个web应用程序设置错误处理页面。
- 如果JSP页面设置的errorPage属性,那么web.xml文件中设置的错误处理将不对该页面起作用。
补充:<error-page>包括三个子元素:<error-code>、<exception-type>、<location>
- <error-page>:指定错误状态码
- <exception-type>:指定异常类的完全限定名
- <location>:指定错误页面的路径
示例1:errorPage属性
测试页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
errorPage="/error.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%
//错误代码,一定抛出异常
int n = 1/0;
%>
</body>
</html>
异常页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>异常页面</title>
</head>
<body>
<p>系统异常,请联系管理员</p>
</body>
</html>
访问结果
示例2:<error-page>标签
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>JSPDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
访问结果:
效果同errorPage属性,如果省略<exception-type>则只匹配找不到路径的异常(404)
访问结果:
3.page指令的isErrorPage属性
作用:显示的声明为错误页面,可以在异常页面使用exception对象。
注意:如果显示声明异常页面,那么我们可以在页面中使用异常对象。如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isErrorPage="true"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>异常页面</title>
</head>
<body>
<p>系统异常,请联系管理员</p>
<p>异常信息:<%=exception.getMessage() %></p>
</body>
</html>
原因在于JSP翻译成Servlet的时候,_jspServlet方法中会声明一个exception对象,然后将异常信息存储到exception对象中。
三、include指令
作用:将被包含页面的代码复制到指页面中,就是包含另一个页面内容。其有@include指令和<jsp:include>指令
aaa@qq.com指令
含义:把任意文件的内容包含进来,如果使用include指令引入了其他JSP页面,那么JSP引擎将把两个JSP翻译成一个servlet,因此也称为静态引入。
语法:<%@include file="fileURL" %>,其中file属性指定被引入文件的路径,以"/"开头,表示当前web应用。
注意:
- 被引入的文件必须遵循JSP语法
- 被引入文件可以是任意扩展名,JSP规范使用.jspf作为静态文件引入的扩展名。
- 引入的两个JSP页面指令不能冲突。
示例
head.jspf代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h1 style="color:red;">网页头部</h1>
foot.jspf代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h1 style="color:blue;">网页尾部</h1>
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>test</title>
</head>
<body>
<%--使用include标签引入引入其它JSP页面--%>
<%@include file="/head.jspf" %>
<h1>网页主体内容</h1>
<%@include file="/foot.jspf" %>
</body>
</html>
查看翻译后的Servlet代码:
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.67
* Generated at: 2017-10-27 13:02:58 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.util.*;
public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
_jspx_dependants.put("/foot.jspf", Long.valueOf(1509109195293L));
_jspx_dependants.put("/head.jspf", Long.valueOf(1509109173575L));
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html lang=\"en\">\r\n");
out.write("<head>\r\n");
out.write(" <meta charset=\"utf-8\">\r\n");
out.write(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\">\r\n");
out.write(" <title>test</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t ");
out.write("\r\n");
out.write(" ");
out.write(" \r\n");
out.write("<h1 style=\"color:red;\">网页头部</h1>");
out.write("\r\n");
out.write(" <h1>网页主体内容</h1>\r\n");
out.write(" ");
out.write("\r\n");
out.write("<h1 style=\"color:blue;\">网页尾部</h1>");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
我们看到静态快:
static {
_jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(2);
_jspx_dependants.put("/foot.jspf", Long.valueOf(1509109195293L));
_jspx_dependants.put("/head.jspf", Long.valueOf(1509109173575L));
}
输出代码:
out.write("<h1 style=\"color:red;\">网页头部</h1>");
out.write("<h1 style=\"color:blue;\">网页尾部</h1>");
含义:如果包含的页面是JSP,则先处理之后再将结果包含,视为动态包含,若非jsp文件则只包含内容,与@include类似。