Reputation: 2939
I want to write something in Java that would compute something on the fly. So, I want to have a Java server that can reply to AJAX requests. That's all I need. I want it to be as minimalistic as humanly possible.
If you want a hypothetical scenario, pretend I want to write an adder service. Something to which I can pass the JSON: { "action" : "add", "args": [0, 1] } and get { "sum" : 1 } back. That's all, but I want it on the web. I don't care about security or anything else. So no sessions, no https or anything.
I don't want to set up a server or use some bloated framework. I'm thinking I can accomplish this with JNetPCap and pure old Java, but I would like to get rid of PCap as well.
I guess I'm asking what in the standard JRE or a very light-weight jar can give me what I'm looking for.
Thank you guys.
Edit: For the purpose of the exercise, imagine that what I want to write should be able to run on anything that has Java installed and has an open port.
Edit #2: It turns out my entire thought process had a fundamental flaw: If I have a server that only serves JSON there's no way to have a page on the same domain that can safely request that JSON without using JSONP or something. So, I will be using the JLHTTP. Thank you all who participated.
Upvotes: 0
Views: 737
Reputation: 3255
You can implement a very basic HTTP server in about a hundred lines of code using Java's nio. I wrote one that I used for a lecture on logging (as a replacement for debug-by-println). It was conceptually similar to your Server.java example, but used the nio packages. Assuming you don't want to write your own, or just want an example, take a look at Raining Sockets. Next, you'll need something to parse the JSON. You can use Gson (no link because my SO reputation isn't high enough), or again, write your own. If you write your own, you may want to use a parser generator tool like ANTLR. ANTLR might also help if you want to support the HTTP specification with a little more rigor than just matching "HTTP GET".
Upvotes: 0
Reputation: 8374
This is the most lightweight functioning HTTP server written in Java that I've come across so far (39 KB for the whole thing). I've used it for doing integration testing, I have no idea how it would fare as a production server. In particular, I don't know how it handles multiple concurrent requests.
Upvotes: 1
Reputation: 2939
I'm leaning towards something like this:
http://examples.oreilly.com/9781565923713/Server.java
I'm not sure if one can handle AJAX requests through it though or if JRE has standard libraries to read requests from Sockets.
Upvotes: 0
Reputation: 6179
I think you might be too quick to dismiss the "bloated-frameworks". What you're trying to do is implement - at the bare minimum - something capable of receiving, understanding, and responding to http GET requests using JSON for your message passing. While this can be done using code completely native to your run of the mill jdk, you would probably end up writing far more code to accomplish this, than you would if you would follow a quick tutorial on Tomcat or Jetty. That is what I'm guessing the purpose of @jcm's comment was.
But if you really want to just get this done and not think about it, all you really need to do is follow a tutorial like this one: http://www.youtube.com/watch?v=EOkN5IPoJVs
The video is using tomcat and eclipse. The version is an older one but its close enough that you can kind of play around and figure it out. I'm only recommending Tomcat because its what I know; but Jetty would work perfectly well as @David suggested and I'm sure there are plenty of simple tutorials out there for that as well.
Also, once you get your servlet up and going, I think you're going to find that parsing your json requests as strings is annoying. I would recommend looking into gson. It is a really simple way to map JSON strings to POJOs.
The gist of what I'm saying is that doing this all natively with java and without any 3rd party software might be the shortest path, but not the easiest. Just like taking your car to your friends house might be a 3 mile drive, but its still easier and quicker than running 1 mile through the woods.
Upvotes: 0
Reputation: 5195
Since AJAX requests/responses rely on HTTP, you're going to have to use some sort of HTTP server. I'd recommend Jetty as it is pretty light-weight and there seems to be many examples on how to use it.
Upvotes: 1