熟悉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中的内容很长,截取其中的一部分):

  1. <!-- The JSP page compiler and execution servlet, which is the mechanism -->
  2. <!-- used by Tomcat to support JSP pages. Traditionally, this servlet -->
  3. <!-- is mapped to the URL pattern "*.jsp". This servlet supports the -->
  4. <!-- following initialization parameters (default values are in square -->
  5. <!-- brackets): -->
  6. <!-- -->
  7. <!-- checkInterval If development is false and checkInterval is -->
  8. <!-- greater than zero, background compilations are -->
  9. <!-- enabled. checkInterval is the time in seconds -->
  10. <!-- between checks to see if a JSP page (and its -->
  11. <!-- dependent files) needs to be recompiled. [0] -->
  12. <!-- -->
  13. <!-- classdebuginfo Should the class file be compiled with -->
  14. <!-- debugging information? [true] -->
  15. <!-- -->
  16. <!-- classpath What class path should I use while compiling -->
  17. <!-- generated servlets? [Created dynamically -->
  18. <!-- based on the current web application] -->
  19. <!-- -->
  20. <!-- compiler Which compiler Ant should use to compile JSP -->
  21. <!-- pages. See the jasper documentation for more -->
  22. <!-- information. -->
  23. <!-- -->
  24. <!-- compilerSourceVM Compiler source VM. [1.5] -->
  25. <!-- -->
  26. <!-- compilerTargetVM Compiler target VM. [1.5] -->
  27. <!-- -->
  28. <!-- development Is Jasper used in development mode? If true, -->
  29. <!-- the frequency at which JSPs are checked for -->
  30. <!-- modification may be specified via the -->
  31. <!-- modificationTestInterval parameter. [true] -->
  32. <!-- -->
  33. <!-- displaySourceFragment -->
  34. <!-- Should a source fragment be included in -->
  35. <!-- exception messages? [true] -->
  36. <!-- -->
  37. <!-- dumpSmap Should the SMAP info for JSR45 debugging be -->
  38. <!-- dumped to a file? [false] -->
  39. <!-- False if suppressSmap is true -->
  40. <!-- -->
  41. <!-- enablePooling Determines whether tag handler pooling is -->
  42. <!-- enabled. This is a compilation option. It will -->
  43. <!-- not alter the behaviour of JSPs that have -->
  44. <!-- already been compiled. [true] -->
  45. <!-- -->
  46. <!-- engineOptionsClass Allows specifying the Options class used to -->
  47. <!-- configure Jasper. If not present, the default -->
  48. <!-- EmbeddedServletOptions will be used. -->
  49. <!-- -->
  50. <!-- errorOnUseBeanInvalidClassAttribute -->
  51. <!-- Should Jasper issue an error when the value of -->
  52. <!-- the class attribute in an useBean action is -->
  53. <!-- not a valid bean class? [true] -->
  54. <!-- -->
  55. <!-- fork Tell Ant to fork compiles of JSP pages so that -->
  56. <!-- a separate JVM is used for JSP page compiles -->
  57. <!-- from the one Tomcat is running in. [true] -->
  58. <!-- -->
  59. <!-- genStringAsCharArray -->
  60. <!-- Should text strings be generated as char -->
  61. <!-- arrays, to improve performance in some cases? -->
  62. <!-- [false] -->
  63. <!-- -->
  64. <!-- ieClassId The class-id value to be sent to Internet -->
  65. <!-- Explorer when using <jsp:plugin> tags. -->
  66. <!-- [clsid:8AD9C840-044E-11D1-B3E9-00805F499D93] -->
  67. <!-- -->
  68. <!-- javaEncoding Java file encoding to use for generating java -->
  69. <!-- source files. [UTF8] -->
  70. <!-- -->
  71. <!-- keepgenerated Should we keep the generated Java source code -->
  72. <!-- for each page instead of deleting it? [true] -->
  73. <!-- -->
  74. <!-- mappedfile Should we generate static content with one -->
  75. <!-- print statement per input line, to ease -->
  76. <!-- debugging? [true] -->
  77. <!-- -->
  78. <!-- modificationTestInterval -->
  79. <!-- Causes a JSP (and its dependent files) to not -->
  80. <!-- be checked for modification during the -->
  81. <!-- specified time interval (in seconds) from the -->
  82. <!-- last time the JSP was checked for -->
  83. <!-- modification. A value of 0 will cause the JSP -->
  84. <!-- to be checked on every access. -->
  85. <!-- Used in development mode only. [4] -->
  86. <!-- -->
  87. <!-- recompileOnFail If a JSP compilation fails should the -->
  88. <!-- modificationTestInterval be ignored and the -->
  89. <!-- next access trigger a re-compilation attempt? -->
  90. <!-- Used in development mode only and is disabled -->
  91. <!-- by default as compilation may be expensive and -->
  92. <!-- could lead to excessive resource usage. -->
  93. <!-- [false] -->
  94. <!-- -->
  95. <!-- scratchdir What scratch directory should we use when -->
  96. <!-- compiling JSP pages? [default work directory -->
  97. <!-- for the current web application] -->
  98. <!-- -->
  99. <!-- suppressSmap Should the generation of SMAP info for JSR45 -->
  100. <!-- debugging be suppressed? [false] -->
  101. <!-- -->
  102. <!-- trimSpaces Should white spaces in template text between -->
  103. <!-- actions or directives be trimmed? [false] -->
  104. <!-- -->
  105. <!-- xpoweredBy Determines whether X-Powered-By response -->
  106. <!-- header is added by generated servlet [false] -->
  107. <!-- -->
  108. <!-- If you wish to use Jikes to compile JSP pages: -->
  109. <!-- Please see the "Using Jikes" section of the Jasper-HowTo -->
  110. <!-- page in the Tomcat documentation. -->
  111.  
  112. <servlet>
  113. <servlet-name>jsp</servlet-name>
  114. <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
  115. <init-param>
  116. <param-name>fork</param-name>
  117. <param-value>false</param-value>
  118. </init-param>
  119. <init-param>
  120. <param-name>xpoweredBy</param-name>
  121. <param-value>false</param-value>
  122. </init-param>
  123. <load-on-startup>3</load-on-startup>
  124. </servlet>

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

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

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

  1 jasper相关jar包

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

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

  2 ant相关jar包

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

  3 tomcat-juli相关jar包

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

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

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

  1. package com.test;
  2. import org.apache.jasper.JspC;
  3.  
  4. public class testCompiler{
  5. public String jspcTest(){
  6. String error="";
  7. try {
  8. JspC jspc = new JspC();
  9. //第一种方式
  10. String[] arg0 = {"-uriroot", "F:/apache-tomcat-6.0.43/webapps/ROOT", "-d", "F:/test",
  11. "index.jsp" };
  12. jspc.setArgs(arg0);
  13.  
  14. //第二种方式
  15. /*jspc.setUriroot("F:/apache-tomcat-6.0.43/webapps/ROOT");//web应用的root目录
  16. jspc.setOutputDir("F:/test");//.java文件和.class文件的输出目录
  17. jspc.setJspFiles("index.jsp");//要编译的jsp */
  18.  
  19. jspc.setCompile(true);//是否编译 false或不指定的话只生成.java文件
  20. jspc.execute();
  21. }catch(Exception e){
  22. error=e.toString();
  23. }
  24. return error;
  25. }
  26. public static void main(String args[]){
  27. testCompiler t=new testCompiler();
  28. System.out.println(t.jspcTest());
  29. }
  30. }

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

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

参考

【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.apache.jasper.JasperException: /index.jsp (line: 1, column: 17) 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引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素

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

  6. Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件

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

  7. Fixflow引擎解析(二)(模型) - BPMN2.0读写

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

  8. Fixflow引擎解析(一)(介绍) - Fixflow开源流程引擎介绍

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

  9. jQuery 2.0.3 源码分析Sizzle引擎解析原理

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

随机推荐

  1. poj3335 半平面交

    题意:给出一多边形.判断多边形是否存在一点,使得多边形边界上的所有点都能看见该点. sol:在纸上随手画画就可以找出规律:按逆时针顺序连接所有点.然后找出这些line的半平面交. 题中给出的点已经按顺 ...

  2. qt 使用非系统字库

    之前的做法都是把 ttc, ttf 这些文件拷贝到系统字库里去(即拷贝到 lib/fonts 下).但是,每次添加字体,我都要把产品的文件系统都给升级一遍吗?这样系统的一致性就不大好了.所以想能不能直 ...

  3. PHP Fatal error: Call to undefined function mb_substr()

    Lamp架构 PHP 5.3.29 #查看php是否有mbstring模块 php -m | grep mbstring yum install php-mbstring -y find / -nam ...

  4. SLF4J的好处

    原来我们使用log4j去打印日志,如果我们要更改日志记录器,比如用comms-Logging,那我们在代码里面还要改每个类的引用包,但是如果用slf4j的话只要在配置的时候改下,代码完全用slf4j的 ...

  5. python面向对象基础

    面向对象基础 1. 简述 编程方式: 面向过程: 根据代码在脚本的堆叠顺序,从上到下依次执行 函数式编程:将相同功能的代码封装到函数中,直接调用即可,减少代码重复性 面向对象:对函数进行分类和封装,将 ...

  6. 第一个python程序-判断登陆用户名和密码是否正确

    #setencoding=utf-8 #用户名和密码输入正确,则登陆成功 #用户名正确密码错误,只再输入密码,有3次机会 #错误3次,则把用户名放入lock中 import os,sys #存放用户名 ...

  7. hdu 2014 青年歌手大奖赛_评委会打分

    题意: 输入N个数,去掉最大和最小的数,求剩余的数的平均数. 解法: 找到分别最大和最小的数,然后从总和中减去他们,再求平均数(不要排序): 1: #include<stdlib.h> 2 ...

  8. javascript表单验证

    表单HTML <form action="" method="post"> <fieldset class="login" ...

  9. CentOS6.5 安装Sphinx 配置MySQL数据源

      前提安装完mysql,并创建测试表和数据 DROP TABLE IF EXISTS `documents`; CREATE TABLE IF NOT EXISTS `documents` ( `i ...

  10. 按照网上方法js删除指定cookie,却怎么也删除不了,解决如下

    网上方法: 查找原因说是没有指定Path,记得系统里以前也没指定还是可以的,就查了一下现在的系统Path,猜测是系统Path由以前的/改为/E7-Planning 就改了前端删除方法 测试一下OK了, ...