开发者

Python web service for a java application?

Forgive me if this is a stupid question. I am completely new to build开发者_开发问答ing web services and complete web apps. I want to develop a particular functionality for a java based web application. However this functionality is simpler to develop with Python. So is it possible If i develop this web service with Python and use it for a Java based webapp?


I think it is possible. Let's assume that your webservice method returns a json string. You can use this string, convert into a HashMap in Java and work with the data.

I personally prefer cherrypy, for the ease of creating a webservice. It has an inbuilt server too.

You can use FlexJSON to convert json string into a HashMap. The method deserialize will help in doing so. This is how I did it.

public Map<String, Object> JsonToMap(String data) {
        Map<String, Object> dataMap;
        dataMap = new JSONDeserializer<Map>().deserialize(data);
        return dataMap;
    }

Here, String data is the json string that your python webservice method is supposed to return.


That depends on how you mean this. Do you mean to use the service as a library within the app? If so, you may want to look at Jython and make it into a plugin.

If, however, you want to run the service as a separate app, then use the web interface to call it from Java, that should be no problem. In theory, the end user of a web service (in this case, the user is your app) should never have to worry about what language the service is written in; as long as the standards are kept to (JSON, XML, etc), most languages will have the capability to work together.


When developing in a framework, it is generally simpler to develop with the language of the framework than it is to develop with a different language.

Servlets are components of the web server (which is also called a Servlet container). The Servlet container and the required Servlet API is all Java. While you could Frankenstein in some sort of Python code, odds are good that the integration effort would eventually make that "simplicity" far more complex than you particularly desire.

If you want a Python web application, use a Python web framework. If you want a Java web application, use a Java framework. Don't try to make the two cross compatible, as the integration points (and used / offered conveniences) are not even guaranteed to be present on the "other side" of the fence.


There are many web frameworks for java, some are better than others. For a fairly comprehensive reference, consider consulting http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#Java.

Python is indeed a fairly intuitive language and because it is frequently used as a server side scripting language, a lot of respectable web frameworks (like webpy and django) have emerged.

It depends what tasks you are trying to implement. If your project requires a java applet, you may be able to embed it within a web page which you dynamically compose using a python framework. However, if you are looking for interoperability between java and python, you will wish to read more about Jython (and it's limitations) as lacqui has suggested.

Personally, I'm a fan of webpy over cherrypy for quick, lightweight, practical and easy web development and templating, however, if speed is a major concern, you may wish to consider this before adopting a dynamically typed interpreted language. Python 3 is on average around 50 times slower than Java 6 over the JVM (http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=python3&lang2=java)

For more information regarding the comparison of java versus python, take a look at http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜