Installation Docs
API Docs


JSPTemplates (MyJasper)

"This product includes software developed by the Apache Software Foundation (http://www.apache.org/)."

JSPTemplates(MyJasper) allows java developers to use the same mechanism JSP uses to generate web content, to generate Strings.
Basically this allows you to write JSPs that don't contain HTML and are never delivered over the web. The result of the JSP is returned as a String.
This will be handy for generating email responses or text output files that require some programming to generate the strings.
This is a (quick and dirty) port of the apache tomcat JSP compilation context and runtime.

JSPTemplates can also be used to generate Static HTML files like many other template engines but with all the versitility of JSP's.

Example of use, this uses a JSP to generate an email message. The JSP could reference a JDBC database to generate its output, or simly place details from the user object in the right places in the message. see Example JSP

  // JSPTemplates imports
  import org.tp23.jasper.*;
  import org.tp23.jsp.*;

  //  java.mail stuff

  MimeMessage emailMessage = new MimeMessage(session);
  emailMessage.addRecipients(Message.RecipientType.BCC, toAddresses);

  // MyJasper stuff
  Engine engine = new MyJasper();
  Map parameters = new HashMap();
  emailMessage.setText( engine.getOutput("standardMessage.jsp",parameters) );



  1. Download Tomcat 3.2 and extract it making a note of where the Jar files are placed. usually %TOMCAT_HOME%/lib
    Look for parser.jar, and jaxp.jar and servlet.jar
  2. Compile the source
    (you will notice that javax.servlet is needed I have not totally cleaned the apache classes of javax references (I think I may never do so since taglib is handy). Store the classes into a JAR called myjasper.jar
    find the myjasper.jar JAR in the lib directory of the JSPTemplate installation
  3. Set up a jsp directory and a classes directory for JSP's and the resulting code/bytecodes.
    The directories can have any names as long as the path does not have & characterss or other funny characters that require escaping on a url.
    If that did not make sense try this: 'use a path made up of letters and numbers only'. For the test application these directories are in the work directory
  4. Include the classes directory in the classpath. Use what ever classpath method you like, either %CLASSPATH% or -classpath in a batch file, or anything that sets the System property java.class.path.
    Also include...
    servlet.jar - servlet/jsp api from SUN
    jaxp.jap - xml parsers api
    parser.jap - xml parser
    tools.jar - sun java compiler included with jdk1.3
    • load the class without a .properties file
      Instantiate an org.tp23.jasper.MyJasper object passing the two directories created as strings (local filesystem naming conventions apply)
      e.g. MyJasper engine = new MyJasper("C:\\work\\jsp","C:\\work\\classes",true); // true = create session

    • load the class with a .properties file
      Extract myjasper.properties from myjasper.jar or create a new text file and in it, put the following

      myjasper.jsp.dir={jsp directory from 3.}
      myjasper.class.dir={classes directory from 3.}
      myjasper.config.dir={jsp directory from 3.}
      myjasper.classpath={classes directory from 3.}; {tomcat home}\\lib\\servlet.jar; {tomcat home}\\lib\\jaxp.jar; {tomcat home}\\lib\\parser.jar; {jdk home}\\lib\\tools.jar

      You will need to ensure the class path is correct, and note the use of double forward slashes in Windows. UNIX will only require single backslashes.
      place the edited myjapser.properties file back (you can use winzip) inside the myjasper.jar file that contains the classes.
      Instantiate a org.tp23.jasper.MyJasper object with no parameters
      e.g. MyJasper engine = new MyJasper();
      The classpath supplied as a properties is the compiler classpath; this is different from the runtime classpath. The compiler classpath currently needs the correct servlet.jar that came with tomcat3.2. The generated classes run in the context of the JVM that instantiated MyJasper and use that JVM's classpath. This means that MyJasper can be used inside a different version of tomcat if the JSP's do not call any method in the servlet API. If you wish to use classes from the standard javax.servlet API they must be explicitly included in the JSP. This may lead to a conflict, if compiling with early servlet.jar breaks running in a later JSP engine. In the future MyJaspers requirement to include servlet.jar will be removed, this can be considered a bug if you want to fix it.

  5. To get the contents of the JSP page call engine.getOutput("testJSP.jsp");
    N.B. a normal JSP may not compile some changes are needed for JSPTemplates see WRITING JSP's

Notes about the engine.

As with tomcat the first time you call it it compiles and loads and thus takes ages, from then on the class file exists and is in momory.
If the JVM ends the class is still there for next time.
If you change the original JSP the compilation happens again.
Reloading happens only after the Engine is shutdown, reloading is not supported yet.
A Map file can be passed to getOutput() that is analoguos to the request parameters but it is able to hold Objects not just Strings.
Similarly a session can be passed and then normal jsp session access can be used so this is the preferred mechanism for passing objects to a JSP. The compiler generates classes in the default package if the JSP is in the root JSP directory.
A SiteCompiler org.tp23.site.SiteC is included that illustrates how to use MyJasper as a templates engine for generating static websites.


Basically the changes made to normal the JSP definition is to remove references to HTTP.
A couple of classes have been changed, so normal JSP's may not compile.
The following classes

have been replaced with
which performs similar functions to both.
When writing JSP's for MyJasper remember the request and response variables are not there use destination instead.
for example
request.getRequestDispatcher(String jsp).forward(request,response);
has been replaced by
destination.forward(String jsp);
The session variable is still there but its class has changed, it now implementsorg.tp23.jsp.Session It works the same as the HttpSession except
a) it never expires (it gets garbage collected normally when the MyJasper object does)
b) it is available to the code that calls the JSP as well as the JSP thus you can call engine.getSession().setAttribute() to set values and in the JSP session.getAttribute() to get them
c) there is only one session per server. I use server is the most vauge sense remember there is nothing to stop you instantiating many MyJasper's in the same JVM. It may be best to keep a singleton MyJasper (although it is not inforced). If you don't separate threads will have separate sessions this may be good or bad but it is definately different.
Obviously there is no clustering... ;(
The main session is accessible to all calls to JSP's if you want individual sessions per JSP just create a HashSession and pass it to a Destination object that you can pass to engine.getOutput(). If you do this the main session is not passed to the JSP but it is not overwitten.


Simple example MyJasper JSP

  <% for( int i=0 ; i<10 ; i++){ %>

  <%="Hello World\n"%>

  <% } %>

Example plain text MyJasper JSP

<%@page import="com.tp23.stuff.*"%><%

UserObject user = (UserObject)destination.getParameter("user");
ArrayList  outstandingOrders = user.getCurrentOrders();


Dear <%=user.getName()%>

   Thank you for contacting MyCo.com.

   <%=user.getSalesRep()%> will be contacting you shortly to try to help you further,

   If your contact is regarding the following orders rest assured they are being processed.

<% for(int i=0;i<outstandingOrders.size();i++){ %>
<% } %>

   Thank you for your continued custom.

<% if (user.getTurnover() > 10000000 ) { %>

   Here is the CEOs personal number 07982 234534

<% } %>