My first Jython web app on GlassFish v3
I have been thinking of playing with Jython and GlassFish for quite some time.
I thought of taking the first shot at it. So after looking around a bit I found
out that I can simply use PyServlet to delegate the HTTP requests to my Jython
servlet which can do whatever with the Python script and write it back to the
Servlet output stream. Let us see how I did it:
Setup
- Install href="http://download.java.net/javaee5/v3/releases/preview/glassfish-snapshot-v3-preview-26_03_2008.zip">GlassFish v3.
-
Install Jython . The current
version I got is Jython 2.2.1.
src="http://weblogs.java.net/blog/vivekp/archive/images/jythonv.png"
width="581" height="60" />
Create a Python Servlet
-
Create the servlet layout:
cd c:/dev
mkdir calendar
cd calendar
mkdir WEB-INF
mkdir WEB-INF/libNow, create Show.py, which is a Python Servlet and save it at c:/dev/calendar:
from javax.servlet.http import HttpServlet
class Show (HttpServlet):
def doGet(self,request,response):
self.doPost (request,response)
def doPost(self,request,response):
toClient = response.getWriter()
response.setContentType ("text/html")
toClient.println ("<body><h1>Calendar</h1><pre>No
Calendar</pre></body></html>") -
Create web.xml to serve all the python files by PyServlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name><b>PyServlet</b></servlet-name>
<servlet-class><b>org.python.util.PyServlet</b></servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern><b>*.py</b></url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app> -
Copy jython.jar and python libraries to WEB-INF/lib. The Python
libraries are located under Lib directory of Jython installation
directory. You will also find jython.jar under the Jython installation
directory.
cd c:/tools/jython
cp jython.jar c:/dev/calendar/WEB-INF/lib
cp -rf Lib c:/dev/calendar/WEB-INF/lib
Deploy the Python web application
-
Start GlassFish v3
[/c/dev]
$./glassfish/bin/asadmin deploy calendar
properties=(name=calendar)
Now access http://localhost:8080/calendar/Show.py, it should show this
page:
src="http://weblogs.java.net/blog/vivekp/archive/images/cal1.png" width="537"
height="327" />
This was simply a jython servlet but there was no Python specific libraries
involved.
Invoke Python libraries
We will show Year 2008 calendar using python class calendar.
Let us modify Show.py.
<b>import sys,calendar,time </b>
from javax.servlet.http import HttpServlet
class Show (HttpServlet):
def doGet(self,request,response):
self.doPost (request,response)
def doPost(self,request,response):
toClient = response.getWriter()
response.setContentType ("text/html")
<b>toClient.println
("<body><h1>Calendar</h1><pre>%s</pre></body></html>" %
calendar.calendar(time.localtime()[0]))</b>Notice that the main change made to Show.py is that we invoke
calendar.calendar() and pass it the localtime. Also, there is an import of
calendar and time.
Now, go back to your browser and refresh the page and you would see Year 2008
calendar:
src="http://weblogs.java.net/blog/vivekp/archive/images/cal2.png" width="630"
height="556" />
This is it! However it involves Jython Servlet and some wiring and could be
lot simpler. Frank plans to work
on simplified and better integrated support for Jython on GlassFish. Stay tuned
there is more to come...
- Login or register to post comments
- Printer-friendly version
- vivekp's blog
- 4347 reads






Comments
by nocturnal - 2008-11-13 05:01
Apparently this is not as straightforward as it looks. Well what i did was this ( my WEB-INF looks like this) -WEB-INF/ Lib jython.jar and now it's working. If you copy all the files from Lib to WEB-INF/lib than it doesn't work and also the project icon in netbeans turns red ( due to errors).by areplogle - 2008-03-29 10:21
using gfv3 and following these instructions, I also get calendar module not found. I installed jython 2.2.1 ----- type Exception report message descriptionThe server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: Traceback (innermost last): File "/Users/areplogle/dev/calendar/Show.py", line 2, in ? ImportError: no module named calendar root cause Traceback (innermost last): File "/Users/areplogle/dev/calendar/Show.py", line 2, in ? ImportError: no module named calendar note The full stack traces of the exception and its root causes are available in the GlassFish/v3 logs. ----------- I then tried copying all of jython/lib to my WEB-INF/lib and restarted. No success. Just curious if anyone may know what I'm doing wrong? Andrewby vivekp - 2008-04-03 11:33
Andrew, The only thing I can think of is maybe you copied the Lib from jythong installation after starting v3. Did you try restarting v3?by vivekp - 2008-03-28 11:01
nocturnal,I think there are more improvements needed in this area, it would be nice if the PyServlet dynamically loads python libs. With GlassFish v3, if done correctly we can do these things thru Sniffer where such dynamically loaded modules are taken care. For now, if you are using GlassFish v3, you would 'asadmin redeploy calendar' or undeploy and deploy.
by nocturnal - 2008-03-28 08:16
After copying jython/Lib dir to WEB-INF/lib it was giving error module calendar not found. The problem got fixed after I restarted my tomcat server. Also check out http://wiki.python.org/jython/JythonMonthly/Articles/October2006/2by brunogh - 2008-03-28 06:40
Very very nice!!!by nocturnal - 2008-03-27 20:36
Wow ! looks cool