实战jsp预编译,充分享受jsp预编译的好处!

来源:互联网 发布:淘宝运动服装男女 编辑:程序博客网 时间:2024/05/18 00:46

jsp预编译有以下好处:

1.省去第一次运行jsp时的编译所花费的时间,实现servlet一样,一步到位的运行。

2.有效的保护源代码,在产品发布的时候只需要提供依据编译好的class文件,不需要提供jsp源文件,对保护jsp源代码有好处,虽然class文件没有混淆,但是很少人愿意手工去把预编译jsp产生的class文件还原为jsp(目前还没发现有自动还原为jsp的工具)。

但是,也注意,如果为了享受第二个好处,那么也就注意,你需要为不同的servlet容器进行不同的预编译。

比如tomcat的是org.apache.jasper.JspC,而Weblogic的是weblogic.jspc

用Tomcat进行预编译的ant脚本如下:

build.properties的内容为:

tomcat.home=D:/jakarta-tomcat-5.5.9
webapp.path=E:/lizongbo/mywebapp

build.xml的内容为:

<project name="Webapp Precompilation" default="all" basedir="."> 
<property file="build.properties"/>
  <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 javaEncoding="UTF-8"
             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="of"
           encoding="UTF-8"
           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>
<jar jarfile="${webapp.path}/WEB-INF/lib/lizongbo.jar" basedir="${webapp.path}/WEB-INF/classes"/>
  </target>
  <target name="all" depends="jspc,compile">
  </target>

</project>

只需要设置好ant的path环境变量,然后修改build.properties。执行ant命令即可。

生成好的jar文件是lizongbo.jar。

在做为产品发布的时候,还需要清除掉${webapp.path}/WEB-INF/classes下的class文件,并且把

${webapp.path}/WEB-INF/generated_web.xml里的servlet映射,添加到${webapp.path}/WEB-INF/web.xml中。

然后就可以删除所有预编过的jsp了。

Tomcat 5.5.9中的admin 模块就是通过jsp预编译打包为catalina-admin.jar的。

D:\jakarta-tomcat-5.5.9\server\webapps\下的几个web应用,都是使用了jsp预编译。

http://www.apache.org/dist/jakarta/tomcat-5/v5.5.9/bin/jakarta-tomcat-5.5.9-admin.zip

参考以下连接,将对你可能遇到的问题有帮助:

遇到 "unmappable character for encoding GBK" 的解决。


http://www.donews.net/lizongbo/archive/2005/05/14/379810.aspx

Jasper 2 JSP Engine How To

Web Application Compilation:

http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jasper-howto.html

jspc Task:

http://ant.apache.org/manual/OptionalTasks/jspc.html

weblogic中使用ant预编译jsp文件

http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=81&threadID=17465&messageID=136608

http://www.knowsky.com/2881.html


1条评论

  1. optimize="of"

    ==>

    optimize="off"