tomcat

来源:互联网 发布:保密局软件 编辑:程序博客网 时间:2024/04/29 05:48

Jasper 2 JSP Engine How To

Table of Contents

简介
配 置
生产配置
网络应用程序编译
使用 Jikes

Introduction

Tomcat 5.0使用Jasper 2 JSP Engine来实现 JavaServer Pages 2.0规范。

Jasper 2被重新设计后,它的性能比原来的Jasper有极大的改善。除了基本的代码改进 以外,还有下面这些变化:

  • JSP Custom Tag Pooling - Java对象例示的JSP客户自定义标签 (JSP Custom Tags)现在可以被汇集起来重新使用。这极大地提高了使用客户自定义 标签的JSP页面的性能。
  • Background JSP compilation - 如果你更改了已经被编译过的JSP 页面,Jasper 2可以在后台重新编译这个页面。先前编译过的JSP页面还是被用来提供 服务。一旦新的页面被编译好了,它就可以取代旧的页面。这有助于提高你的JSP页面 在生产服务器上的可使用性。
  • Recompile JSP when included page changes - Jasper 2现在可以 在编译时探测到什么时候JSP里的一个页面被更改过,并重新编译父JSP。
  • JDT used to compile JSP pages - Eclipse的JDT Java编译器现在 用来完成JSP中java源代码的编译工作。这个编译器加载源代码依赖于容器的类加载器。 Ant和javac依旧可以使用。

 

Jasper是通过使用servlet类org.apache.jasper.servlet.JspServlet来 实现的。

Configuration

默认的Jasper是被配置用来开发web应用程序的。关于使用于Tomcat生产服务器上的 Jasper配置信息,参看生产配置( Production Configuration)这一章。

用来实现Jasper的servlet是通过使用你的全局 $CATALINA_BASE/conf/web.xml文件里面的参数来配置的。

  • checkInterval - 如果development设置为false,reloading设置为 true,后台编译就可以实现了。checkInterval是检查一个JSP页面是否需要被重新编译 的秒数间隔。默认值是300秒。
  • compiler - 推荐使用Ant来编译JSP页面。具体信息请查阅Ant的 文档。如果没有被赋值,则默认的Eclipse JDT Java编译器将被用来代替Ant。 没有默认值。
  • classdebuginfo - 类文件在编译时是否显示排错(debugging)提示信息? truefalse,默认是true。
  • classpath - 在编译产生的servlets时,应该使用什么class path呢? 默认的classpath是基于当前web应用程序动态地产生的。
  • compilerSourceVM - 编译源文件时采用的JDK版本是什么?(默认是 JDK 1.4)
  • compilerTargetVM - 生成文件是采用的JDK版本是什么?(默认是 JDK1.4)
  • development - Jasper是否处于开发模式(将在每一次存取时检查JSP 是否被修改)?truefalse,默认值是true 。
  • enablePooling - 确定标签处理池(tag handler pooling)是否可被 使用。truefalse,默认值是true 。
  • engineOptionsClass - 允许指定的类来配置Jasper。如果没有指定, 则使用默认的Servlet内置参数(EmbeddedServletOptions)。
  • ieClassId - 当使用<jsp:plugin>标签时要发送给Internet Explorer的class-id值. 默认形式是clsid:8AD9C840-044E-11D1-B3E9-00805F499D93
  • fork - 是否让Ant把JSP页面编译进行分叉(fork),这样一来编译就 可以在与Tomcat在不同的JVM里执行? truefalse,默认值是true 。
  • javaEncoding - 用来产生java源文件的Java文件编码。默认是UTF8
  • genStringAsCharArray - 是否把字符串转换为字符数组?在某些 情况下会用到。默认 false.
  • keepgenerated - 是否应该保留为每一页产生的Java源代码,而 不是把它删除掉?truefalse , 默认值是true 。
  • mappedfile - 是否应该为每一行输入打印一个静态说明,来使排错 变得容易?truefalse , 默认值是true 。
  • modificationTestInterval - Checks for modification for a given JSP file (and all its dependent files) will be performed only once every specified amount of seconds. Setting this to 0 will cause the JSP to be checked on every access. Default is 4 seconds.
  • reloading - Should Jasper check for modified JSPs? true or false, default false.
  • scratchdir - 在编译JSP页面的时候,应该使用什么scratch目录?默认的目录是当前web应用程序的工作目录。
  • trimSpaces - 样板本文里actions或指令之间的空白空间是否应该被修剪?默认值是false

 

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. This new compiler will be updated to support the Java 5 syntax as soon as possible.

Apache Ant, which was used in previous Tomcat releases, can be used instead instead of the new compiler by simply removing the common/lib/jasper-compiler-jdt.jar file, and placing the ant.jar file from the latest Ant distribution in the common/lib folder. If you do this, you also need to use the "javac" argument to catalina.sh.

Production Configuration

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.

在生产Tomcat服务器里使用Jasper 2时,你应该考虑在默认的配置上进行下列修改。

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

 

Web Application Compilation

Using Ant is the preferred way to compile web applications using JSPC. Use the script given below (a similar script is included in the "deployer" download) to precompile a webapp:

 

<project name="Webapp Precompilation" default="all" basedir=".">   <target name="jspc">     <taskdef classname="org.apache.jasper.JspC" name="jasper2" >       <classpath id="jspc.classpath">         <pathelement location="${java.home}/../lib/tools.jar"/>         <fileset dir="${tomcat.home}/bin">           <include name="*.jar"/>         </fileset>         <fileset dir="${tomcat.home}/server/lib">           <include name="*.jar"/>         </fileset>         <fileset dir="${tomcat.home}/common/lib">           <include name="*.jar"/>         </fileset>       </classpath>     </taskdef>     <jasper2              validateXml="false"              uriroot="${webapp.path}"              webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"              outputDir="${webapp.path}/WEB-INF/src" />   </target>   <target name="compile">    <mkdir dir="${webapp.path}/WEB-INF/classes"/>    <mkdir dir="${webapp.path}/WEB-INF/lib"/>    <javac destdir="${webapp.path}/WEB-INF/classes"           optimize="off"           debug="on" failonerror="false"           srcdir="${webapp.path}/WEB-INF/src"    excludes="**/*.smap">      <classpath>        <pathelement location="${webapp.path}/WEB-INF/classes"/>        <fileset dir="${webapp.path}/WEB-INF/lib">          <include name="*.jar"/>        </fileset>        <pathelement location="${tomcat.home}/common/classes"/>        <fileset dir="${tomcat.home}/common/lib">          <include name="*.jar"/>        </fileset>        <pathelement location="${tomcat.home}/shared/classes"/>        <fileset dir="${tomcat.home}/shared/lib">          <include name="*.jar"/>        </fileset>        <fileset dir="${tomcat.home}/bin">           <include name="*.jar"/>         </fileset>       </classpath>      <include name="**" />      <exclude name="tags/**" />    </javac>  </target>  <target name="all" depends="jspc,compile">  </target>  <target name="cleanup">  <delete>        <fileset dir="${webapp.path}/WEB-INF/src"/>        <fileset dir="${webapp.path}/WEB-INF/classes/org/apache/jsp"/>  </delete>  </target></project>

 

下面的命令行可以用来运行这个script(用Tomcat基础路径和指向应该被预编译的webapp的路径来代替那些记号)。

$ANT_HOME/bin/ant -Dtomcat.home=<$TOMCAT_HOME> -Dwebapp.path=<$WEBAPP_PATH>

 

然后,对于在预编译过程中产生的servlets的声明和映射必须被加入到网络程序部署描述符里去。把${webapp.path}/WEB-INF/generated_web.xml插入到${webapp.path}/WEB-INF/web.xml文件应该被放置的地方。重新启动web程序(使用the manager),并测试它以验证预编译过的servlets能正常运行。网络程序部署描述符里适当的标记(token)也可以被用来通过Ant过滤功能自动插入所产生servlets声明和映射。事实上这就是Tomcat发布的所有webapps作为build过程的一部分是怎样自动被编译的。

At the jasper2 task you can use the option addWebXmlMappings for automatic merge the ${webapp.path}/WEB-INF/generated_web.xml with the current web application deployment descriptor at ${webapp.path}/WEB-INF/web.xml. When you want to use Java 5 feature inside your jsp's, add the following javac compiler task attributes: source="1.5" target="1.5". For live application you can also compile with optimize="on" and without debug info debug="off".

When you don't want to stop the jsp generation at first jsp syntax error, use failOnError="false"and with showSuccess="true" all successfull jsp to java generation are printed out. Sometimes it is very helpfull, when you cleanup the generate java source files at ${webapp.path}/WEB-INF/src and the compile jsp servlet classes at ${webapp.path}/WEB-INF/classes/org/apache/jsp.

Hints:

  • When you switch to another tomcat release, then regenerate and recompile your jsp's with this version again!
  • Use java system property at server runtime to disable tag pooling org.apache.jasper.runtime.JspFactoryImpl.USE_POOL=false. and limit the buffering with org.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true. Note that changing from the defaults may affect performance, but depending on the application.

 

原创粉丝点击