1

I have to implement one web2py application which has to access java code (which has code to connect to the remote machine) but not sure whether we can do it in web2py or not.My PC has Java 1.6, Python2.7 ,web2py ,eclipse installed.

Use case is :

I have created one button in web2py application and upon clicking the button, it should instantiate the java object and invoke particular method of that java object which will further connect to the remote machine.

Doubts are:

  1. Can we deploy that particular java class to web2py server so web2py application can easily access it?

  2. Is it possible to import that class from python code?

  3. How to instantiate java object from python code?

  4. And how to invoke java method from python code?

Regards, Piks

jadkik94
  • 7,000
  • 2
  • 30
  • 39
piks
  • 1,621
  • 8
  • 32
  • 59

2 Answers2

2

I would consider looking into webservices. If you could expose url from java, that will route to a method/function of java where logic is performed and it returns json object. While in web2py urllib2 you can make a request and decode that json into native python dictionary. The clue is that you would have to expose all the methods of objects and pass the object back and forth as json. Do not be scared in most programming lanugages objects are just hash_arrays/dictionaries with some special qualities. So if you can serialize and deserialize the object and expose apriopriate urls you will be fine.

Also there is implementation of web2py in jython. But then the entire stack will be in JVM and i may be more complex to work with.

mAm
  • 177
  • 1
  • 13
  • Okay, I understood what you are saying,it seems like i need to do so many extra work to achieve it,So if I go for web service implementation and NO JAVA code is required then how to achieve it? I again put my use case in different manner: 1. our web2py app sits on one machine where web client will also run and DB will also be there. – piks Apr 12 '12 at 03:15
  • If I go for web service implementation and NO JAVA code is required then how to achieve it? I again put my use case in different manner: 1. our web2py app sits on one machine where web client will also run and DB will also be there. 2. User can access web2py app using url from remote machine and access the content saved in DB and display to the user. 3. Server can talk to the one remote system where web service server is installed and fetch the required data from there and save to the DB. As i am new bee in web service so Could you please provide me some basic level of idea how to achieve it. – piks Apr 12 '12 at 03:27
  • There is a chapter in book explaining mostly how to expose webservice from within the web2py its not that. I am not that familiar with java, but I bet there are some microframeworks that just expose certain functions as urls addresses and provide basic post&get. What you would have to do is make those functions return jsons and then you can make request via urllib. I would answer myself if its worth it... if its for some simple classes I bet you would rewrite it faster than struggle with java stack. – mAm Apr 14 '12 at 17:59
  • If you really want that functionality I would look here: http://stackoverflow.com/questions/3290522/urllib2-and-json read more about urllib, and learn about servlets in java + jsons in java. It should not be insanly hard, but as I said before if its for a small thing I doubt its worth the trouble. – mAm Apr 14 '12 at 18:04
  • Just a comment about Jython: Although the documentation states that web2py runs on Jython without modification I could not get it to work with any recent version. Searching the web shows low interest in running web2py on Jython. So I would rather not recommend that option... – brdlph Apr 24 '13 at 14:26
0

If all the java is doing is connecting to a remote machine, then I strongly recommend that you reimplement it in python.

If it is doing something substantive, then consider putting a web interface in front of it (I like the spark microframework for this), and you can speak to it using the requests module (get it via pip).

Marcin
  • 48,559
  • 18
  • 128
  • 201