|
JSPTemplates | |||||||||
PREV NEXT | FRAMES NO FRAMES |
"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) 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);
parser.jar
, and jaxp.jar
and servelt.jar
lib
directory of the JSPTemplate installationjsp
directory and a classes
directory
for JSP's and the auto generated code/bytecodes.work
directoryclasses
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
.
org.tp23.jasper.MyJasper
object passing the
two directories created as strings (local filesystem naming conventions
apply) MyJasper engine = new MyJasper("C:\\work\\jsp","C:\\work\\classes",true);
// true = create session
this method does not easily let you add to the MyJasper classpath.myjasper.properties
from myjasper.jar or create a
new text file and in it, put the following linesmyjasper.jsp.dir=
{jsp directory from 3.} myjasper.class.dir=
{classes directory from 3.}myjasper.config.dir=
{jsp directory from 3.} myjasper.config.cache=false
myjasper.classpath={classes directory from 3.}; %TOMCAT_HOME%\\lib\\servlet.jar;
%TOMCAT_HOME%\\lib\\jaxp.jar; %TOMCAT_HOME%\\lib\\parser.jar; c:\\jdk1.3\\lib\\tools.jar
myjapser.properties
file back inside the
myjasper.jar
file that contains the classes.org.tp23.jasper.MyJasper
object with no parameters
MyJasper engine = new MyJasper();
engine.getOutput("testJSP.jsp");
getOutput()
that is analoguos to the
request parameters but it is able to hold Objects not just Strings.
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 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().
<% for( int i=0 ; i<10 ; i++){ %> <%="Hello World"%> <% } %>
<%@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 | |||||||||
PREV NEXT | FRAMES NO FRAMES |