Servlet-kinda thingies (was Re: Migrating from PHP to Python...

mstevens at firedrake.org mstevens at firedrake.org
Mon Sep 3 08:36:55 EDT 2001


On 03 Sep 2001 15:26:20 +0300, Ville Vainio <vvainio at karhu.tp.spt.fi> wrote:
>Chris Dutton <chris at cmb-enterprises.com> writes:
>
>> As for performance, I don't know of any benchmarks on the subject, but in
>> certain applications I wouldn't be surprised if Python was faster and had a
>> smaller resource footprint, because there isn't a ton of built-in stuff.
>> You have the core language, then import the modules you need.
>
>What about running Python scripts as "servlets", that is, processes
>that are running constantly and only react to form posts/gets by
>executing some doGet/doPost method (in different threads, possibly)? I
>assume there is no reason to not use Jython with Java servlet
>framework (except perhaps performance?). A web server implemented in
>python would obviously be able to do that, though I don't know how
>fast it would be. Web serving is quite IO-bound, so python might work
>quite well.

The basic concept of running jython from within servlets works 
perfectly well...

Here's a simple servlet that does jython stuff that I wrote a while
ago whilst experimenting with jython:

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";
    }
    
}

Presumably this could be extended fairly easily to a generic form
by implementing the whole servlet class in jython as a subclass of
HttpServlet, but I've not looked into it.

Michael



More information about the Python-list mailing list