Python servlet for Java applet ?

Piet van Oostrum piet at cs.uu.nl
Thu Apr 30 08:06:03 EDT 2009


>>>>> Linuxguy123 <linuxguy123 at gmail.com> (L) wrote:

>L> On Sat, 2009-04-25 at 02:00 +0200, Piet van Oostrum wrote:
>>> >>>>> Linuxguy123 <linuxguy123 at gmail.com> (L) wrote:
>>> 
>>> >L> Hi guys.
>>> >L> Is there a way to use a python application as the back end (ie rpc) for
>>> >L> a Java based applet ?
>>> 
>>> Yes, you can use Corba, XMLRPC, SOAP, JSON-RPC. Corba probably will have
>>> to be tunnelled through port 80 (the others already do this by default
>>> because they are based on HTTP).
>>> 
>>> >L> How does it work compared to a Java servlet with a Java applet ?
>>> 
>>> With a Java applet communicating with a Java servlet you also have the
>>> option of using RMI. For the rest it is similar.

>L> How does one "connect" the servlet to the applet ?  Does anyone know of
>L> an example program that demonstrates a Python servlet with a Java
>L> applet ?

Well, for a start, in the Python world the word 'servlet' isn't used
much. So I assume it comes from your Java legacy and you actually mean
'any server-side python script' (in webware they use the term
'servlet', however).

I have constructed an example for you that uses XMLRPC for the
communication. Use asked for RPC in the original posting, and I think
for applets XMLRPC is one of the easiests, especially since it avoids
the port problems with firewalls that you have with RMI or Corba. But
you didn't give any indication of what kind of application you had in
mind, so maybe RPC isn't necessary and a REST-style communication would
also be possible.

Also I have just used CGI for the 'servlet', as it is the easiest to
install on the server. If this is for a high-volume site then you should
choose something with better performance, such as WSGI, but then you
would have to install  mod_wsgi in Apache. Another possibility would be
to use a Web framework like Zope, Webware or Django.

I have created a simple applet and a python 'servlet' with a RPC that
just returns a string containing the current dat and time on the server,
prefixed by a parameter string. 

AFAIK, Java doesn't have XMLRPC support in its standard library
(javax.xml.rpc is for SOAP) therefore I have used the Apache XMLRPC
implementation from http://apache.org/dist/ws/xmlrpc/. You have to copy
3 jar files to your website together with the applet. Also, because of
the 'same origin' security policy of applets the python 'servlet' must
be on the same machine (same IP address) as the applet.

With SOAP you can do in principle the same but I consider it more
complicated.

You can test the applet at
http://www.pietvanoostrum.com/xmlrpc/xmlrpc.html.
Here is the code:

The Python code:
,----[ xmlrpcsrv.py ]
| #! /usr/bin/env python
| 
| '''
| Example XML-RPC server in Python.
| Use as CGI script
| '''
| 
| from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
| from datetime import datetime
| 
| def get_date_time(label):
|     '''Get the current date and time on the server as a string
|     preceded by label'''
|     return label + ": " + str(datetime.now())
| 
| handler = CGIXMLRPCRequestHandler()
| handler.register_function(get_date_time)
| handler.handle_request()
`----

HTML page containing the applet:
,----[ xmlrpc.html ]
| <html> 
| <title>Hello XML-RPC</title> 
| <center> <h1>XML-RPC Applet Demo</h1> </center> 
| <p>
| The Python server says:
| </p> 
| <applet codebase="./"
|    archive="ws-commons-util-1.0.2.jar,xmlrpc-client-3.1.2.jar,xmlrpc-common-3.1.2.jar"
|    code="examples.XMLRPCApplet" 
|    width=500 height=120> 
| </applet> 
| </html>
`----

The applet code:
,----[ XMLRPCApplet.java ]
| package examples; 
| 
| import java.applet.Applet; 
| import java.awt.Graphics; 
| import org.apache.xmlrpc.client.XmlRpcClient;
| import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
| import java.net.URL;
| 
| public class XMLRPCApplet extends Applet { 
| 
|     String message = "blank"; 
|   
|     public void init() { 
|         try { 
|             XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
|             // This will become something like 
|             // "http://www.pietvanoostrum.com/cgi-bin/xmlrpcsrv.py"
|             String url = "http://" +  getCodeBase().getHost() 
|                                    + "/cgi-bin/xmlrpcsrv.py";
|             System.out.println(url);
|             config.setServerURL(new URL(url));
|             XmlRpcClient xmlrpc = new XmlRpcClient();
|             xmlrpc.setConfig(config);
|             Object[] params = new Object[]{new String("Current time")};
|             message = (String) xmlrpc.execute ("get_date_time", params);
|         } catch (Exception e) {
|             message = "Exception??";
|             System.out.println("HelloApplet exception: " + 
|                                     e.getMessage()); 
|             e.printStackTrace(); 
|         }
|     }
| 
|     public void paint(Graphics g) { 
|               g.drawString(message, 25, 50); 
|     } 
| }
`----

The directory structure on the website is:

xmlrpc/
    xmlrpc.html
    examples
        XMLRPCApplet.class
    xmlrpc-client-3.1.2.jar  
    xmlrpc-common-3.1.2.jar
    ws-commons-util-1.0.2.jar  

cgi-bin/
    xmlrpcsrv.py

Hope this helps.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list