JSP 基础(二)
五 注释
5.1 JSP注释 <%--注释内容--%>
5.2 HTML注释 <!--注释内容-->
5.3 Java注释
六 JSP指令
在JSP中有三种类型的指令
6.1 page指令为当前页面提供处理命令
语法格式:<%@ page %>
page指令属性
| 
 属性名  | 
 值  | 
 默认值  | 
| 
 language  | 
 脚本语言名称  | 
 "java"  | 
| 
 Info  | 
 网页信息  | 
 无  | 
| 
 contentType  | 
 MIME类型和JSP编码  | 
 "text/html;charset=ISO-8859-1"  | 
| 
 import  | 
 类和包  | 
 none  | 
| 
 buffer  | 
 缓冲区大小  | 
 8192  | 
| 
 autoFlush  | 
 缓冲满,刷新还是抛出异常  | 
 "true"  | 
| 
 session  | 
 访问页面是否创建会话  | 
 "true"  | 
| 
 isThreadSafe  | 
 线程是否安全  | 
 "true"  | 
| 
 errorPage  | 
 URL  | 
 none  | 
| 
 isErrorPage  | 
 布尔值  | 
 "false"  | 
示例1:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" isThreadSafe="false" autoFlush="true"%> 2 <%@ page errorPage="error.jsp" contentType="text/html; charset=utf-8" session="true" %>
示例2:
index.jsp;
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" isThreadSafe="false" autoFlush="true"%> 2 <%@ page errorPage="error.jsp" contentType="text/html; charset=utf-8" session="true" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 int num=2/0; //错误! 7 8 %> 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 <title>My JSP 'index.jsp' starting page</title> 14 15 <meta http-equiv="pragma" content="no-cache"> 16 <meta http-equiv="cache-control" content="no-cache"> 17 <meta http-equiv="expires" content="0"> 18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 19 <meta http-equiv="description" content="This is my page"> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 24 </head> 25 <body> 26 Index jsp 27 </body> 28 </html>
error.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" isErrorPage="true"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'error.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 页面访问出错! 27 </body> 28 </html>
当访问index.jsp的时候就先显示:

因为设置了出错的页面(<%@ page errorPage="error.jsp" %> <%@ page isErrorPage="true"%>)
6.2 include指令用于把另一个文件包含在JSP中
语法格式:<% @ include file=" "%>
示例:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" autoFlush="true"%> 2 <%@ page errorPage="error.jsp" contentType="text/html; charset=utf-8" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 7 %> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title>My JSP 'index.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 <body> 25 <%@ include file="header.jsp" %> 26 27 </body> 28 </html> 29 30 <!--header.jsp--> 31 <%@ page pageEncoding="utf-8" %> 32 <div style="height:100px;background-color: blue">header.jsp 33 </div>
访问页面出现:

include指令是一个静态的页面包含,是把被包含的文件拷贝到当前页面来一起编译。
6.3 taglib指令指定如何包含和访问自定义标签库
七 JSP标准动作
7.1 <jsp:include>动作
语法格式:<jsp:include page="" flush=""/>
page:表示一个相对路径。可以是一个静态页面的问价名。也可以是一个动态的相对路径值。
flush:为真时,当缓冲区满时会自动清空。注意:这个属性是必须属性,而且值只能是true。
<jsp:include>动作实现的则是一种动态的包含,他是把显示的结果插入到当前的页面来显示。
示例:
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" autoFlush="true"%> 2 <%@ page errorPage="error.jsp" contentType="text/html; charset=utf-8" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 7 %> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title>My JSP 'index.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 <body> 25 <jsp:include page="header.jsp" flush="true"/> 26 <jsp:include page="nav.html" flush="true"/> 27 <div style="height:200px;background-color: orange;">main.jsp</div> 28 29 30 31 </body> 32 </html>
1 <div style="height:100px; background-color:green ">导航栏</div>
访问页面出现的:

在nav.html出现了文字的乱码!
需要在web.xml设置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" id="WebApp_ID" version="3.0"> 3 <display-name>JSP</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- 设置静态页面的字符编码 --> 13 <jsp-config> 14 <jsp-property-group> 15 <url-pattern>*.html</url-pattern> 16 <page-encoding>utf-8</page-encoding> 17 </jsp-property-group> 18 19 </jsp-config> 20 </web-app>
现在访问页面:

7.2 <jsp:forword>动作
语法格式:
<jsp:forword page="" />
当程序运行到<jsp:forword>语句时,控制权就交给了另一个JSP.(相当于转发)。
其与<jsp:include page="" flush=""/> 的用法相同的。
八 JSP隐式对象
JSP提供了九个隐式对象
| 
 对象名  | 
 描述  | 
 作用域  | 
| 
 request  | 
 代表与请求相关的HttpServletRequest对象  | 
 request  | 
| 
 response  | 
 代表与响应相关的HttpServletResponse对象  | 
 page  | 
| 
 pageContext  | 
 代表封装请求某个JSP页面时请求环境的pageContext对象  | 
 page  | 
| 
 session  | 
 代表特定用户请求会话的HttpSession对象。该对象只有在JSP页面参与一个HTTP会话时才有意义  | 
 session  | 
| 
 application  | 
 代表Web应用程序的ServletContext对象  | 
 application  | 
| 
 out  | 
 代表与响应输出流相关的JspWriter对象  | 
 page  | 
| 
 config  | 
 代表JSP 页面的Servlet相关的ServletConfig对象  | 
 page  | 
| 
 page  | 
 等于Java编程语言中的this变量  | 
 page  | 
| 
 exception  | 
 代表JSP页面抛出的Trowable对象。这个对象只能在JSP错误页面中使用  | 
 page  | 
下一章节对JSP提供了九个隐式对象一一介绍!
                    
                
                
            
        
浙公网安备 33010602011771号