jsp三大指令


     include,page,taglib


1. include指令:

          作用:在当前页面用于包含其他页面。

         

 语法:<%@include file="common/header.jsp"%>


          

注意:

              1)原理是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp)中,合并翻译成一个java源文件,再编译运行。这种包含叫静态包含(源码包含)


不需要出现全局的html标签以及 不能出现重复的jsp脚本或者声明!!(如:html head title body等)


  1. ​<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>​
  2. ​<%​
  3. ​String path = request.getContextPath();​
  4. ​String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";​
  5. ​%>​
  6. ​<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">​
  7. ​<html>​
  8. ​<head>​
  9. ​ <base href="<%=basePath%>">​
  10. ​<title>My JSP 'index.jsp' starting page</title>​
  11. ​<metahttp-equiv="pragma"content="no-cache">​
  12. ​<metahttp-equiv="cache-control"content="no-cache">​
  13. ​<metahttp-equiv="expires"content="0">​
  14. ​<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">​
  15. ​<metahttp-equiv="description"content="This is my page">​
  16. ​<!--​
  17. ​ <link rel="stylesheet" type="text/css" href="styles.css">​
  18. ​ -->​
  19. ​</head>​
  20. ​<body>​
  21. ​<%--​
  22. ​ include指令:​
  23. ​作用:在当前页面用于包含其他页面。​
  24. ​语法:<%@include file="common/header.jsp"%>​
  25. ​ 注意:​
  26. ​ 1)原理是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp)中,合并翻译成一个java源文件,再编译运行。这种包含叫静态包含(源码包含)​
  27. ​ 2)如果使用静态包含,被包含页面中不需要出现全局的html标签了!!(如:html head title body等)​
  28. ​ --%>​
  29. ​<%@ include file="common/header.jsp"%>​
  30. ​ This is my JSP page. <br>​
  31. ​</body>​
  32. ​</html>​



   

jsp三大指令_page



  在使用include静态包含时,jsp出现了如下报错:Duplicate local variable path/basePath



jsp三大指令_jsp指令_02



 Demo如下:



  1. ​<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>​
  2. ​<%​
  3. ​String path = request.getContextPath();​
  4. ​String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";​
  5. ​%>​
  6. ​<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">​
  7. ​<html>​
  8. ​<head>​
  9. ​ <base href="<%=basePath%>">​
  10. ​<title>My JSP 'action.jsp' starting page</title>​
  11. ​<metahttp-equiv="pragma"content="no-cache">​
  12. ​<metahttp-equiv="cache-control"content="no-cache">​
  13. ​<metahttp-equiv="expires"content="0">​
  14. ​<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">​
  15. ​<metahttp-equiv="description"content="This is my page">​
  16. ​<!--​
  17. ​ <link rel="stylesheet" type="text/css" href="styles.css">​
  18. ​ -->​
  19. ​</head>​
  20. ​<body>​
  21. ​<%--静态包含--%>​
  22. ​<%@include file="../common/head.jsp" %>​
  23. ​</body>​
  24. ​</html>​



其中, head.jsp如下:



  1. ​<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>​
  2. ​<%​
  3. ​String path = request.getContextPath();​
  4. ​String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";​
  5. ​%>​
  6. ​<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">​
  7. ​<html>​
  8. ​<head>​
  9. ​ <base href="<%=basePath%>">​
  10. ​<title>My JSP 'index.jsp' starting page</title>​
  11. ​<metahttp-equiv="pragma"content="no-cache">​
  12. ​<metahttp-equiv="cache-control"content="no-cache">​
  13. ​<metahttp-equiv="expires"content="0">​
  14. ​<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">​
  15. ​<metahttp-equiv="description"content="This is my page">​
  16. ​<!--​
  17. ​ <link rel="stylesheet" type="text/css" href="styles.css">​
  18. ​ -->​
  19. ​</head>​
  20. ​<body>​
  21. ​ 这是一个通用的头部页面<br/>​
  22. ​</body>​
  23. ​</html>​



原因分析:



    include指令静态包含,是直接把被包含的页面(header.jsp)的内容翻译到包含页面(action.jsp)中,合并翻译成一个java源文件,再编译运行,即先合并再翻译。但是,2个jsp页面中都有path和basePath两个变量了,jsp被翻译成jsp源文件后,属于变量重复定义。删除被包含页面中重复的jsp脚本问题即可解决。也可以运用jsp标签jsp:include动态包含来解决,动态包含是先翻译再合并。



  1. ​<%​
  2. ​String path = request.getContextPath();​
  3. ​String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";​
  4. ​%>​



问题解决。



 

jsp三大指令_taglib_03



2. page指令



jsp三大指令_java_04



  作用: 告诉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三大指令_include_05


 效果如下:


jsp三大指令_page_06


jsp文件编码问题


         pageEncoding: 告诉Tomcat服务器使用什么编码格式翻译jsp文件(jsp -> java文件)


         contentType: tomcat服务器发送给浏览器的数据编码(Tomcat服务器 -> 浏览器)


 

jsp三大指令_page_07


 错误页面配置方式1:(一个页面一个页面配置)


errorPage="common/error.jsp"


Demo如下:


int i = 10 / 0 ;


<%@  page 
language="java"
import="java.text.*"
pageEncoding="utf-8"
errorPage="common/error.jsp"
isErrorPage="false"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'page.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>
<%
new SimpleDateFormat();
int i = 10/0;
%>
</body>
</html>


错误处理页面:

isErrorPage="true" 指定当前页面是否为错误处理页面。false,不是错误处理页面,则不能使用exception内置对象;true,是错误处理页面,可以使用exception内置对象。


<%@ 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 'error.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>
<h6>错误原因:<%=exception.getMessage() %></h6> <br>
</body>
</html>


错误页面配置方式2:( 配置全局的错误处理页面 )

<error-page>


 web.xml如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!--
全局错误处理页面配置
-->
<!-- 服务器端错误处理页面 -->
<error-page>
<error-code>500</error-code>
<location>/common/error500.jsp</location>
</error-page>
<!-- 客户器端错误处理页面 -->
<error-page>
<error-code>404</error-code>
<location>/common/error404.html</location>
</error-page>

</web-app>
3. taglib指令


3. taglib指令(自定义标签中使用,具体见我的相关博客笔记)