Jython Under Tomcat

A little something different this post, something a little closer to my day job, which is as a developer.

Here I will go through how to configure Jython to run under Tomcat.
I have done this on an AWS instance, but because it is java based, it can be done on Windows and Mac just as easily. In fact, at work, I did this under Windows.

So, here is how you set up Jython to work under Tomcat.

  • Install Tomcat normally
  • Under the webapps directory, create a directory for your webapp. In this case, I’ve called it jythonTempate because I use it as a template for my apps.
  • Under jythonTemplate, create 2 new directories, WEB-INF and META-INF
  • Under the WEF-INF folder, create a lib directory
  • Download jython-standalone.jar, the latest version can be found here, and place it in the WEB-INF/lib directory.

    This library will be loaded into memory when the tomcat application starts.

Creating The Servlet

  • Jython servles work by means of an intermediate Java servlet known as PyServlet. This is the servlet that Tomcat runs Jython servlets in.
    Now we need to tell Tomcat to invoke the PyServlet whenever it gets a request for a resource with *.py. We do that through the web.xml file.
  • Under WEB-INF, create a file called web.xml with the following content:
    <web-app>
        <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>
    </web-app>
    

    This registers the PyServlet and matches it with any resource that matches the pattern *.py

  • Next we create the Jython Servlet. Create a text file called jythonTemplate.py under the jythonTemplate directory.
    Give it the following contents:

    import sys
    
    from java.io import *
    from javax.servlet.http import HttpServlet
    
    class jythonTemplate(HttpServlet):
        def doGet(self, request, response):
            self.doPost(request, response)
    
        def doPost(self, request, response):
            toClient = response.getWriter()
            response.setContentType("text/html")
            toClient.println("<html><head><title>Servlet Test</title></head>" +
                        "<body><h1>Servlet Test</h1></body></html>")
    
    if __name__ == "__main__":
        JS = jythonTemplate()
    
  • Start tomcat and go to the URL of the server. For example, if you are on localhost : http://loalhost:8080/jythonTemplate/jythonTemplate.py
    You should see a similar output like this:
    You now have created your first Jython servlet.

Adding the Python Libraries

Jython/Python has a rich set of libraries that you can use within you Tomcat application, but before you can start using these libraries you need to make them available to your application. To do this,

  • Download the jython installer from the link above.
  • Under WEB-INF create a directory called lib-python
  • Unzip the jar file and copy the contents of the Lib directory to the lib-python directory. It doesn’t matter what name you use for the lib-python directory, it just needs to be in the WEB-INF directory. All these files load into tomcat under this directory.
  • You now have access to the python library to use within your servlets.

Set the Default Application

To set the default jython application to be run when you go into the top level http://localhost/jythonTemplate URL, you need to modify the context.xml.
This file needs to be created under the META-INF.It contains the following content:

<?xml version="1.0" encoding="UTF-8"?>
<Context anitJARLocking="true" path="/jythonTemplate/jythonTemplate.py"/>

You may have to restart tomcat for the changes to take effect.

Generally, if you modify a python servlet, the changes take effect straight away, but if you make a python library file. It generates a class file on load and this is what is loaded into tomcat. When I make these types of changes, you still need to re-load the application or restart tomcat.

I do a lot of work with SoftwareAG’s WebMethods, I am in integration. So what I have done is write a library in Java that allows me to execute webMethods services through Java, but since I also use Jython, I can execute these services in Jython now as well.

So, why Jython? Especially given that the version is 2.7.1, which the python version will cease support soon? Well, because I like the python language. I find it easy to use than Java.
I need to play around a bit more and see if I can use Flask to create applications. But for now, I create simple tools that help me with my day to day work. I also at some point want to see if I can replicate this using JRuby.

Please let me know in the comments if you found this useful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.