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/

转载于:https://www.cnblogs.com/davidwang456/p/4156491.html

A different twist on pre-compiling JSPs--reference相关推荐

  1. RHCSA--Vim 文件查看和查找文件

    (1) 应用vi命令在/tmp文件夹下创建文件,文件名newfile.在newfile首行输入日期时间 [root@localhost ~]# date > /tmp/newfile [root ...

  2. 表格中json数据展示

    表格中json数据展示 表格中有的列后端返回的是json格式,可以用 el-popover 来展示 <template slot-scope="scope"><e ...

  3. 解决:Error while compiling statement: FAILED: SemanticException [Error 10007]: Ambiguous column refere

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1.报错: Error while compiling statement: FAILED: Sem ...

  4. Oracle Enqueue Lock Type Reference including 11g new lock

    Oracle Enqueue Lock Type Reference including 11g new lock 内部视图x$ksqst记录了以enqueue type分类的队列管理统计信息(Enq ...

  5. c++ 错误: reference to local variable ‘...’ returned

    当返回函数的零时量的引用的时候就会出现这种情况. 3.cc: In function 'const string& add_(const string&, const string&a ...

  6. Error while compiling statement: FAILED: LockException [Error 10280]

    beeline在创建数据库时碰见报错 Error: Error while compiling statement: FAILED: LockException [Error 10280]: Erro ...

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

    转:https://blog.csdn.net/aitangyong/article/details/39453365 我们知道java语言提供了4种引用类型:强引用.软引用(SoftReferenc ...

  8. Spark 报错 : Error: bad symbolic reference. A signature in SparkContext.class refers to term conf

    报错如下: Error:scalac: Error: bad symbolic reference. A signature in SparkContext.class refers to term ...

  9. Spring Boot Admin Reference Guide

    2019独角兽企业重金招聘Python工程师标准>>> Spring Boot Admin Reference Guide Johannes Edmeier@joshisteVers ...

最新文章

  1. fnv64 mysql,centos7安装搭建rabbitmq
  2. 中国计算机学会论坛上5专家激辩:量子计算机10年内成熟?中美之间还有5-6年差距...
  3. python 编译exe
  4. Docker的安装和使用说明——Docker for Windows
  5. Gym - 101102C
  6. 三维错切变换矩阵_图像的仿射变换
  7. 前端架构设计1:代码核心
  8. 有多少游客被峨眉山的猴子亲过脸?
  9. Git删除本地/本地远程/远程服务器分支
  10. Spring中原型prototype
  11. JDBC上传文件存入BLOB字段
  12. maven下手动导入ojdbc6.jar
  13. 企业信息化战略与实施(2)信息系统生命周期与战略规划方法
  14. android expandablelistview横向,完美实现ExpandableListView二级分栏效果
  15. 在win10系统中批量修改文件名称
  16. 2022年“研究生科研素养提升”系列公益讲座在线测评
  17. 利用自定义注解,统计方法执行时间
  18. 华为中兴为何对未来信心十足?
  19. 接口01_精通Postman接口测试基础应用
  20. 视频转换成gif (知乎)

热门文章

  1. 主页被挟持 火狐浏览器_看过来!关于IE、360浏览器访问学校部分网站的设置说明在这里...
  2. excel图表交互联动_如何使用高大上的多级联动交互式图表来分析人员结构?
  3. visual studio 代码提示插件_请收好:10 个实用的 VS Code 插件
  4. python搭建自动化测试平台_如何用python语言搭建自动化测试环境
  5. android模拟手指滑动,Android Accessibility 模拟界面滑动
  6. 张宁北大计算机系,同是北大出身,差距悬殊!张宁在山西坐冷板凳,祝铭震已坐稳首发...
  7. C++ 命名空间 实战(二)之 直接数组访问迭代器访问
  8. C++虚继承时的构造函数
  9. mysql 建表,解决中文输入
  10. python 栈道实现