Tomcat 6 --- 使用Jasper引擎解析JSP

时间:2021-08-23 18:28:45

熟悉JAVA web开发的朋友都知道JSP会被转换成java文件(预编译),然后编译成class使用,即按照JSP-->java-->class的过程进行编译。

由于JVM只认识class文件,它不知道什么是JSP,因此在tomcat中 如何把JSP解析成java文件 就是本文所要描述的问题。

其他翻译内容参考:Tomcat官方文档翻译

如有错误,请予指正。

什么是Jasper

  Jasper是tomcat中使用的JSP引擎,在Tomcat 6中使用的是Jasper 2,相对于原来的版本作了不少的改进,比如:JSP的标签缓冲池、后台编译、页面改变时自动重新编译、Eclipse中JDT编译等等。

  那么Jasper到底是做什么的呢?

  简单的说,就是把JVM不认识的JSP文件解析成java文件,然后编译成class文件提供使用。目前有很多的JSP解析引擎,Tomcat中使用的是Jasper。

  在Tomcat中可以通过配置 CATALINA_HOME/conf/web.xml 中的内容,配置Jasper的选项(web.xml中的内容很长,截取其中的一部分):

  <!-- The JSP page compiler and execution servlet, which is the mechanism  -->
<!-- used by Tomcat to support JSP pages. Traditionally, this servlet -->
<!-- is mapped to the URL pattern "*.jsp". This servlet supports the -->
<!-- following initialization parameters (default values are in square -->
<!-- brackets): -->
<!-- -->
<!-- checkInterval If development is false and checkInterval is -->
<!-- greater than zero, background compilations are -->
<!-- enabled. checkInterval is the time in seconds -->
<!-- between checks to see if a JSP page (and its -->
<!-- dependent files) needs to be recompiled. [0] -->
<!-- -->
<!-- classdebuginfo Should the class file be compiled with -->
<!-- debugging information? [true] -->
<!-- -->
<!-- classpath What class path should I use while compiling -->
<!-- generated servlets? [Created dynamically -->
<!-- based on the current web application] -->
<!-- -->
<!-- compiler Which compiler Ant should use to compile JSP -->
<!-- pages. See the jasper documentation for more -->
<!-- information. -->
<!-- -->
<!-- compilerSourceVM Compiler source VM. [1.5] -->
<!-- -->
<!-- compilerTargetVM Compiler target VM. [1.5] -->
<!-- -->
<!-- development Is Jasper used in development mode? If true, -->
<!-- the frequency at which JSPs are checked for -->
<!-- modification may be specified via the -->
<!-- modificationTestInterval parameter. [true] -->
<!-- -->
<!-- displaySourceFragment -->
<!-- Should a source fragment be included in -->
<!-- exception messages? [true] -->
<!-- -->
<!-- dumpSmap Should the SMAP info for JSR45 debugging be -->
<!-- dumped to a file? [false] -->
<!-- False if suppressSmap is true -->
<!-- -->
<!-- enablePooling Determines whether tag handler pooling is -->
<!-- enabled. This is a compilation option. It will -->
<!-- not alter the behaviour of JSPs that have -->
<!-- already been compiled. [true] -->
<!-- -->
<!-- engineOptionsClass Allows specifying the Options class used to -->
<!-- configure Jasper. If not present, the default -->
<!-- EmbeddedServletOptions will be used. -->
<!-- -->
<!-- errorOnUseBeanInvalidClassAttribute -->
<!-- Should Jasper issue an error when the value of -->
<!-- the class attribute in an useBean action is -->
<!-- not a valid bean class? [true] -->
<!-- -->
<!-- fork Tell Ant to fork compiles of JSP pages so that -->
<!-- a separate JVM is used for JSP page compiles -->
<!-- from the one Tomcat is running in. [true] -->
<!-- -->
<!-- genStringAsCharArray -->
<!-- Should text strings be generated as char -->
<!-- arrays, to improve performance in some cases? -->
<!-- [false] -->
<!-- -->
<!-- ieClassId The class-id value to be sent to Internet -->
<!-- Explorer when using <jsp:plugin> tags. -->
<!-- [clsid:8AD9C840-044E-11D1-B3E9-00805F499D93] -->
<!-- -->
<!-- javaEncoding Java file encoding to use for generating java -->
<!-- source files. [UTF8] -->
<!-- -->
<!-- keepgenerated Should we keep the generated Java source code -->
<!-- for each page instead of deleting it? [true] -->
<!-- -->
<!-- mappedfile Should we generate static content with one -->
<!-- print statement per input line, to ease -->
<!-- debugging? [true] -->
<!-- -->
<!-- modificationTestInterval -->
<!-- Causes a JSP (and its dependent files) to not -->
<!-- be checked for modification during the -->
<!-- specified time interval (in seconds) from the -->
<!-- last time the JSP was checked for -->
<!-- modification. A value of 0 will cause the JSP -->
<!-- to be checked on every access. -->
<!-- Used in development mode only. [4] -->
<!-- -->
<!-- recompileOnFail If a JSP compilation fails should the -->
<!-- modificationTestInterval be ignored and the -->
<!-- next access trigger a re-compilation attempt? -->
<!-- Used in development mode only and is disabled -->
<!-- by default as compilation may be expensive and -->
<!-- could lead to excessive resource usage. -->
<!-- [false] -->
<!-- -->
<!-- scratchdir What scratch directory should we use when -->
<!-- compiling JSP pages? [default work directory -->
<!-- for the current web application] -->
<!-- -->
<!-- suppressSmap Should the generation of SMAP info for JSR45 -->
<!-- debugging be suppressed? [false] -->
<!-- -->
<!-- trimSpaces Should white spaces in template text between -->
<!-- actions or directives be trimmed? [false] -->
<!-- -->
<!-- xpoweredBy Determines whether X-Powered-By response -->
<!-- header is added by generated servlet [false] -->
<!-- -->
<!-- If you wish to use Jikes to compile JSP pages: -->
<!-- Please see the "Using Jikes" section of the Jasper-HowTo -->
<!-- page in the Tomcat documentation. --> <servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>

  具体的参数,上面都有解释,这里就不多赘述了。

  都是些调节JSP编译的参数,比如多长时间检测一次,debug的调试信息相关配置,编译信息等等。

如何利用Japser.Jspc自定义预编译JSP

  1 jasper相关jar包

  在tomcat6中提供了几个jasper的jar包,相对于之前版本,去掉了jasper-compiler.jar以及jasper-runtime.jar,合并为jasper.jar

Tomcat 6 --- 使用Jasper引擎解析JSP

  另外如果开发者自己想要编译JSP,还需要使用ant以及tomcat-juli.jar。

  2 ant相关jar包

  其中ant相关的jar包可以去官网下载ant.zip,然后解压提取其中lib内的jar包。

Tomcat 6 --- 使用Jasper引擎解析JSP

  3 tomcat-juli相关jar包

  tomcat-juli.jar位于CATALINA_HOME/bin/目录下。

  在Eclipse的构建路径下添加上述相关的jar包即可,然后创建测试类:

  添加JAR包步骤:右键工程-->Properties-->Java Build Path-->Libraries-->Add External JARs-->选择添加的JAR包-->OK

package com.test;
import org.apache.jasper.JspC; public class testCompiler{
public String jspcTest(){
String error="";
try {
JspC jspc = new JspC();
//第一种方式
String[] arg0 = {"-uriroot", "F:/apache-tomcat-6.0.43/webapps/ROOT", "-d", "F:/test",
"index.jsp" };
jspc.setArgs(arg0); //第二种方式
/*jspc.setUriroot("F:/apache-tomcat-6.0.43/webapps/ROOT");//web应用的root目录
jspc.setOutputDir("F:/test");//.java文件和.class文件的输出目录
jspc.setJspFiles("index.jsp");//要编译的jsp */ jspc.setCompile(true);//是否编译 false或不指定的话只生成.java文件
jspc.execute();
}catch(Exception e){
error=e.toString();
}
return error;
}
public static void main(String args[]){
testCompiler t=new testCompiler();
System.out.println(t.jspcTest());
}
}

  可以使用两种方式进行自定义的JSP编译。

  测试后,可以在 F:/test 目录下发现编译出的index.jsp的java文件以及class文件。

Tomcat 6 --- 使用Jasper引擎解析JSP

参考

【1】Jasper2 JSP引擎:http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html

【2】解读JSP解析过程:http://www.cnblogs.com/zollty/p/3309310.html

【3】使用Jspc编译JSP文件:http://kjah.iteye.com/blog/625588

Tomcat 6 --- 使用Jasper引擎解析JSP的更多相关文章

  1. apache、nginx、Tomcat、IIS引擎解析漏洞

                                            引擎解析漏洞 常见的web容器有IIS.Apache.Nginx.Tomcat等,以下是详细讲解 IIS IIS简介 是 ...

  2. 报错:org&period;apache&period;jasper&period;JasperException&colon; &sol;index&period;jsp &lpar;line&colon; 1&comma; column&colon; 17&rpar; equal symbol expected

    现象:写了如下一个jsp文件,导入需要用到的两个包: 运行结果报错:org.apache.jasper.JasperException: /index.jsp (line: 1, column: 17 ...

  3. JSP引擎、JSP容器、Web服务器

    JSP引擎与JSP容器指的都是同一样的东西,他们都是用来同一管理和运行Web引用程序的“软件”.常见的JSP引擎有Tomcat.JRun.Resin 广义上来说,JSP引擎是用来管理和运行Web应用程 ...

  4. JBoss和Tomcat版本、及Servlet、JSP规范版本对应一览 【转】

    原文地址:http://blog.csdn.net/hills/article/details/40896357 JBoss和Tomcat版本.及Servlet.JSP规范版本对应一览 JBossAS ...

  5. Fixflow引擎解析&lpar;四&rpar;&lpar;模型&rpar; - 通过EMF扩展BPMN2&period;0元素

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  6. Fixflow引擎解析&lpar;三&rpar;&lpar;模型&rpar; - 创建EMF模型来读写XML文件

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  7. Fixflow引擎解析&lpar;二&rpar;&lpar;模型&rpar; - BPMN2&period;0读写

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  8. Fixflow引擎解析&lpar;一&rpar;&lpar;介绍&rpar; - Fixflow开源流程引擎介绍

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  9. jQuery 2&period;0&period;3 源码分析Sizzle引擎解析原理

    jQuery 2.0.3 源码分析Sizzle引擎 - 解析原理 声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 先来回答博友的提问: 如何解析 div > p + ...

随机推荐

  1. 【转】web移动端一些常用知识

    1.去掉 a,input 在移动端浏览器中的默认样式(半透明灰色遮罩阴影) a,button,input,optgroup,select,textarea { -webkit-tap-highligh ...

  2. Linux学习四:UDP编程&lpar;上&rpar;

    关于UDP和TCP对比优缺,这里就不说了. 使用UDP代码所掉用的函数和用于TCP的函数非常类似,这主要因为套接口库在底层的TCP和UDP的函数上加了一层抽象,通过这层抽象使得编程更容易,但失去了一些 ...

  3. GNU C 扩展(转)

    GNU CC 是一个功能非常强大的跨平台 C 编译器,它对 C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.这里对支持支持 GNU 扩展的 C 语言成为 GN ...

  4. android SDK 快速更新配置(转)

    http://blog.csdn.net/yy1300326388/article/details/45074447 1.强制使用http替换https链接 Tools>选择Options,勾选 ...

  5. ActionBar 中setDisplayHomeAsUpEnabled和setHomeButtonEnabled和setDisplayShowHomeEnabled方法的理解

    setHomeButtonEnabled这个小于4.0版本的默认值为true的.但是在4.0及其以上是false,该方法的作用:决定左上角的图标是否可以点击.没有向左的小图标. true 图标可以点击 ...

  6. C&num; - winform使用Dictionary的时候&comma;程序一闪而过&excl;

    概述: 具体也不知道是多线程造成,还是Dictionary的缺陷. 解决方案: 1.如果可能会添加相同键值,你就别用add,直接添加键值,这样不报错 2.使用Try...catch可以捕获异常. 3. ...

  7. e2e测试框架之Cypress

    谈起web自动化测试,大家首先想到的是Selenium!随着近几年前端技术的发展,出现了不少前端测试框架,这些测试框架大多并不依赖于Selenium,这一点跟后端测试框架有很大不同,如Robot Fr ...

  8. Initialization of bean failed&semi; nested exception is java&period;lang&period;IllegalArgumentException&colon; error at &colon;&colon;0 inconsistent binding

    1.发生原因  springAOP 里面绑定参数出现错误  核对绑定参数的名称    核对 springAOP的版本 2.aop切面表达式写的有误

  9. Mybatis select、insert、update、delete 增删改查操作

    MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索.MyBatis 可以使用简单的XML ...

  10. 中国用户通过rchange用银联充值到PerfectMoney再给BTC-E充值进行搬砖的方法

    最近迷上了比特币这个疯狂的东西,相信很多技术人员都感兴趣. 比特币.莱特币钱包下载和把数据迁移到C盘以外其他盘的方法. 莱特币和山寨币的原理跟比特币基本上一样,可以参考这个方法进行,莱特币的钱包数据迁 ...