I want to pre-compile ALL JSPs in my application

A

Alex Vassiliev

Hi

Is there any simple way to tell Tomcat to precompile _all_ JSPs in my
application? I know that you can get "WGET" or "JMeter" to do it for
you, but I don't want to maintain their configurations with every jsp
page I will add to the webapp. Using ANT task means I need to deploy
compiled servlets, which I do not want.

Thanks
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

Hi
Is there any simple way to tell Tomcat to precompile _all_ JSPs in my
application? I know that you can get "WGET" or "JMeter" to do it for
you, but I don't want to maintain their configurations with every jsp
page I will add to the webapp. Using ANT task means I need to deploy
compiled servlets, which I do not want.
look at the jspc tool shipped with tomcat
 
A

Alex Vassiliev

Me again

Here is the solution I've adopted: A special jsp which will compile
all other jsps within the webapp:

-----------------------------------------------------------------------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="javax.servlet.*,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); //
process subfolders
}
}
}
%>

<html>
<head><title>Precompiling *.JSPs</title></head>
<body>
<h4>Precompiling *.JSPs:</h4>
<ul>
<%
HttpServletRequest req = new HttpServletRequestWrapper(request) {
public String getQueryString() {
return "jsp_precompile=true";
};
};

compileAllJsps(pageContext, out, req, response, "/");
%>
</ul>
<h4>Done.</h4>
</body>
</html>
-----------------------------------------------------------------------

It works with Tomcat 4.1.27, and I can't see any reason why it would
not work in any other Servlet 2.3 and JSP 1.2 compatible container...

Alex Vassiliev
POWERWARE (New Zealand)
 
C

Chris Smith

Alex said:
Here is the solution I've adopted: A special jsp which will compile
all other jsps within the webapp:
[...]

It works with Tomcat 4.1.27, and I can't see any reason why it would
not work in any other Servlet 2.3 and JSP 1.2 compatible container...

Note that this will only work if none of those JSPs require any
application state in order to complete normally. Hence, I know it
wouldn't work in my own code, or in pretty much any other non-trivial
application that's designed to fail fast in case of a problem with
application state. Some exception-handling could make the code more
robust.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

Is there any simple way to tell Tomcat to precompile _all_ JSPs in my
application?

A way that would work with any servlet engine is to write a little
script that simulates a browser and just exercises all the code.
 
A

Alex Vassiliev

Chris Smith wrote in message news: said:
Note that this will only work if none of those JSPs require any
application state in order to complete normally. Hence, I know it

Let's have another look at the request object I am passing to the
"include()":

--------------------------------------------------------------------
HttpServletRequest req = new HttpServletRequestWrapper(request) {
public String getQueryString() {
return "jsp_precompile=true";
};
};
--------------------------------------------------------------------

It is a wrapper around the original request. It should behave like the
original request, but with "?jsp_precompile=true" parameter added. As
per section 8.4.2 of the JSP 1.2 specification it is a "precompilation
request". JSPs are compiled but NOT executed! And that's exactly
what we need... ;-)


The only potential problem is that I forgot to override
getProperty(name) method (and probably others)... It is not an issue
for Tomcat, but other containers might have problems. More robust
variant would be something like this:

--------------------------------------------------------------------
HttpServletRequest req = new HttpServletRequestWrapper(request) {
public String getQueryString() {
return "jsp_precompile=true";
};
public String getParameter(String name) {
return "jsp_precompile".equal(name) ? "true" : null;
};
};
 
C

Chris Smith

Alex said:
Let's have another look at the request object I am passing to the
"include()":

--------------------------------------------------------------------
HttpServletRequest req = new HttpServletRequestWrapper(request) {
public String getQueryString() {
return "jsp_precompile=true";
};
};
--------------------------------------------------------------------

It is a wrapper around the original request. It should behave like the
original request, but with "?jsp_precompile=true" parameter added. As
per section 8.4.2 of the JSP 1.2 specification it is a "precompilation
request". JSPs are compiled but NOT executed! And that's exactly
what we need... ;-)

Ah, okay. Missed that bit. Sorry.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Alex Vassiliev

Roedy Green said:
A way that would work with any servlet engine is to write a little
script that simulates a browser and just exercises all the code.

That is a very good approach and you can test different aspects of
your application as well, but it requires fair bit of maintenance...
:( I've looked at apache-jakarta JMeter to do that, but given the
timeframe I've got - it is not an option...

Thanks anyway,
Alex Vassiliev
POWERWARE (New Zealand)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top