什么是Java Server Pages?
JSP全称Java Server Pages,是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。
JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分。网页开发者们通过结合HTML代码、XHTML代码、XML元素以及嵌入JSP操作和命令来编写JSP
JSP通过网页表单获取用户输入数据、访问数据库及其他数据源,然后动态地创建网页。
JSP标签有多种功能,比如访问数据库、记录用户选择信息、访问JavaBeans组件等,还可以在不同的网页中传递控制信息和共享信息
JSP基于Java Servlets API,因此,JSP拥有各种强大的企业级Java API,包括JDBC,JNDI,EJB,JAXP等等
与纯 Servlets相比:JSP可以很方便的编写或者修改HTML网页而不用去面对大量的println语句
总的来说,JSP网页就是用另一种方式来编写servlet而不用成为Java编程高手。除了解释阶段外,JSP网页几乎可以被当成一个普通的servlet来对待
JSP 生命周期
理解JSP底层功能的关键就是去理解它们所遵守的生命周期
JSP生命周期就是从创建到销毁的整个过程,类似于servlet生命周期,区别在于JSP生命周期还包括将JSP文件编译成servlet
JSP 语法
<% 代码片段 %> 脚本程序
<%! declaration; [ declaration; ]+ … %> 声明一个或多个变量、方法
<%= 表达式 %> JSP表达式(应该是Java语言合法的表达式,最后被转化成一个字符串)
<%— 注释 —%> JSP注释,注释内容不会被发送至浏览器甚至不会被编译(程序猿可以看见)
JSP 的中文编码问题
<%@ page language=”java” contentType=”text/html; charset=UTF-8”
pageEncoding=”UTF-8”%>
JSP指令 (编译指令)
<%@ directive attribute=”value” %>
<%@ page … %> 定义页面的依赖属性,比如脚本语言、error页面、缓存需求等等
<%@ include … %> 包含其他文件
JSP可以通过include指令来包含其他文件。被包含的文件可以是JSP文件、HTML文件或文本文件。包含的文件就好像是该JSP文件的一部分,会被同时编译执行
先编译后运行
<%@ taglib … %> 引入标签库的定义,可以是自定义标签 最常用的功能
include指令:先包含编译,后运行
include行为:jsp:include 先运行,后包含 在编译出来的jsp中没有include的内容
JSP行为 (动作标签)10种
动作元素只有一种语法,它符合XML标准:
jsp:include动作相当于request.getRequestDispatcher(“你要跳转的页面”).forward(request, response)
与JSP指令元素不同的是,JSP动作元素在请求处理阶段起作用。
JSP行为标签使用XML语法结构来控制servlet引擎。它能够动态插入一个文件,重用JavaBean组件,引导用户去另一个页面,为Java插件产生相关的HTML等等
原来格式为表格(table),转换较复杂,未转换,需要手动复制一下
{"cells":[{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"jsp:include"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"用于在当前页面中包含静态或动态资源"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"jsp:useBean"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"寻找和初始化一个JavaBean组件"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"jsp:setProperty"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"设置 JavaBean组件的值"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"jsp:getProperty"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"将 JavaBean组件的值插入到 output中"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"jsp:forward"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"从一个JSP文件向另一个文件传递一个包含用户请求的request对象"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"jsp:plugin"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"用于在生成的HTML页面中包含Applet和JavaBean对象"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"jsp:element"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"动态创建一个XML元素"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"jsp:attribute"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"定义动态创建的XML元素的属性"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"jsp:body"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","fontName":"Open Sans","fontSize":"14px","value":"定义动态创建的XML元素的主体"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"jsp:text"},{"wrap":"true","verticalAlign":"top","textAlign":"start","textColor":"#333333","backColor":"rgb(246, 244, 240)","fontName":"Open Sans","fontSize":"14px","value":"用于封装模板数据"}],"widths":[365,365],"heights":[32,32,32,32,32,32,32,32,32,32]}
JSP隐含对象
JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。这样自动定义的变量一共有九个。
JSP API允许用户自定义标签,一个自定义标签库就是自定义标签的集合。
Taglib指令引入一个自定义标签集合的定义,包括库路径、自定义标签
request HttpServletRequest类的实例
response HttpServletResponse类的实例
out PrintWriter类的实例,用于把结果输出至网页上
session HttpSession类的实例
application ServletContext类的实例,与应用上下文有关
config ServletConfig类的实例
pageContext PageContext类的实例,提供对JSP页面所有对象以及命名空间的访问
page 类似于Java类中的this关键字
Exception Exception类的对象,代表发生错误的JSP页面中对应的异常对象
session对象用来跟踪在各个客户端请求间的会话。
page 对象就是this对象的同义词
context 语境、上下文
JSP 开发中推荐全部使用JSP行为完成,JSP行为使代码整齐,可读性高