A different twist on pre-compiling JSPs--reference
I’ve blogged about this topic earlier and expressed my frustrations as to how web containers don’t provide good support for precompiling JSP, with the exception ofWebLogic. As I’ve said before, WebLogic offers great support for pre-compiling JSPby adding a few simple snippets inside the weblogic.xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN"
"http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">
<weblogic-web-app>
<jsp-descriptor>
<jsp-param>
<param-name>compileCommand</param-name>
<param-value>javac</param-value>
</jsp-param>
<jsp-param>
<param-name>precompile</param-name>
<param-value>true</param-value>
</jsp-param>
<jsp-param>
<param-name>workingDir</param-name>
<param-value>./precompile/myapp</param-value>
</jsp-param>
<jsp-param>
<param-name>keepgenerated</param-name>
<param-value>true</param-value>
</jsp-param>
</jsp-descriptor>
</weblogic-web-app>
As you can see from the snippet of XML above, all you have to do is pass in a parameter called precompile and set it to true. You can set additional attributes like compiler (jikes or javac), package for generated source code and classes, etc. Check out the BEA documentation for more information on the additional parameters.
Now Tomcat or Jetty doesn’t support this type of functionality directly. Tomcat has a great document on how to use Ant and the JavaServer Page compiler, JSPC. The process involves setting up a task in Ant, and then running org.apache.jasper.JspC on the JSP pages to generate all the classes for the JSP pages. Once the JSP’s are compiled, you have to modify the web.xml to include a servlet mapping for each of the compiled servlet. Yuck! I know this works, but it’s just so ugly and such a hack. I like the WebLogic’s declarative way of doing this – Just specify a parameter in an XML file and your JSP’s are compiled at deploy time.
I didn’t want to use the Ant task to precompile as I thought I was just hacking ant to do what I need to do. And if I am going to just hack it, I’m going to create my own hack.As I started to think about how to go about doing this, I came across the rarely mentioned ‘Precompilation Protocol’. The ‘Precompilation Protocol’ is part of the JSP 2.0 specification (JSR 152) and it says that any request to a JSP page that has a request parameter with name jsp_precompile is a precompilation request. The jsp_precompile parameter may have no value, or may have values true or false. In all cases, the request should not be delivered to the JSP page. Instead, the intention of the precompilation request is that of a suggestion to the JSP container to precompile the JSP page into its JSP page implementation class. So requests like http://localhost:8080/myapp/index.jsp?jsp_precompile or http://localhost:8080/myapp/index.jsp?jsp_precompile=true or http://localhost:8080/myapp/index.jsp?jsp_precompile=false or http://localhost:8080/myapp/index.jsp?data=xyz&jsp_precompile=true are all valid requests. Anything other than true, false or nothing passed into jsp_precompile will get you an HTTP 500. This is a great feature and gave me an idea on how to precompile my JSP’s.
Taking advantage of Precompilation Protocol, I wrote a simple Servlet that was loaded at startup via a attribute in the web.xml. Once the web application is deployed, this servlet would connect to each of the JSP. I also decided to stuff in the details my servlet is going to need to precompile the JSP’s. Here’s the web.xml that defines my PreCompile servlet and all the other attributes it needs. Instead of using the web.xml, I could have use a property file or a -D parameter from the command line but this was just a proof-of-concept and so I used the web.xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>preCompile</servlet-name>
<servlet-class>com.j2eegeek.servlet.util.PreCompileServlet</servlet-class>
<init-param>
<param-name>jsp.delimiter</param-name>
<param-value>;</param-value>
</init-param>
<init-param>
<param-name>jsp.file.list</param-name>
<param-value>index.jsp;login.jsp;mainmenu.jsp;include/stuff.jsp….</param-value>
</init-param>
<init-param>
<param-name>jsp.server.url</param-name>
<param-value>http://localhost:8080/myapp/</param-value>
</init-param>
<load-on-startup>9</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>preCompile</servlet-name>
<url-pattern>/preCompile</url-pattern>
</servlet-mapping>
</web-app>
The web.xml is pretty simple and just defines the PreCompile servlet. The PreCompile servlet is a simple servlet that takes advantage of the servlet lifecycle and uses the init() method of the servlet to connect to each JSP individually and precompile them. In addition to the init() method, we are also defining that the servlet be loaded at the web application deploy time. Here’s a little snippet from the servlet that show the precompilation of the JSPs.
/**
* Called by the servlet container to indicate to a servlet that
* the servlet is being placed into service.
*
* @param javax.servlet.ServletConfig config
* @throws javax.servlet.ServletException ServletException
*/
publicvoid init(ServletConfig config) throws ServletException {
log.info("Starting init() in " + CLASS_NAME);
String server_url = config.getInitParameter(SERVER_PREFIX);
String jsp_delim = config.getInitParameter(JSP_DELIMITER);
String jsps = config.getInitParameter(JSP_FILE_LIST);
if ((jsps != null) && (StringUtils.isNotBlank(jsps))) {
StringTokenizer st = new StringTokenizer(jsps, jsp_delim);
while (st.hasMoreTokens()) {
String jsp = st.nextToken();
log.info("Starting precompile for " + jsp);
connect(server_url + jsp + JSP_PRECOMPILE_DIRECTIVE);
}
log.info("Precompiling JSP’s complete");
}
}
In digging into the JSP 2.0 specification, I learned that I can also use the sub-element in conjunction with a JSP page. Another interesting approach to solve this pre-compile issue.
Here’s the fully commented servlet (HTML | Java) that does the precompiling. I currently use this as a standalone war that’s deployed after your main application. You can also ‘touch’ the precompile.war file to reload the PreCompile servlet and precompile an application. I whipped this up just to prove a point and don’t really recommend using this in production, even though I am currently doing that. I am going to send feedback to the JSP 2.0 Expert Group and ask them to look into making precompiling a standard and mandatory option that can be described in the web.xml. If you think this is an issue as well, I’d recommend you do the same. Drop me an email or a comment if you think I am on the right track or completely off my rocker.
reference from:http://www.j2eegeek.com/2004/05/03/a-different-twist-on-pre-compiling-jsps/
A different twist on pre-compiling JSPs--reference的更多相关文章
- Compiling JSPs Using the Command-Line Compiler---官方
Web Server provides the following ways of compiling JSP 2.1-compliant source files into servlets: JS ...
- adpatch options=hotpatch
--no need to shutdown application and no need to enable maintenance mode adpatch options=hotpatch fi ...
- XML Publisher Template Type - Microsoft Excel Patch
XML Publisher Template Type - Microsoft Excel Patch Oracle XML Publisher > Templates > Create ...
- ROC曲线和PR曲线
转自:http://www.zhizhihu.com/html/y2012/4076.html分类.检索中的评价指标很多,Precision.Recall.Accuracy.F1.ROC.PR Cur ...
- LeetCode_Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Deploying JAR Package & JSP Page in EBS R12.2.4 WLS
https://pan.baidu.com/s/1OomyeLdbGWxTtCKVcweo0w # Uninstall JAR JSP QRCODE 1.# 查找QRCODE相关文件位置 [root@ ...
- [HDOJ1232]畅通工程(并查集)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1232 题目描述 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表, ...
- Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide
Preface Validating data is a common task that occurs throughout all application layers, from the pre ...
- Sphinx 2.2.11-release reference manual
1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
随机推荐
- 正确安装 django-socketio
直接使用 pip 安装,连 example project 都运行不了... 要正常使用,关键是要使用正确版本的依赖包 Django (1.5.5) django-socketio (0.3.2) g ...
- Django如何设置proxy
设置porxy的原因 一般情况下我们代理设置是针对与浏览器而言,通常只需在浏览器设置中进行配置,但它只针对浏览器有效,对我们自己编写的程序并任何效果,这时就需要我们在软件编码中加入代理设置. --- ...
- POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- WPF Canvas小例子
源码下载:DraggingElementsInCanvas_demo.rar
- Android模拟器常用命令收录
一.Linux命令 1.挂载/systme分区为读写状态 mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system 2.切换为Root用户 ...
- BZOJ 3969 low power
Description 有\(n\)个机器,每个机器有\(2\)个芯片,每个芯片可以放\(k\)个电池.每个芯片能量是\(k\)个电池的能量的最小值.两个芯片的能量之差越小,这个机器就工作的越好.现在 ...
- BZOJ 3563 DZY Loves Chinese
Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上 ...
- iOS内存管理系列之一:对象所有权与引用计数
当一个所有者(owner,其本身可以是任何一个Objective-C对象)做了以下某个动作时,它拥有对一个对象的所有权(ownership): 1. 创建一个对象.包括使用任何名称中包含“alloc” ...
- Python3.x和Python2.x的区别-转
这个星期开始学习Python了,因为看的书都是基于Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下3.x和 ...
- mysql主从配置(清晰的思路)
mysql主从配置.鄙人是在如下环境测试的: 主数据库所在的操作系统:win7 主数据库的版本:5.0 主数据库的ip地址:192.168.1.111 从数据库所在的操作系统:linux 从数据的版本 ...