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/

时间: 2024-10-06 07:57:39

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: JSP are automatically compiled at runtime. The jspc command-line tool, described in this section, enables you to precompile JSPs at the command line. Yo

adpatch options=hotpatch

--no need to shutdown application and no need to enable maintenance mode find . -name FAS420.rdf -bash-3.2$ strings -a ./fa/12.0.0/reports/US/FAS420.rdf|grep '$Header' SQL> select bug_id,application_short_name from ad_bugs where bug_number='9287896';

Plus One Linked List

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 解法一:reverse /** *

Deploying JAR Package &amp; JSP Page in EBS R12.2.4 WLS

2018年5月1日 10:05 # Uninstall JAR JSP QRCODE 1.# 查找QRCODE相关文件位置 [[email protected] ~]# find /u01/ -name '*qrcode*' /u01/install/VISION/fs1/FMW_Home/Oracle_EBS-app1/applications/oacore/html/WEB-INF/classes/_pages/__getqrcode.class /u01/install/VISION/fs

[HDOJ1232]畅通工程(并查集)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1232 题目描述 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可).问最少还需要建设多少条道路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是城镇数目N ( <

undefined reference to _imp__xmlFree

Re: [xml] MSYS and MINGW: undefined reference to _imp__xmlFree From: Mike Peat <mpeat unicorninterglobal com> To: danielg teragram com Cc: "Daniel Richard G." <oss teragram com>, xml gnome org Subject: Re: [xml] MSYS and MINGW: undef

Compiling GCC 5 on OS X

*/--> pre.src {background-color: Black; color: White;} Compiling GCC 5 on OS X Table of Contents 准备工作 gmp mpfr mpc isl gcc 原文: https://solarianprogrammer.com/2015/05/01/compiling-gcc-5-mac-os-x/ 准备工作 go to: http://gcc.gnu.org/mirrors.htm downloading

java中的4种reference的差别和使用场景(含理论、代码和执行结果)

我们知道java语言提供了4种引用类型:强引用.软引用(SoftReference).弱引用(WeakReference)和幽灵引用(PhantomReference),与引用密切相关的,还有一个引用队列ReferenceQueue.引用和引用队列的关系,对于垃圾回收来说非常重要,学习垃圾回收机制,必须要先了解引用和引用队列的使用方法.本文主要参考网上的一些理论,同时配合自己的一些测试代码,更好的理解这些概念.这篇博客也解决了 System.gc()和-XX:+DisableExplicitGC

A const field of a reference type other than string can only be initialized with null Error [duplicate]

I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveIndices = new int[,] { {200,362},{250,370},{213,410} , {400,330} , {380,282} , {437, 295} , {325, 405} , {379,413} ,{343,453} , {450,382},{510,395},{46