jsp三大指令
include,page,taglib
1. include指令:
作用:在当前页面用于包含其他页面。
语法:<%@include file="common/header.jsp"%>
注意:
1)原理是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp)中,合并翻译成一个java源文件,再编译运行。这种包含叫静态包含(源码包含)
不需要出现全局的html标签以及 不能出现重复的jsp脚本或者声明!!(如:html head title body等)
-
<%@ 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 'index.jsp' starting page</title>
-
<metahttp-equiv="pragma"content="no-cache">
-
<metahttp-equiv="cache-control"content="no-cache">
-
<metahttp-equiv="expires"content="0">
-
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
-
<metahttp-equiv="description"content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
<body>
-
<%--
-
include指令:
-
作用:在当前页面用于包含其他页面。
-
语法:<%@include file="common/header.jsp"%>
-
注意:
-
1)原理是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp)中,合并翻译成一个java源文件,再编译运行。这种包含叫静态包含(源码包含)
-
2)如果使用静态包含,被包含页面中不需要出现全局的html标签了!!(如:html head title body等)
-
--%>
-
<%@ include file="common/header.jsp"%>
-
This is my JSP page. <br>
-
</body>
-
</html>
在使用include静态包含时,jsp出现了如下报错:Duplicate local variable path/basePath
Demo如下:
-
<%@ 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 'action.jsp' starting page</title>
-
<metahttp-equiv="pragma"content="no-cache">
-
<metahttp-equiv="cache-control"content="no-cache">
-
<metahttp-equiv="expires"content="0">
-
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
-
<metahttp-equiv="description"content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
<body>
-
<%--静态包含--%>
-
<%@include file="../common/head.jsp" %>
-
</body>
-
</html>
其中, head.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 'index.jsp' starting page</title>
-
<metahttp-equiv="pragma"content="no-cache">
-
<metahttp-equiv="cache-control"content="no-cache">
-
<metahttp-equiv="expires"content="0">
-
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
-
<metahttp-equiv="description"content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
<body>
-
这是一个通用的头部页面<br/>
-
</body>
-
</html>
原因分析:
include指令静态包含,是直接把被包含的页面(header.jsp)的内容翻译到包含页面(action.jsp)中,合并翻译成一个java源文件,再编译运行,即先合并再翻译。但是,2个jsp页面中都有path和basePath两个变量了,jsp被翻译成jsp源文件后,属于变量重复定义。删除被包含页面中重复的jsp脚本问题即可解决。也可以运用jsp标签jsp:include动态包含来解决,动态包含是先翻译再合并。
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
问题解决。
2. page指令
作用: 告诉tomcat服务器如何翻译jsp文件
1. language="java" --告诉服务器使用什么动态语言来翻译jsp文件
2. import="java.util.*" --告诉服务器java文件使用什么包,导入包,多个包之间用逗号分割
3. pageEncoding="utf-8" --告诉服务器使用什么编码翻译jsp文件(成java文件)
4. contentType="text/html; charset=utf-8" --服务器发送浏览器的数据类型和内容编码
5. 注意:在开发工具中,以后只需要设置pageEncoding即可解决中文乱码问题
6. errorPage="error.jsp" --指定当前jsp页面的错误处理页面
7. isErrorPage="false" --制定当前页面是否为错误处理页面。false,不是错误处理页面,则不能使用exception内置对象;true,是错误处理页面,可以使用exception内置对象。
8. buffer="8kb" --jsp页面的缓冲区大小。
9. isELIgnored="false"
10. session="true" --是否开启session功能。false,不能使用session内置对象;true,可以使用session内置对象。
效果如下:
jsp文件编码问题
pageEncoding: 告诉Tomcat服务器使用什么编码格式翻译jsp文件(jsp -> java文件)
contentType: tomcat服务器发送给浏览器的数据编码(Tomcat服务器 -> 浏览器)
错误页面配置方式1:(一个页面一个页面配置)
errorPage="common/error.jsp"
Demo如下:
int i = 10 / 0 ;
错误处理页面:
isErrorPage="true" 指定当前页面是否为错误处理页面。false,不是错误处理页面,则不能使用exception内置对象;true,是错误处理页面,可以使用exception内置对象。
错误页面配置方式2:( 配置全局的错误处理页面 )
<error-page>
web.xml如下: