How to Run Jython on Tomcat
We look at the code and processes necessary to help you get Jython running on top of Tomcat, because, well, Python is fun!
Join the DZone community and get the full member experience.Join For Free
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 on Windows.
So, here is how you set up Jython to work under Tomcat.
- Install Tomcat normally.
- Under the web apps directory, create a directory for your web app. In this case, I've called it jythonTempatebecause I use it as a template for my apps.
- Under jythonTemplate, create two 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 servlets 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>
- Next we create the Jython Servlet. Create a text file called jythonTemplate.py under the jythonTemplate directory. Give it the following contents:
This registers the PyServlet and matches it with any resource that matches the pattern *.py
Python 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() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 Jjython 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 code:
<?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 reload 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 easier 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.
Published at DZone with permission of Holger Paffrath, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.