Python servlet for Java applet ?

L

Linuxguy123

Hi guys.

Is there a way to use a python application as the back end (ie rpc) for
a Java based applet ?

How does it work compared to a Java servlet with a Java applet ?

Thanks
 
P

Piet van Oostrum

Linuxguy123 said:
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

Linuxguy123

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).


With a Java applet communicating with a Java servlet you also have the
option of using RMI. For the rest it is similar.

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

Thanks !
 
M

Marco Bizzarri

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

Thanks !


Ok, let's make some basic questions:

1) do you know how to program an applet in order to invoke an URL on a server?

2) do you know how to program a server in order to answer to HTTP requests?

If the answer is *NO* in both cases, better if you take a look at this
topic in general (I'm sure you can google around a number of tutorials
on this) and then return here asking questions about what you're
unable to do in python.

Of course, these are my 2 cents.

Regards
Marco
 
P

Piet van Oostrum

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.
 
L

Linuxguy123

Ok, let's make some basic questions:

1) do you know how to program an applet in order to invoke an URL on a server?

I know how to invoke a URL with an application. I assume its the same
doing it with an applet.
2) do you know how to program a server in order to answer to HTTP requests?

I've got a good idea how to do that.

I thought that applets weren't allowed to access URLs directly. If they
can, this problem becomes trivial.

Thanks for the reply.
 
P

Piet van Oostrum

Linuxguy123 said:
L> I thought that applets weren't allowed to access URLs directly. If they
L> can, this problem becomes trivial.

They are allowed if the URL is on the same IP address as where the
applet came from (same origin policy). But in your original post you
wanted RPC.
 
L

Linuxguy123

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).

<snip>

I agree that servlet isn't a Python term. I should have put it in
quotes.

Your reply was very instructive. Thanks for taking the time to post
it.

I am probably going to build my "servlet" as a simple Python socket
service.

LG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,160
Latest member
KieranKisc

Latest Threads

Top