My first Jython web app on GlassFish v3
Setup
- Install GlassFish v3.
-
Install Jython . The current
version I got is Jython 2.2.1.
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...
- Login or register to post comments
- Printer-friendly version
- vivekp's blog
- 4001 reads






Comments
by nocturnal - 2008-11-13 06: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 11: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 12: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 12: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 09: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 07:40
Very very nice!!!by nocturnal - 2008-03-27 21:36
Wow ! looks cool