解读JSP的解析过程
解读JSP的解析过程
互联网上,这方面的资料实在太少了,故把自己研究的一些结果公布出来。
首先,问大家几个问题,看大家能不能回答出来,或者在网上能不能找到答案:
1、page、include、taglib这三个编译指令,执行的顺序是什么?
2、JSP文件中的Java代码、编译指令、动作指令、EL标签、第三方标签、静态文本等,被编译的顺序是什么?
3、常用的、与JSP解析/编译相关的类有哪些?换句话说,JSP解析、编译技术是建立在哪些接口和工具之上的?
4、JSP技术所有的编译指令和动作指令有哪些?
5、JSP技术是由谁发起的,现在有哪几个标准?都有哪些服务器或者项目支持JSP的解析和编译?
先回答简单、基础性的问题:
回答问题5:
JSP的发起者为——Sun Microsystems, Inc.
JSP是由Java Servlets发展而来。
JSP和Servlet一样,都是在Java Community Process(Java社区组织)等众多人参与下,共同开发出来的,并且最终制定了一些规范和标准。Java体系中的规范和技术,会被制定成JSR规范。JSP和Servlet也属于这个规范之中,例如JSR-53规定了JSP 1.2和Servlet 2.4的规范,JSR-152规定了JSP 2.0的规范。(详见http://zh.wikipedia.org/wiki/JSR)
JSP常用的版本有1.2、2.0和2.1(最新版本),其中1.2版本最大的进步在于优化了JSTL(JavaServer Pages Standard Tag Library),已经过时了(因为它不支持EL表达式),2.0在1.2的基础上有了巨大的进步,因为它引入了Expression Language(EL表达式)、简化了tag标签的扩展、增强了XML的语法,以及其他一些重要改进,我是翻译过来的,官方原文如下:
The JSP 2.0 specification (JSR-152) substantially extended the technology by integrating a simple yet powerful expression language, simplifying the tag extension API, and enhancing the pure XML syntax,
among other important enhancements. These enhancements greatly reduced the learning curve of the technology, warranting a major version number upgrade.
而JSP 2.1是在JSP 2.0的基础上,为JSF而生的,简而言之,JSP 2.1加强了EL,更能够适应J2EE、J2SE、Servlet API的发展。
JSP 2.1 to enhance the expression language to meet the needs of JSF technology. Many of these enhancements are likely to be useful in other contexts as well.
Enhancements to be considered for the JSP 2.1 expression language include, but are not limited to, the following:
- moving the expression language chapter into its own specification document, to be delivered under this JSR with consultation from the JavaServer Faces expert group
- ability to redefine the behavior of the "." operator through a Property Resolver API
- ability to plug in Variable Resolvers on a per-application and per-page basis
- ability to plug in Property Resolvers on a per-application and per-page basis
- ability to express references to bean methods using the expression language and invoking those methods via a Method Binding API
- ability to express references to bean properties using the expression language and getting/setting those attributes via a Property Binding API
- ability to defer expression evaluation until a time of a tag handler's choosing
目前支持JSP的引擎有很多,NB的人自己都可以写一套出来,最常见的WebLogic、Tomcat、WebSphere、Resin(据说效率比Tomcat等要高,网易等大网站都在用)。
回答问题3:
第一个想到的就是Java Servlet API,即servlet-api.jar ,然后JSP的解析有一个工具包,即jasper-compiler.jar,因为JSP是一个标准,那么在解析工具包之上,应该有一个规范的API包,即jsp-api.jar,JSTL是JSP规范的一部分,故还有jstl-api.jar,另外,还有expression language扩展包,el-api.jar。还注意到一点,JSP是基于XML的,故也涉及到XML的解析(用的是SAX)。
补充一点,可以手动调用程序来解析(也叫预编译)JSP,在Tomcat或WebSphere下,是调用JspC这个工具类:
Tomcat下,jsp是通过org.apache.jasper.JspC编译工具将JSP 页面的预编译,在WAS下,是通过 com.ibm.websphere.ant.tasks.JspC进行预编译。
回答问题4和1:
JSP技术所有的编译指令和动作指令,可以参看官方API文档,我这里找到的如下:
ATTRIBUTE_ACTION"attribute"
ATTRIBUTE_DIRECTIVE_ACTION"directive.attribute"
BODY_ACTION"body"
DECLARATION_ACTION"declaration"
DIRECTIVE_ACTION"directive."
DOBODY_ACTION"doBody"
ELEMENT_ACTION"element"
EXPRESSION_ACTION"expression"
FALLBACK_ACTION"fallback"
FORWARD_ACTION"forward"
GET_PROPERTY_ACTION"getProperty"
INCLUDE_ACTION"include"
INCLUDE_DIRECTIVE_ACTION"directive.include"
INVOKE_ACTION"invoke"
JSP_ATTRIBUTE_ACTION"jsp:attribute"
JSP_ATTRIBUTE_DIRECTIVE_ACTION"jsp:directive.attribute"
JSP_BODY_ACTION"jsp:body"
JSP_DECLARATION_ACTION"jsp:declaration"
JSP_DOBODY_ACTION"jsp:doBody"
JSP_ELEMENT_ACTION"jsp:element"
JSP_EXPRESSION_ACTION"jsp:expression"
JSP_FALLBACK_ACTION"jsp:fallback"
JSP_FORWARD_ACTION"jsp:forward"
JSP_GET_PROPERTY_ACTION"jsp:getProperty"
JSP_INCLUDE_ACTION"jsp:include"
JSP_INCLUDE_DIRECTIVE_ACTION"jsp:directive.include"
JSP_INVOKE_ACTION"jsp:invoke"
JSP_OUTPUT_ACTION"jsp:output"
JSP_PAGE_DIRECTIVE_ACTION"jsp:directive.page"
JSP_PARAM_ACTION"jsp:param"
JSP_PARAMS_ACTION"jsp:params"
JSP_PLUGIN_ACTION"jsp:plugin"
JSP_ROOT_ACTION"jsp:root"
JSP_SCRIPTLET_ACTION"jsp:scriptlet"
JSP_SET_PROPERTY_ACTION"jsp:setProperty"
JSP_TAG_DIRECTIVE_ACTION"jsp:directive.tag"
JSP_TAGLIB_DIRECTIVE_ACTION"jsp:taglib"
JSP_TEXT_ACTION"jsp:text"
JSP_USE_BEAN_ACTION"jsp:useBean"
JSP_VARIABLE_DIRECTIVE_ACTION"jsp:directive.variable"
OUTPUT_ACTION"output"
PAGE_DIRECTIVE_ACTION"directive.page"
PARAM_ACTION"param"
PARAMS_ACTION"params"
PLUGIN_ACTION"plugin"
ROOT_ACTION"root"
SCRIPTLET_ACTION"scriptlet"
SET_PROPERTY_ACTION"setProperty"
TAG_DIRECTIVE_ACTION"directive.tag"
TAGLIB_DIRECTIVE_ACTION"taglib"
TEXT_ACTION"text"
USE_BEAN_ACTION"useBean"
VARIABLE_DIRECTIVE_ACTION"directive.variable"
其中directive就是编译指令,总结起来,有:page、include、taglib、tag、attribute、variable,关于他们的执行顺序,看解析程序是最有说服力的,程序如下:
[ 摘自org.apache.jasper.compiler.Parser ]
private void parseDirective(Node parent)
throws JasperException
{
this.reader.skipSpaces();
String directive = null;
if (this.reader.matches("page")) {
directive = "<%@ page";
if (this.isTagFile) {
this.err.jspError(this.reader.mark(), "jsp.error.directive.istagfile", directive);
}
parsePageDirective(parent);
} else if (this.reader.matches("include")) {
directive = "<%@ include";
parseIncludeDirective(parent);
} else if (this.reader.matches("taglib")) {
if (this.directivesOnly)
{
return;
}
directive = "<%@ taglib";
parseTaglibDirective(parent);
} else if (this.reader.matches("tag")) {
directive = "<%@ tag";
if (!(this.isTagFile)) {
this.err.jspError(this.reader.mark(), "jsp.error.directive.isnottagfile", directive);
}
parseTagDirective(parent);
} else if (this.reader.matches("attribute")) {
directive = "<%@ attribute";
if (!(this.isTagFile)) {
this.err.jspError(this.reader.mark(), "jsp.error.directive.isnottagfile", directive);
}
parseAttributeDirective(parent);
} else if (this.reader.matches("variable")) {
directive = "<%@ variable";
if (!(this.isTagFile)) {
this.err.jspError(this.reader.mark(), "jsp.error.directive.isnottagfile", directive);
}
parseVariableDirective(parent);
} else {
this.err.jspError(this.reader.mark(), "jsp.error.invalid.directive");
}
this.reader.skipSpaces();
if (!(this.reader.matches("%>")))
this.err.jspError(this.start, "jsp.error.unterminated", directive);
}
再来回答问题2:
根据源码,我看到JSP编译的顺序是这样的:
1-- getJspConfigPageEncoding
2-- determineSyntaxAndEncoding
3-- 解析成 Node.Nodes parsedPage 对象,即取出所有节点
4-- 解析每个节点
步,是从web.xml等配置文件中去读取配置(里面有个<jsp-config>配置),如果配置时设置了统一编码,则使用这种类型的编码,第2步,是根据文件来获取编码,注意看如下一段代码:
if ((jspReader.matches("tag ")) || (jspReader.matches("page")))
{
jspReader.skipSpaces();
Attributes attrs = Parser.parseAttributes(this, jspReader);
encoding = getPageEncodingFromDirective(attrs, "pageEncoding");
if (encoding != null) {
break;
}
encoding = getPageEncodingFromDirective(attrs, "contentType");
if (encoding != null) {
saveEncoding = encoding;
}
}
}
程序首先判断有无编译指令tag或者page,如果有,则检查编译指令是否指定了pageEncoding属性或者contentType属性。根据这种逻辑,可知如下这种写法:
<%@ page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
其实是重复指定了编码,解析时会以pageEncoding为准。
第4步,解析每个节点:
while (reader.hasMoreInput()) {
parser.parseElements(root);
}
这里又分为几个步骤,先看程序:
private void parseElements(Node parent)
throws JasperException
{
this.start = this.reader.mark();
if (this.reader.matches("<%--")) {
parseComment(parent);
} else if (this.reader.matches("<%@")) {
parseDirective(parent);
} else if (this.reader.matches("<jsp:directive.")) {
parseXMLDirective(parent);
} else if (this.reader.matches("<%!")) {
parseDeclaration(parent);
} else if (this.reader.matches("<jsp:declaration")) {
parseXMLDeclaration(parent);
} else if (this.reader.matches("<%=")) {
parseExpression(parent);
} else if (this.reader.matches("<jsp:expression")) {
parseXMLExpression(parent);
} else if (this.reader.matches("<%")) {
parseScriptlet(parent);
} else if (this.reader.matches("<jsp:scriptlet")) {
parseXMLScriptlet(parent);
} else if (this.reader.matches("<jsp:text")) {
parseXMLTemplateText(parent);
} else if ((!(this.pageInfo.isELIgnored())) && (this.reader.matches("${"))) {
parseELExpression(parent, '$');
} else if ((!(this.pageInfo.isELIgnored())) && (this.reader.matches("#{")))
{
parseELExpression(parent, '#');
} else if (this.reader.matches("<jsp:")) {
parseStandardAction(parent);
} else if (!(parseCustomTag(parent))) {
checkUnbalancedEndTag();
parseTemplateText(parent);
}
}
处理的顺序如下:
1-- “<%-- --%>”类型的注释
2-- “<%@ %>”编译指令
3-- “<jsp:directive. %>”编译指令
4-- “<%! %>”声明指令
5-- “<%= %>”表达式指令
6-- “<% %>”嵌入脚本
7-- “<jsp:text >”嵌入文本
8-- “${ }”EL表达式
9-- “#{ }”EL表达式
10-- “<jsp: >”其他jsp动作指令
11-- 自定义的tag标签
然后,再看看jsp中的java代码(ScriptingElement)是怎么执行的:
第一步:
new一个Node节点,然后把java的字符串完整地赋值给Node的text属性,然后把node添加到Parent Node 队列(List)里面。
第二步:读取这些Nodes,将其转换成java源代码,然后在调用java编译器将源代码编译成class文件。(注意:这个功能相当于是把字符串,转换成了java字节码)
这个过程,调用了SmapUtil将上面那些nodes转换成Java源文件,然后调用JDTCompiler工具类,将Java源文件编译成.class文件,我看Tomcat调用的是org.eclipse.jdt.internal.compiler.*包下面的编译工具,实际上JDK也为我们提供了自己手动编译Java文件的方法,JDK 1.6可以用javax.tools.JavaCompiler。
借鉴这个过程,我自己也实现了“字符串——生成Java文件——编译成class文件”的全自动化过程。这个过程很有用,可以允许我们自己在网站上 上传Java文件并编译运行!
关于JSP解析、编译的研究,暂时就告一段落,有兴趣的可以和我交流。
解读JSP的解析过程的更多相关文章
- Spring-IOC源码解读2.2-BeanDefinition的载入和解析过程
1. 对IOC容器来说这个载入过程就像是相当于把BeanDefinition定义的信息转化成Spring内部表示的数据结构.容器对bean的管理和依赖注入过程都是通过对其持有的BeanDefiniti ...
- DNS原理及其解析过程 精彩剖析
本文章转自下面:http://369369.blog.51cto.com/319630/812889 DNS原理及其解析过程 精彩剖析 网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址 ...
- DNS解析过程详解
先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baidu.com在配置当中应该是www.baidu.com.(最后有一点),一般我们在浏览器里输入时会省略后面的点 ...
- java中文乱码解决之道(七)-----JSP页面编码过程
我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...
- DNS解析过程
参考: http://www.maixj.net/ict/dns-chaxun-9208 http://blog.it985.com/8389.html DNS(Domain Name System) ...
- DNS原理及其解析过程【精彩剖析】(转)
2012-03-21 17:23:10 标签:dig wireshark bind nslookup dns 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...
- dig理解DNS的解析过程 - 阿权的书房
关于DNS的常识,可以阅读附录的一些参考资料.本文旨在尝试举例用dig命令理解这个过程,并非权威知识,仅供参考.测试域名为阿权的书房的域名 www.aslibra.com 和 www.163.com. ...
- 解决JSP 不解析EL表达式
解决JSP 不解析EL表达式,jsp在使用EL表达式的时候发现它不被解析,而是直接以字符串的形式显示了出来,经过查阅资料和实践,终于得知了原因并找到了解决方案 原因是:在默认情况下,Servlet 2 ...
- HiveSQL解析过程详解 | 学步园
HiveSQL解析过程详解 | 学步园 http://www.xuebuyuan.com/2210261.html
随机推荐
- 收藏所用C#技术类面试、笔试题汇总
技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...
- jqurey 遍历 div内的所有input单选复选按钮并判断是否选中及Attr(checked)无效的解决
关于页面前面标签 <ul> @{ foreach (var item in vote) { if (!string.IsNullOrEmpty(item.Img)) { <li cl ...
- 关于Chrome的开发15个小技巧
一.快速查找文件 如果你使用过Sublime,那么你会知道’Go to anything’的强大.没错,Chrome现在也有了这一功能. 操作如下: 1.F12打开你的Chrome调试器: 2.按下C ...
- 【BZOJ1008】【HNOI2008】越狱(数学排列组合题)
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3140 Solved: 1317[Submit][Status] ...
- 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- IOS 判断日期是今天,昨天还是明天
git地址: https://github.com/JsoonLi/NSDate-Extension - (NSString *)compareDate:(NSDate *)date{ NSTim ...
- 50个提高PHP编程效率的方法
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...
- Daily Scrum – 1/6
Meeting Minutes 确认修复了一个bug,即变方的: 分配了以后的任务: 确认将速度写入了用户的设置文件 Burndown Progress part 组员 今日工作 Time ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- 【codevs 1200】【NOIP 2012】同余方程 拓展欧几里德求乘法逆元模板题
模板,,, #include<cstdio> using namespace std; void exgcd(long long a,long long b,long long & ...