Search |
||
My first Jython web app on GlassFish v3Posted by vivekp on March 26, 2008 at 4:17 PM PDT
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
Deploy the Python web application
[/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 librariesWe 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... »
Related Topics >>
Java Enterprise Comments
Comments are listed in date ascending order (oldest first)
Submitted by nocturnal on Thu, 2008-03-27 20:36.
Wow ! looks cool
Submitted by nocturnal on Fri, 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/2
Submitted by vivekp on Fri, 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.
Submitted by vivekp on Thu, 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?
Submitted by areplogle on Sat, 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?
Andrew
Submitted by nocturnal on Thu, 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).
|
||
|
|