Python Servlets
The motivation for Python Servlets comes from the Java programming language and
related technologies that were designed at the end of the past century.
The Java Servlet API has evolved with over 20 years of contribution from millions
of software developers.
The API is fast, efficient, and secure.
It is also full featured, providing a standard interface for common services which
include access mechanisms, session management, and application security.
An Exciting Example
The HelloServlet example is useful for introducing the mechanisms integrating
a Python Servlet into a Web Application.
HelloServlet.py
from pythonx.servlet.http.HttpServlet import HttpServlet
class HelloServlet(HttpServlet):
def doGet(self, request, response):
response.setContentType("text/plain")
response.getWriter().println("Hello, World!")
An application developer has some flexibility regarding where this file is located in the filesystem.
Returning Binary Data
The ImageServlet example demonstrates how to return non-textual data.
ImageServlet.py
from pythonx.servlet.http.HttpServlet import HttpServlet
class ImageServlet(HttpServlet):
def doGet(self, request, response):
# Set response content type
response.setContentType("image/png")
# Get file contents
file = open("website/icons/paige-450.png", "rb")
contents = file.read()
file.close()
# Return file contents
outputStream = response.getOutputStream()
outputStream.write(contents)
Sample Servlet Executions
Several servlets are available to demonstrate their execution.
| Hello |
A trivial first servlet that generates a simple message |
| Image |
An example of a servlet that returns binary data (an image file). |
| BMI Form |
This example presents a simple page containing a form that is passed to a servlet
when the form is submitted to the server.   The example was scraped from
the Internet from an instructional site to teach Java Servlets. |
| Redirect |
The redirect servlet demonstrates how a servlet passes control to a PSP.
This is a useful trick that developers can use if they would like to use servlets to process
logic and yield control to PSP to render the output returned to the client. |