The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


My first Jython web app on GlassFish v3

Posted 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

  • 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

    gfstart-jython.png
[/c/dev]
$./glassfish/bin/asadmin deploy calendar
properties=(name=calendar)

Now access http://localhost:8080/calendar/Show.py, it should show this page:

cal1.png

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:
cal2.png

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)

Wow ! looks cool

Very very nice!!!

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

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.

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?

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

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).