暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

JSP在Tomcat的相关配置 (JSPServlet系列之四)

中间件技术讨论圈 2016-09-07
298

原参考页面:http://tomcat.apache.org/tomcat-8.5-doc/jasper-howto.html


1.Jasper2的改进点

在Tomcat 8.5 以上使用的是 Jasper 2,对应的规范也就是JSP2.3;

Tomcat 的 Jasper 2 因为要编译大量的jsp文件,设计目标就是更高的提高jsp的编译效率,改进点:

  • JSP Custom Tag Pooling - The java objects instantiated for JSP Custom Tags can now be pooled and reused. This significantly boosts the performance of JSP pages which use custom tags.

          Tomcat为自定义的custom tag做的一个pool的缓存,这个缓存池的目的,是为了更好的将自定义的自定义的标签编译到再一次出现的位置,因为自定义的标签肯定是多次出现在自己写的jsp中的;


  • Background JSP compilation - If you make a change to a JSP page which had already been compiled Jasper 2 can recompile that page in the background. The previously compiled JSP page will still be available to serve requests. Once the new page has been compiled successfully it will replace the old page. This helps improve availability of your JSP pages on a production server.

          jsp后台编译,说白了就是后台启动一个线程,这个线程时刻关注jsp文件的modifcation的时间,如果一旦发生修改,说明该jsp被修改,那么再一次访问这个jsp的时候,就需要访问最新的jsp编译的servlet了;


  • Recompile JSP when included page changes - Jasper 2 can now detect when a page included at compile time from a JSP has changed and then recompile the parent JSP.

          对于jasper的编译过程,也不是一帆风顺的,很多jsp由于写法和一些错误的标签,会导致jsp无法编译成功,对于这种再一次编译也无济于事,但有的时候,如线程锁超时,内存太多等因素,jsp一次没有编译成功,对于这个情况,如果多提供了一次编译的机会,那么就会编译成功,而这个也即是jasper的工作和任务;


  • JDT used to compile JSP pages - The Eclipse JDT Java compiler is now used to perform JSP java source code compilation. This compiler loads source dependencies from the container classloader. Ant and javac can still be used.

    原来的compiler就是AntCompiler,但是AntCompiler 会占用大量的内存,这些类都需要重新加载,而JDTCompiler就会活用自己的ClassLoader进行加载,效率提升了一大截;


2.重要参数配置

在web.xml中,有很多的参数可以配置到init parameters中,全局的配置在$CATALINA_BASE/conf/web.xml 。


  • checkInterval - If development is false and checkInterval is greater than zero, background compiles are enabled. checkInterval is the time in seconds between checks to see if a JSP page (and its dependent files) needs to be recompiled. Default 0
     seconds.

         当jsp出于开发模式的时候,后台不断的check你的jsp是否被修改过,每一次都是需要重新开始扫描任务,这里的checkInterval就是这个check的时间;


  • classdebuginfo - Should the class file be compiled with debugging information? true
     or false
    , default true
    .

         在编译过程中打开debug的调试信息;


  • classpath - Defines the class path to be used to compile the generated servlets. This parameter only has an effect if the ServletContext attribute org.apache.jasper.Constants.SERVLET_CLASSPATH is not set. This attribute is always set when Jasper is used within Tomcat. By default the classpath is created dynamically based on the current web application.

         在genrate生成java文件之后,java编译servlet的时候,带上的classPath;


  • compiler - Which compiler Ant should use to compile JSP pages. The valid values for this are the same as for the compiler attribute of Ant's javac task. If the value is not set, then the default Eclipse JDT Java compiler will be used instead of using Ant. There is no default value. If this attribute is set then setenv.[sh|bat]
     should be used to add ant.jar
    ant-launcher.jar
     and tools.jar
     to the CLASSPATH
     environment variable.

          选择Tomcat前面讲过的内置的两个compiler,也可以自定义编译器,如果你足够牛B的话;


  • compilerSourceVM - What JDK version are the source files compatible with? (Default value: 1.7
    )

          第一步,genrate生成java文件,从哪个jdk版本开始兼容;

  • compilerTargetVM - What JDK version are the generated files compatible with? (Default value: 1.7
    )

          第二步,编译成class文件,是从哪个jdk版本开始兼容;

    

  • 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
     or false
    , default true
    .

          开发模式和生产模式,jsp出于开发模式的时候,如果jsp被修改过了,在开发模式中会进行重新编译,其作用的参数就是前面的checkInterval这些,如果到了生产模式,jsp只是应用启动的时候编译一次,以后怎么修改都不管用了;


  • 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
     or false
    , default true
    .

          第一节提到的那个自定义Taglib的pool池;


  • engineOptionsClass - Allows specifying the Options class used to configure Jasper. If not present, the default EmbeddedServletOptions will be used. This option is ignored if running under a SecurityManager.

          对于这些默认的配置,Tomcat通过Options接口的实现类进行load,默认的是EmbeddedServletOptions  ,当然你也可以自己指定;


  • genStringAsCharArray - Should text strings be generated as char arrays, to improve performance in some cases? Defaultfalse
    .

          一个比较重要的优化参数,text如果编译String类型比较消耗性能,因为我们可以看到,在servlet中大多数都是out.println这种,而一个string对象和一个char array,肯定是char array要提升一些性能了;


  • ieClassId - The class-id value to be sent to Internet Explorer when using <jsp:plugin> tags. Default clsid:8AD9C840-044E-11D1-B3E9-00805F499D93
    .

          ie浏览器中,<jsp:plugin>中的一些插件,会生成一些id;


  • javaEncoding - Java file encoding to use for generating java source files. Default UTF8
    .

          java文件的encoding


  • keepgenerated - Should we keep the generated Java source code for each page instead of deleting it? true
     or false
    , default true
    .

          在jsp编译为servlet的class文件之后,是否其java源文件也保留


  • mappedfile - Should we generate static content with one print statement per input line, to ease debugging? true
     or false
    , default true
    .

          是否在jsp编译之后的class文件中加带调试信息;


  • maxLoadedJsps - The maximum number of JSPs that will be loaded for a web application. If more than this number of JSPs are loaded, the least recently used JSPs will be unloaded so that the number of JSPs loaded at any one time does not exceed this limit. A value of zero or less indicates no limit. Default -1

    在源码分析的时候,还记得JspServletWrapper中的实例需要有一个队列来进行维护吗?这个参数就是控制这个队列中的数量的;

  • jspIdleTimeout - The amount of time in seconds a JSP can be idle before it is unloaded. A value of zero or less indicates never unload. Default -1

    多长时间,一个jsp不被访问,可以被卸掉,JspServletWrapper


  • 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. Default is 4
     seconds.

          这个是jsp页面的check modification的时间间隔,当checkInterval到了,check应用中的jsp任务开始了,对于每一个jsp页面到上一次check都需要一个时间,这个时间的设置很有讲究,需要和checkInterval一块配置来进行设置,默认是4s;


  • 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.

          这个属性就是当编译失败了,是否再重新进行编译,再来一次;


  • scratchdir - What scratch directory should we use when compiling JSP pages? Default is the work directory for the current web application. This option is ignored if running under a SecurityManager.

          jsp编译成servlet放在哪里,默认放在work下面,host虚拟主机下面的包下;


  • trimSpaces - Should white spaces in template text between actions or directives be trimmed ?, default false
    .

          这个也是一个优化参数,空白行空格之类的都给干掉,这样会减少编译的字符和解析的难度


  • xpoweredBy - Determines whether X-Powered-By response header is added by generated servlet. true
     or false
    , defaultfalse
    .

         支持编译完servlet在响应头上加xpoweredBy ,这个选项是告诉浏览器端,这个页面的支持技术是xxx;


关于Compiler的选择的英文原文:

The Java compiler from Eclipse JDT in included as the default compiler. It is an advanced Java compiler which will load all dependencies from the Tomcat class loader, which will help tremendously when compiling on large installations with tens of JARs. On fast servers, this will allow sub-second recompilation cycles for even large JSP pages.


3.jvm虚拟机关于编译的一个bug


As described in bug 39089, a known JVM issue, bug 6294277, may cause a java.lang.InternalError: name is too long to represent
 exception when compiling very large JSPs. If this is observed then it may be worked around by using one of the following:

  • reduce the size of the JSP

  • disable SMAP generation and JSR-045 support by setting suppressSmap
     to true
    .

这个bug是说明javac在编译大文件的手,可能会有问题,这也就是前面为什么Tomcat会搞出一个recompile的参数的原因,

具体的做法可以看到,减小jsp文件大小,或者利用suppressSmap
 ,甚至禁用JSR45;


关于JSR45是一个 什么东西呢?

这是一个jsp的debug的一个规范,例如实现了这个规范,你可以甚至在eclipse调试jsp,而不是jsp最后编译的;

因此,如果你编译的时候,还用了smap这些信息,那么肯定会影响效率,其次,还会导致jvm的bug,因此samp能不用就不用;



4.生产模式


The main JSP optimization which can be done is precompilation of JSPs. However, this might not be possible (for example, when using the jsp-property-group feature) or practical, in which case the configuration of the Jasper servlet becomes critical.

When using Jasper 2 in a production Tomcat server you should consider making the following changes from the default configuration.


生产模式是很重要的,能提高jsp的运行效率,如果需要配置为生产模式的话,那么下面的配置需要改变:


  • development - To disable on access checks for JSP pages compilation set this to false
    .

  • genStringAsCharArray - To generate slightly more efficient char arrays, set this to true
    .

  • modificationTestInterval - If development has to be set to true
     for any reason (such as dynamic generation of JSPs), setting this to a high value will improve performance a lot.

  • trimSpaces - To remove useless bytes from the response, set this to true
    .


5.jsp-config标签

<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素。其中<taglib> 元素在JSP 1.2 时就已经存在;而<jsp-property-group> 是JSP 2.0 新增的元素。<jsp-property-group> 元素主要有八个子元素,它们分别为: 


1.<description>:设定的说明 
2.<display-name>:设定名称 
3.<url-pattern>:设定值所影响的范围,如: /CH2 或 /*.jsp
4.<el-ignored>:若为 true,表示不支持 EL 语法 
5.<scripting-invalid>:若为 true,表示不支持 <% scripting %>语法 
6.<page-encoding>:设定 JSP 网页的编码 
7.<include-prelude>:设置 JSP 网页的抬头,扩展名为 .jspf
8.<include-coda>:设置 JSP 网页的结尾,扩展名为 .jspf


例子:

<jsp-config>      

  <taglib>      

    <taglib-uri>Taglib</taglib-uri>      

    <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>      

  </taglib>      

  <jsp-property-group>      

    <description>Special property group for JSP Configuration JSP example.</description>      

    <display-name>JSPConfiguration</display-name>      

    <url-pattern>/jsp/* </url-pattern>      

    <el-ignored>true</el-ignored>      

    <page-encoding>GB2312</page-encoding>      

    <scripting-invalid>true</scripting-invalid>      

    <include-prelude>/include/prelude.jspf</include-prelude>      

    <include-coda>/include/coda.jspf</include-coda>      

  </jsp-property-group>      

</jsp-config> 


文章转载自中间件技术讨论圈,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论