加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

A different twist on pre-compiling JSPs--reference

发布时间:2020-12-14 06:20:14 所属栏目:Java 来源:网络整理
导读: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 of. As I’ve said before,WebLogic offers?by adding a few simple snippets insi

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 of. As I’ve said before,WebLogic offers?by adding a few simple snippets inside the weblogic.xml file.

"1.0" encoding="UTF-8" ?>

weblogic-web-appjsp-descriptorjsp-paramparam-namecompileCommandparam-nameparam-valuejavacparam-valuejsp-paramjsp-paramparam-nameprecompileparam-nameparam-valuetrueparam-valuejsp-paramjsp-paramparam-nameworkingDirparam-nameparam-value./precompile/myappparam-valuejsp-paramjsp-paramparam-namekeepgeneratedparam-nameparam-valuetrueparam-valuejsp-paramjsp-descriptorweblogic-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??on the additional parameters.

Now Tomcat or Jetty doesn’t support this type of functionality directly. Tomcat has??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.

:)??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.

"1.0" encoding="ISO-8859-1"?>

web-app

servletservlet-namepreCompileservlet-nameservlet-classcom.j2eegeek.servlet.util.PreCompileServletservlet-classinit-paramparam-namejsp.delimiterparam-nameparam-value;param-valueinit-paraminit-paramparam-namejsp.file.listparam-nameparam-valueindex.jsp;login.jsp;mainmenu.jsp;include/stuff.jsp….param-valueinit-paraminit-paramparam-namejsp.server.urlparam-nameparam-valuehttp://localhost:8080/myapp/param-valueinit-paramload-on-startup9load-on-startupservlet

servlet-mappingservlet-namepreCompileservlet-nameurl-pattern/preCompileurl-patternservlet-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 */publicvoidthrows

"Starting init() in "

ifnullnewwhile"Starting precompile for ""Precompiling JSP’s complete"

In digging into the?,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 (?|?) 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??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/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读