編寫代碼對全部JSP進行預編譯

一些JSP容器(例如規範中的第8.4.2節點提到的)支持對JSP頁面進行預編譯的能力 爲了預編譯一個JSP頁面, 咱們訪問這個頁面須要使用?jsp_precompile這樣的模式 例如: [url]http://hostname.com/mywebapp/mypage.jsp?jsp_precompile[/url] 這個JSP頁面不會被執行,若是容器支持預編譯,這個jsp頁面將會按照要求被編譯. 下面是一個JSP頁面,這個頁面將會掃描當前目錄(包括子目錄),而且預編譯這些目錄下全部的JSP文件. <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="javax.servlet.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.jsp.*"%> <%@ page import="java.util.Set,java.util.Iterator,java.io.IOException"%> <%! private void compileAllJsps      (PageContext pageContext, JspWriter out,       HttpServletRequest request,HttpServletResponse response,       String uripath)          throws IOException, ServletException {      Set set = pageContext.getServletContext().getResourcePaths(uripath);      for (Iterator iter = set.iterator(); iter.hasNext();) {          String uri = (String) iter.next();          if (uri.endsWith(".jsp")) {              out.write("<li>"+ uri +"</li>");              out.flush();              RequestDispatcher rd =                 getServletContext().getRequestDispatcher(uri);              if (rd == null) {                  throw new Error(uri +" - not found");                  }              rd.include(request, response);          }          else if (uri.endsWith("/")) {              compileAllJsps(pageContext, out, request, response, uri);          }      }    } %> <html><head><title>Precompiling *.JSPs</title></head> <body><h4>Precompiling *.JSPs:</h4> <ul> <%   HttpServletRequest req = new HttpServletRequestWrapper(request) {       public String getQueryString() {           // can be "jsp_precompile=true"           return "jsp_precompile";           };   };   compileAllJsps(pageContext, out, req, response, "/"); %> </ul> <h4>Done.</h4> </body> </html> 主意:不少中間件服務器提供預編譯JSP頁面的工具(如EAServer,BEAWLS等)去檢查一個JSPC命令文件. 轉載請註明本文來自程式先鋒網站 www.javabiz.cn
相關文章
相關標籤/搜索