JSPTemplates

JSPTemplates

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

See:
          Description

Packages
org.tp23.jasper  
org.tp23.jasper.compiler This package contains code that compiles the JSP pages.
org.tp23.jasper.runtime This package contains code that the JSP runtime uses.
org.tp23.jasper.servlet This package contains code that is mostly specific to running the JSP compiler in a webcontext.
org.tp23.jsp This package contains interfaces and exceptions to abstract the compilation from accessing the compiled results.

 

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

JSPTemplates (MyJasper)

 

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.addFrom(fromAddresses);
  emailMessage.addRecipients(Message.RecipientType.BCC, toAddresses);
  emailMessage.setSubject(subject);

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

  SMTPTransport.send(emailMessage);
  



INSTALLATION

  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 servelt.jar
  2. Compile the JSPTemplates source in the IDE of your choice or from the command line.
    (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
    OR
    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 auto generated code/bytecodes.
    The directories can have any names as long as the path does not have & characters 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 subdirectories 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 - java java compilers
  5. To get the contents of the JSP page call engine.getOutput("testJSP.jsp");
    N.B. a normal JSP may not compile some changes may be needed see WRITING JSP's

Notes about the engine.



WRITING JSP's

Basically the changes made to normal JSP is to remove references to HTTP.
A couple of classes have been rehashed , so normal JSP's will not compile.
The replaced classes are
javax.servlet.http.ServletRequest
javax.servlet.http.ServletResponse

replaced with
org.tp23.jsp.Destination
which performs similar functions to both.
When writing JSP's for MyJasper remember the request and response variables are not there use destination instead.
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 is now a org.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. The term server is used in 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 thread 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 and it is not erased for other calls to getOutput().


EXAMPLES

Simple example MyJasper JSP


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

  <%="Hello World"%>

  <% } %>

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++){ %>
   order:<%=outstandingOrders.get(i)%>
<% } %>

   Thank you for your continued custom.


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

   Here is the CEOs personal number 07982 234534

<% } %>




JSPTemplates