views:

342

answers:

2

I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.

I have got the java code receiving connections on sockets and responding with some simple HTML.

My problem is that I will POST data to the Java server and that code needs to act on the data. How do I go about reading the data that is posted in the HTML request, if it is even possible. If not, is there any other way you would do this.

If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing.

Thanks

A: 

In your case I probably wouldn't work with low level socket connections.

I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. With a servlet you can easily access the POST data, process it and return something to the caller.

If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container.

If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. This allows you to provide a fully featured API for your Java resources in an easy way.

Florian Gutmann
Hmmm. I really don't want to use anything as complicated as that. I am really limited on resources and the code only receives 2 types of request and does not need to issue a response at all. The simpler the better, hence why I am using sockets. Also, my web host doesn't support any of the things you mentioned above I don't think. At the moment, the test code is just a Jar file that I run that waits for connections.
danpalmer
Implementing your own http server is really tedious and error prone work. With winstone you can still just stick to your single jar and use the complete servlet api see: http://winstone.sourceforge.net/#embedding. The same is possible with jetty or "simple" as number5 metioned already.
Florian Gutmann
I think I am going to go with embedding Jetty in my application as their documentation for embedding seems really good. However I dan't want to embed all the bits I don't need. How do I figure out what I nede and what I don't and how do I get the code into my application? Eclipse is awful for ease of use compared to Visual Studio and Xcode so I can't figure out how to do it. There are loads of different import methods!
danpalmer
I don't know jetty very well, but probably it's not easy to just let some parts of jetty away. Why would you like to? To save some kilobyte of memory? If you really like to get rid of unused stuff you could try to run some obfuscator like ProGuard on your application http://proguard.sourceforge.net/. But normally this isn't necessary and I can't really see your point.
Florian Gutmann
+3  A: 

You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple

and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod

number5
I am going to try to embed Jetty. I can see it has good documentation, but it all assumes you know how to actually embed the code in your jar file. I don't.
danpalmer
I did manage to embed jetty and it is really great. Thank you for persuading me to do so. You saved me a lot of coding and a lot of tearing my hair out!
danpalmer