|
From: Robert W. B. <rb...@di...> - 2000-12-22 03:44:15
|
Hello,
On Thu, 21 Dec 2000, Ray Leyva wrote:
> Just wondering. Cause we're building a site based solely on Java, and
> JSP. I was wondering if it was possible to use Jython from with JSP,
> and if so can someone send a really really simple ( rudimentary )
> FooBar, World! style sample.
>
> thx
> rl
I'm been looking at this as well. So far, I'm unable to use a jythonc
compiled file with Tomcat jsp due to a StackOverflow in __init__ or other
problems loading the inner class. I think I'm missing something though.
What is sure to work is embedding a PythonInterpreter. It can go right in
the scriptlet sections, and you can exec python code, or execfile to fill
larger areas of the page from python files. Here's a brain-dead example
that just uses time from PythonInterpreter in a JSP page to get you
started. Make sure to add the jython.jar to the classpath and possibly
add -Dpython.home=/path/to/jythondir in the container's startup script.
(This example was tested on Jakarta Tomcat 3.2)
------------begin time.jsp page---------------------------
<%@ page import = "org.python.util.PythonInterpreter" %>
<%@ page import = "org.python.core.*" %>
<html>
<head><title>hello</title></head>
<body bgcolor="white">
<font size=4>
Hello everyone, it is currently
<% PythonInterpreter interp = new PythonInterpreter();
interp.exec("from time import ctime, time");
interp.exec("t = ctime(time())");
String t = interp.get("t").toString(); %>
<%= t %>
<br>
</font>
</body>
</html>
-------------end time.jsp page----------------------------
You could also write Java files that use PythonInterpreter, and in turn,
use them within the JSP files. I found myself using PythonInterpreter so
much that I just made a Java class with a runPyFile and runPyCode methods
that respectively set up a JySP directory to run files from,
and exec strings of Jython code. Both of which just return a String for
the JSP.
It seems likely that jythonc compiled classes should work with JSP- when I
figure out what I'm doing wrong, I'll forward more examples.
Hopefully more to come someday soon :)
-Robert
|