JSP有20个指令,但是一般的指令都没有用,常用的有三个指令:page,include,taglib。
1. page
--> 最复杂:<%@page language="java" info="xxx"...%>
(1) pageEncoding和contentType:
- pageEncoding:它指定当前jsp页面的编码,只要不说谎,就不会有乱码!在服务器要把jsp编译成.java时需要使用pageEncoding!
- contentType:它表示添加一个响应头:Content-Type!等同与response.setContentType("text/html;charset=utf-8");
> 如果两个属性只提供一个,那么另一个的默认值为设置那一个。
> 如果两个属性都没有设置,那么默认为iso
(2)import:导包!可以出现多次
(3)errorPage和isErrorPage
- errorPage:当前页面如果抛出异常,那么要转发到哪一个页面,由errorPage来指定
- isErrorPage:它指定当前页面是否为处理错误的页面!当该属性为true时,这个页面会设置状态码为500!而且这个页面可以使用9大内置对象中的exception!
实现异常抛出
错误代码抛出a.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="errorPage.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'a.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
int n = 10 / 0;
pageContext.setAttribute("aaa", "AAA");
String xxx = (String)pageContext.getAttribute("aaa");
%>
</body>
</html>
接收错误代码处理:errorPage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'errorPage.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>哈哈~出错了!</h1>
</body>
</html>
web.xml内配置错误芒果码
<error-page>
<error-code>404</error-code>
<location>/error/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/errorPage.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/index.jsp</location>
</error-page>
(4)autoFlush和buffer<了解>
> autoFlush:指定jsp的输出流缓冲区满时,是否自动刷新!默认为true,如果为false,那么在缓冲区满时抛出异常!
> buffer:指定缓冲区大小,默认为8kb,通常不需要修改!
(5)isELIgnored(了解):是否忽略el表达式,默认值为false,不忽略,即支持!
(5)基本没用:
> language:指定当前jsp编译后的语言类型,默认值为java。
> info:信息!
> isThreadSafe:当前的jsp是否支持并发访问!
> session:当前页面是否支持session,如果为false,那么当前页面就没有session这个内置对象!
> extends:让jsp生成的servlet去继承该属性指定的类!
2. include --> 静态包含
- 与RequestDispatcher的include()方法的功能相似!
- <%@include%> 它是在jsp编译成java文件时完成的!他们共同生成一个java(就是一个servlet)文件,然后再生成一个class!
- RequestDispatcher的include()是一个方法,包含和被包含的是两个servlet,即两个.class!他们只是把响应的内容在运行时合并了!
代码演示:
hel.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hel.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String name="zhangSan";
String pagepath = "lo.jsp";
%>
<%@include file="lo.jsp" %>
</body>
</html>
lo.jsp
<%
out.print(name);
%>
在编译前服务器会将两个文件合拼生成一个java文件而不是生成两个java文件
作用:把页面分解了,使用包含的方式组合在一起,这样一个页面中不变的部分,就是一个独立jsp,而我们只需要处理变化的页面。
3. taglib --> 导入标签库
* 两个属性:
> prefix:指定标签库在本页面中的前缀!由我们自己来起名称!
> uri: 指定标签库的位置!
> <%@taglib prefix="s" uri="/struts-tags"%> 前缀的用法<s:text>