五 注释 

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指令指定如何包含和访问自定义标签库

七 JSP标准动作

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="" />      

当程序运行到<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提供了九个隐式对象一一介绍!

JSP 基础(二)的更多相关文章

  1. 新手学Html之JSP基础语法——入门(二)

    JSP基础语法 JSP注释 comment.jsp <%@ page language="java" contentType="text/html; charset ...

  2. Java学习-033-JavaWeb_002 -- 网页标记语言JSP基础知识

    JSP 是 Sun 公司提倡的一门网页技术标准.在 HTML 文件中,加入 Java 代码就构成了 JSP 网页,当 Web 服务器访问 JSP 请求的时候,首先执行其中的 Java 程序源码,然后以 ...

  3. JavaEE系列之(一)JSP基础知识详解

    一.JSP基础语法     1.JSP简介        JSP(Java Server Pages),其根本是一个简化的Servlet设计,它实现了在Java中使用HTML标签.JSP是一种动态网页 ...

  4. JSP基础使用

    一.JSP简介 JSP(Java Sever Pages):是为了能让 Java 在 Web 页面运行的一种语言. 在JSP中包括两种主要内容: 1. HTML.JS语言(静态内容).由客户端浏览器负 ...

  5. Jsp基础语法(由简入杂)

    JSP基础语法 一,JSP简介 Jsp是一个简化的Servlet设计,是在服务器端执行,他实现了再Java中使用HTML标签. Jsp是一种动态网页技术标准也是JAVAEE的标准 二,常见动态网站开发 ...

  6. JSP基础与提高(一).md

    JSP基础 JSP的由来 1.1. 为什么有JSP规范 Servlet技术产生以后,在使用过程中存在一个很大的问题,即为了表现页面的效果而需要输出大量的HTML标签,这些标签在Servlet中表现为一 ...

  7. Python全栈开发【基础二】

    Python全栈开发[基础二] 本节内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典) 其他(编码,range,f ...

  8. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  9. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

随机推荐

  1. Android SimpleAdapter ViewBinder

  2. android PendingIntent 使用通知传递多个参数,及不覆盖的问题

    Intent updateIntent = new Intent(GetNoticeService.this, DetailGonggaoActivity.class); updateIntent.p ...

  3. html5 实现qq聊天的气泡效果

    教程:http://m.blog.csdn.net/blog/yhc13429826359/38778337 写的很好.自己实现的时候,由于img float:left,会脱离文档流,导致结构混乱. ...

  4. bootstrap 图片切换

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  5. Oracle PLSQL读取(解析)Excel文档

    http://www.itpub.net/thread-1921612-1-1.html !!!https://code.google.com/p/plsql-utils/ Introduction介 ...

  6. mysql 返回结果按照指定的id顺序返回

    mysql> ,,,,) order by field(id,,,,,) limit ,; +----+-------+------+ | id | name | sex | +----+--- ...

  7. 【Win】Clso QR Tool 二维码小工具

    一个可以生成并识别二维码的windows小工具,纯绿色.不含糖. 可以通过输入文本生成二维码,或者加载本地图片.剪贴板内的图片,直接解析出二维码内容. 支持自定义LOGO. 下载文件 (当前版本:1. ...

  8. Git清空历史,清空历史删除的文件,降低.git 文件大小

    执行以下步骤之前 请做好源码备份 本操作用来清理github上面的历史删除文件,减少库的体积. 第一步骤 下载JDK环境和JAR包 https://rtyley.github.io/bfg-repo- ...

  9. MD5和SHA加密实现

    @Test public void TestMD5AndSHA() throws NoSuchAlgorithmException { String MD5=Md5("123456" ...

  10. Codeforces Round #479 (Div. 3)解题报告

    题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...