learning jsp for the 2nd time
第 I 条 基础知识
1) 什么是脚本语言
A high-level programming, or command, language that is interpreted (translated on the fly) rather than compiled ahead of time.
A scripting, or script, language may be a general-purpose programming language or it may be limited to specific functions used to augment the running of an application or system program. J
Scripting languages (commonly called scripting programming languages or script languages) are computer
programming languages that are typically
interpreted and can be typed directly from a keyboard. Thus,
scripts are often distinguished from programs, because programs are converted permanently into binary executable files (i.e., zeros and ones) before they are run. Scripts remain in their original form and are interpreted command-by-command each time they are run.
Scripts were created to shorten the traditional edit-compile-link-run process.
2) jsp文件在server中几种可能形式
l jsp源码
l jsp的等价servlet源码:jsp-àservlet
l 已编译的class文件:jspàservletà.class
3) jsp的工作过程
4) jsp到servlet的转换
jsp到servlet的转换基本有两个部分:
_jspService()和servlet的top层的成员(参见下面部分)。
表达式、scriptlet、模板数据被jsp容器用来创建_jspService()方法,该方法对应于servlet的service(),或更常用的doGet(),doPost()。
声明被创建为servlet的成员。
a) _jspService()
有三部分代码:
容器的初始化(如初始化在jsp中使用的隐含对象)和退出代码;
处理html模板数据和表达式的代码;
任意scriptlet内容。
模板数据:
注意转义:<% à </%
scriptlet:
不同的scriptlet被依次拼接在一起,因此可以将一段完整的java代码分解在不同的<% %>段中,只有保证拼接之后能形成完整的java代码。
初始化jsp的方法:
从外部文件读初始化数据:在web.xml中配置初始化数据;
override方法jspInit()。
jspInit()和jspDestory()
jsp似乎不让直接调用servlet的init()和destory(),因此使用上面两个方法。
第 II 条 将jsp分解为:元素和行为
jsp文件由jsp元素,模板数据(通常是html)组成。
Jsp从隐含的request对象获取请求,
然后使用脚本元素、伪指令、隐含对象设置要返回的响应头、进行业务逻辑的计算、构建返回文件(通常是html)的结构。
Servlet引擎完成从jsp到servlet的转换,并用response将返回文件传给client。
节 2.01 元素:伪指令,脚本元素,行为 + 隐含对象 +注释机制
(a) 伪指令
1) 基础
格式:
<%@ %>
四种:
page,include,taglib,自定义
伪指令可以理解为jsp的复用机制。(page不全是复用的范畴)
page定义jsp页面的属性,
include指定一个可以将其内容包含进来的外部文件,
taglib启用一个标准行为,
自定义伪指令启用一个自定义行为。
2) Page
3) Include
见下:jsp的复用机制
4) taglib
taglib是创建、使用、修改对象的高层jsp元素,使用xml语法,格式:
<tagname [attr=”value” ..]>..</tagname>
<jsp:useBean>,<jsp:setProperty>,<jsp:getProperty>,<jsp:include>,<jsp:forward>,<jsp:param>,<jsp:plugin>,自定义
(b) 脚本元素
(i) 表达式
格式:
<%= exp %>
表达式到java代码的转换:
the expression is <%= new java.util.Date() %>
转换为:
out.write(“the expression is”);
out.write( new java.util.Date() );
out.write(“/r/n”);
因此,可以看到<%= new java.util.Date() %>是不能在表达式末尾加“;”的。
(ii) scriptlet
jsp的重要部分。格式:
<% statement; statement; ...%>
scriptlet到java代码的转换:
scriptlet被简单添加到servlet的_jspService()方法中。
因此会有下面这种情况:
<%
for(int i=0; i<10; i++){
%>
this is in a for loop!
<%
}
%>
转换为(注意大括号的转换):
for(int i=0; i<10; i++){
out.write(“/r/nthis is in a for loop!”);
}
(iii) 声明
格式:
<%! Statement; statement; ... %>
声明到java代码的转换:
声明被加为servlet的成员。
例如:
<%!
Public void justATest(){
}
%>
被转换到对应的servlet的top层:
public class Xxxx{
..
public void justATest(){
}
..
}
用法:
变量声明;
方法定义;
内部类:当在servlet中还需要一个类的时候,用<%! %>来定义内部类。
(c) 隐含对象
jsp对应的servlet中包含了几个隐含对象,可以直接在jsp中使用。
request
response
pageContext
session
application 在_jspService()中初始化。
out
page
conf
exception
pageContext:
该对象跟踪4个层面上的属性,jsp页面,http请求,http会话,整个应用。
session:
初始化时,新建或访问当前激活的HttpSession对象。
application:
page:
当前servlet接口引用的变量,基本上是this的别名。但此对象典型情况下对jsp页面写入者不可用。
exception:
引用jsp页面中catch块已经溢出但未被捕获的java.lang.Throwable实例。
(d) 注释机制:jsp和html
两种:
jsp的<%-- -- %>
html/xml的<!-- -->
对于<%-- --%>:jsp编译的时候跳过,该段代码被抛弃。
对于<!-- -->:jsp编译的时候,跟其他html代码一样,只读不处理。
因此看如下的用法:
<!—
this an annotation which can be seen in the final file.
And java code here can still be run <%= request.getAttribute(“some attribute”)%>
-->
这段代码由html和java两部分组成,jsp编译时只读不处理html部分,处理java部分。所以经编译之后得到:
<!—
this an annotation which can be seen in the final file.
And java code here can still be run some value
-->
这段代码进入最后返回给客户端的html文件中。
启示:
jsp注释给jsp开发人员看。
Html注释给用户看。
节 2.02 jsp的处理及流程分析
(a) client的请求过程
i) servlet引擎的分类:
in-process模式:servlet引擎是web server的一部分。
out-of-process模式:web server没有servlet引擎,而是通过一个connector解释对servlet的请求并采用与实现(从server到servlet engine)相关的协议,将请求转发到servlet engine。其他请求还是有web server完成。Apache的htttp服务器就是这种模式,它通过connecter连接到tomcat工作。
ii) Servlet引擎对请求的处理过程:
请求的信息àHttpServletRequest对象à初始化HttpServletResponse对象待使用à调用service()或_jspService(),以前两个对象为参数。
iii) Jsp中分离事务与数据的机制:<jsp:forward>
在一个jsp中进行事务处理,然后将结果转发到另一个jsp页面中显示。
iv) Jsp的复用机制:简单复用<%@ include file=””%>和<jsp:include page=”” flush=”true”>
前者是预处理的,后者是call的。
前者是
n 静态包含,
n 是伪指令,
n 可以包含各种文件,甚至是定义java方法的<%! %>。
n 注意:规范不要求在包含文件改变的情况下,必须检查改变,但是tomcat等应该是检查的,
n 在编译时完成包含;
后者是
n 动态的,
n 是行为,
n flush似乎在规范一直是true,即在包含资源前刷新JspWriter out,
n 包含对象可以是文件或动态资源,如jsp、servlet,
n <jsp:include>调用的jsp页面可以访问其调用者的隐含对象,但是不能设置调用者的Response对象的响应头,因为<jsp:include>先被刷新,再处理资源包含的问题,所以此时的响应头已经被设置
n 可以向被包含的jsp传递参数
n 被包含的jsp的request uri的base跟调用者一样了。
v) <jsp:forward>和<jsp:include>到java的转换
利用javax.servlet.RequestDispatcher实现。
节 2.03 行为:与其他组件协同工作
(a) 与数据库
(b) 从无状态到有状态:会话
http是无状态协议,所以要采用方法保存状态
n server端:保持所有数据,并给出一个标识,client记住该标识
n client端:保持数据,并在需要时发回server。
u 更有效
u 常用技术:
¡ cookies
¡ url重写
¡ 隐藏域
¡ servlet提供的http会话api
(c) 线程模型
Ø 三种线程创建方法:
n Thread类
n Runnable接口
n Time和TimeTask类
Ø 线程模型:
n 默认的单一实例多线程模型:推荐
n 可选的单线程模型:几乎没有优势
(d) 与javabean
(e) 与xml
应该是跟java处理xml一样。
(f) 与其他web组件
Ø 与其他客户端通信
n 浏览器不是唯一的jsp客户端。可以使用http协议的任何程序都可以做jsp客户端。
(g) 其他问题
(i) 测试与调试
(ii) 发布应用
分享到:
相关推荐
Machine Learning for Algorithmic Trading, 2nd edition
Deep Learning for Time Series Forecasting Predict the Future with MLPs, CNNs and LSTMs in Python Jason Brownlee 5 25 step-by-step lessons, 575 pages. 深度学习方法为时间系列预测提供了许多希望,例如...
the current state of the art performance of deep learning algorithms for TSC by presenting an empirical study of the most recent DNN architectures for TSC. We give an overview of the most successful...
Scala for Machine Learning(2nd)_Code 源码 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Machine Learning for the Web Publisher: Packt Publishing - ebooks Account (July 29, 2016) 代码 https://github.com/PacktPublishing/Machine-Learning-for-the-Web Features Targets two big and prominent ...
Deep Learning for the Classification Deep Learning for the Classification Deep Learning for the Classification
Learning the bash Shell 2nd Ed 1998.chm
Code for Machine Learning for Algorithmic Trading, 2nd edition
Deep Reinforcement Learning for Sponsored Search Real-time Bidding,一篇强化学习方面的论文
Machine Learning for the Web Explore the web and make smarter predictions using Python
Machine Learning for the Web 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Learning Cython Programming(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Richard Sutton’s & Andrew Barto’s Reinforcement Learning: An Introduction (2nd Edition) book.
While we may briefly delve into some theoretical aspects of machine learning algorithms and required maths for machine learning, the book will generally take a practical, applied approach with a ...
Machine Learning for the Quantified Self On the Art of Learning from Sensory Data 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者...
Learning Ceph(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Learning Cython Programming(2nd) 英文azw3 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Deep-Time-Series-Forecasting-with-Python-An-Intuitive-Introduction-to-Deep-Learning-for-Applied-Time-Series-Modeling.pdf
for the Quantified Self On the Art of Learning from Sensory Data, Sensors are all around us, and increasingly on us. We carry smartphones and watches, which have the potential to gather enormous ...
Machine Learning for the Web 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除