jsp预编译有如下好处:html
1.省去第一次运行jsp时的编译所花费的时间,实现servlet同样,一步到位的运行。java
2.有效的保护源代码,在产品发布的时候只须要提供依据编译好的class文件,不须要提供jsp源文件,对保护jsp源代码有好处,虽然class文件没有混淆,可是不多人愿意手工去把预编译jsp产生的class文件还原为jsp(目前还没发现有自动还原为jsp的工具)。web
可是,也注意,若是为了享受第二个好处,那么也就注意,你须要为不一样的servlet容器进行不一样的预编译。apache
好比tomcat的是org.apache.jasper.JspC,而Weblogic的是weblogic.jspctomcat
用Tomcat进行预编译的ant脚本以下:app
build.properties的内容为:webapp
tomcat.home=D:/jakarta-tomcat-5.5.9
webapp.path=E:/lizongbo/mywebappjsp
build.xml的内容为:工具
<project name="Webapp Precompilation" default="all" basedir=".">
<property file="build.properties"/>
<target name="jspc">ui
<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
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