 |
My first Jython web app on GlassFish v3
Posted by vivekp on March 26, 2008 at 04:17 PM | Comments (6)
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
Create a Python Servlet
-
Create the servlet layout:
cd c:/dev
mkdir calendar
cd calendar
mkdir WEB-INF
mkdir WEB-INF/lib
Now, 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>PyServlet</servlet-name>
<servlet-class>org.python.util.PyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</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:
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.
import sys,calendar,time
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>%s</pre></body></html>" %
calendar.calendar(time.localtime()[0]))
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:
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...
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Wow ! looks cool
Posted by: nocturnal on March 27, 2008 at 08:36 PM
-
Very very nice!!!
Posted by: brunogh on March 28, 2008 at 06:40 AM
-
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/2
Posted by: nocturnal on March 28, 2008 at 08:16 AM
-
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.
Posted by: vivekp on March 28, 2008 at 11:01 AM
-
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?
Andrew
Posted by: areplogle on March 29, 2008 at 10:21 AM
-
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?
Posted by: vivekp on April 03, 2008 at 11:33 AM
|