Jython for Servlets

Michael Stevens mstevens at firedrake.org
Fri Jun 22 10:11:48 EDT 2001


On Thu, 21 Jun 2001 11:59:08 -0500, Michael Chermside <mcherm at destiny.com> wrote:
>I would like to use Jython to write servlets in python and run them
>on a java application server (WebLogic or one if its ilk). Seem like
>this should be pretty easy to do... does anyone have a "hello world"
>example I can work from?

I wrote this a while ago playing with jython and servlets:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.python.util.PythonInterpreter; 
import org.python.core.*; 

public class JythonServlet extends HttpServlet {
    
    protected void doGet (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<html><head><title>JythonServlet</title></head>");
        out.println("<h1>JythonServlet</h1>");
        try {
            PythonInterpreter interp = new PythonInterpreter();
            interp.exec("import sys");
            interp.set("out", out);
            interp.exec("out.println('<p> the jython interpreter lives!</p>')");
            interp.exec("ver = sys.version");
            PyObject ver = interp.get("ver");
            out.println("<p>The jython version is " + ver + "</p>");
        } catch (PyException e) {
            out.println("<b>Help! Python Exception</b>");
        }  
        out.println("<hr></body></html>");
        out.close();
        
    }
    
    protected void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        this.doGet(req, res);
    }
    
    public String getServletInfo() {
        return "Jython Servlet - Michael Stevens - michaels at iii.co.uk";
    }
    
}




More information about the Python-list mailing list