DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Use AzureSignTool to Sign Executables With Azure DevOps
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • How To Build a Google Photos Clone - Part 1

Trending

  • A Guide to Developing Large Language Models Part 1: Pretraining
  • The Role of Functional Programming in Modern Software Development
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  1. DZone
  2. Coding
  3. Languages
  4. How to Run Jython on Tomcat

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!

By 
Holger Paffrath user avatar
Holger Paffrath
·
Mar. 07, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.4K Views

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.Image title
  • Under jythonTemplate, create two new directories, WEB-INF and META-INFImage title
  • 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.Image title 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>

    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:
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:Image titleYou 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.Image title

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.

Apache Tomcat Jython Directory application

Published at DZone with permission of Holger Paffrath, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Use AzureSignTool to Sign Executables With Azure DevOps
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • How To Build a Google Photos Clone - Part 1

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: