您的位置:首页 > Web前端 > JavaScript

JSP 基础(二)

2015-11-08 23:02 796 查看
[b]五 注释 [/b]

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:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isThreadSafe="false" autoFlush="true"%>
<%@ page errorPage="error.jsp"  contentType="text/html; charset=utf-8" session="true" %>


示例2:

index.jsp;

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isThreadSafe="false" autoFlush="true"%>
<%@ page errorPage="error.jsp"  contentType="text/html; charset=utf-8" session="true" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
int num=2/0; //错误!

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
Index jsp
</body>
</html>


error.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 '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>
页面访问出错!
</body>
</html>


当访问index.jsp的时候就先显示:



因为设置了出错的页面(<%@ page errorPage="error.jsp" %> <%@ page isErrorPage="true"%>)

6.2 include指令用于把另一个文件包含在JSP中

语法格式:<% @ include file=" "%>

示例:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" autoFlush="true"%>
<%@ page errorPage="error.jsp"  contentType="text/html; charset=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>

<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>
<%@ include  file="header.jsp" %>

</body>
</html>

<!--header.jsp-->
<%@ page pageEncoding="utf-8" %>
<div style="height:100px;background-color: blue">header.jsp
</div>


访问页面出现:



include指令是一个静态的页面包含,是把被包含的文件拷贝到当前页面来一起编译。

6.3 taglib指令指定如何包含和访问自定义标签库

[b]七 JSP标准动作[/b]

7.1 <jsp:include>动作

语法格式:<jsp:include page="" flush=""/>

page:表示一个相对路径。可以是一个静态页面的问价名。也可以是一个动态的相对路径值。

flush:为真时,当缓冲区满时会自动清空。注意:这个属性是必须属性,而且值只能是true。

<jsp:include>动作实现的则是一种动态的包含,他是把显示的结果插入到当前的页面来显示。

示例:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" autoFlush="true"%>
<%@ page errorPage="error.jsp"  contentType="text/html; charset=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>

<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>
<jsp:include page="header.jsp" flush="true"/>
<jsp:include page="nav.html" flush="true"/>
<div style="height:200px;background-color: orange;">main.jsp</div>

</body>
</html>


<div style="height:100px; background-color:green ">导航栏</div>

访问页面出现的:



在nav.html出现了文字的乱码!

需要在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" id="WebApp_ID" version="3.0">
<display-name>JSP</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>
<!-- 设置静态页面的字符编码 -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.html</url-pattern>
<page-encoding>utf-8</page-encoding>
</jsp-property-group>

</jsp-config>
</web-app>


现在访问页面:



7.2 <jsp:forword>动作

语法格式:

<jsp:forword page="" />

[b]当程序运行到<[b]jsp:forword>语句时,控制权就交给了另一个JSP.(相当于转发)。[/b][/b]

[b][b]其与[b]<jsp:include page="" flush=""/> 的用法相同的。 [/b][/b][/b]

[b]八 JSP隐式对象[/b]

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提供了九个隐式对象一一介绍!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: