3

I have not worked with HTTP post/get before, my up coming project in my office is based on http post/get in java. Its basically client - server based application. the client will post some info and I need to get that info and process the string and vice-verse. this project has to be developed on J2SE. You can assume this some thing like a JMS queue message processing stuff. I googled for the info but most of the information was for web application, mine should work like a message queue. Can someone explain me how to do this or point me where I can get some useful info.

Thanks Arun

AKV
  • 183
  • 4
  • 24
  • Do you actually need to use HTTP, or will your own custom protocol be fine? (ie. do you have control of both the client and the server?) – Crollster Dec 09 '11 at 03:31

5 Answers5

3

Jetty is a popular web server, designed to easily be embedded in an application.

Its HTTP server component can run inside your application and respond to requests by dispatching to your custom code.

Jetty also features an HTTP client that you can use on the client side to send requests.

This is a rather big topic and I won't be able to post a complete guide, but Jetty's documentation is generally of very high quality and should be a good starting point.

Martin
  • 37,119
  • 15
  • 73
  • 82
  • Jetty is a good answer for the server. Don't worry about distinctions like J2SE v.s. other stuff. Jetty will run in you J2SE JVM just fine. If you don't want to deploy extra libraries to the client (to use Jetty http client for example), you can do an http post just with Java standard libraries. example here: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – BillRobertson42 Dec 09 '11 at 05:15
3

Well, if you don't need to specifically use strict HTTP, and you need to just use Java SE (and not Java EE, which rules out Servlets, JSPs, JMS, etc), then you need to probably investigate ServerSocket and Socket classes.

Server

Your server would need to listen on a TCP port (say, port 8080) - usually you would pick a port number between 1025 and 65,535, however if you are attempting to use an already defined service that has a default port, then use that. Note however, that on unix, in order to listen on any port below 1024, I believe you need to be root. Traditionally, port 80 is used for HTTP.

To listen on this port, you would need something like this in your code:

ServerSocket srvSocket = new ServerSocket(8080);
Socket socket = srvSocket.accept();

This pretty much the most basic code that would cause your application to wait until something connected to port 8080. Once connected, you could obtain both an InputStream and OutputStream for your connected client, by interrogating the returned socket object, allowing you to read content from the client, and inserting these requests in a queue. This queue could be then processed by some other Thread.

Client

In order for your client to connect to the server, you would need to use something based on the following example:

Socket connection = new Socket("server.domain.com", 8080);
OutputStream output = connection.getOutputStream();

You would then write your request to the server into the OutputStream (and read from the InputStream returned from getInputStream() if you expected a response)

The code supplied is pretty basic, but it should give you a rough idea of how to proceed. You can even use this method if you wanted to use real HTTP, however it might be a better idea to use some premade library if that was the case (although its probable that you're not going to require all functionality defined in the HTTP spec itself).

Anyway, I hope that provides you a good starting point from which to build.

Crollster
  • 2,751
  • 2
  • 23
  • 34
  • Sockets are nice for simple stuff, but you need to be very careful if you need Queue behavior where all messages are processed in order. – user949300 Dec 09 '11 at 05:42
  • Agreed, however using Sockets, queues, etc, is exactly how one might code a MUD (which is where most of my ServerSocket experience comes from). As long as you wrap up the request from the socket in some sort of object (eg, such as HttpServletRequest, which is unavailable to the OP as it's in JEE), then queuing these objects should be fairly simple. As with most things it will depend on the skill and experience of the developers in question. We can only answer the questions that were asked. – Crollster Dec 09 '11 at 05:49
  • I've used sockets a lot too - they work well in most cases. Nice post. I'll give you an upvote now. – user949300 Dec 09 '11 at 05:58
  • A small correction. In my last mail the HTTP has been revised to HTTPS . SO have to incorporate ssl security as well. – AKV Dec 13 '11 at 00:17
  • Ok, in that case, I would recommend you follow Martin's advice and embed Jetty in your application - it's a wonderful piece of code, and is an absolute breeze to use! – Crollster Dec 13 '11 at 01:52
0

I suggest you start with learning the basics of HTTP protocol. This article is a good starter. After you understood the basics follow the this article on how to programatically communicate (read/write) with HTTP servers. After that Google is your friend.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

If you weren't restricted to J2SE, you could use Servlets for managing the POST/GET methods of HTTP. Evaluate if it is possible, otherwise you'd be reinventing the wheel

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

I also have a mainly SE background. On the client side, writing get/post is pretty easy. Or you can Google to find source code. I found that using REST was straightforward and understandable. On the server side, there are many options and I have very limited experience. I wrote the server using standard JEE6 and it wasn't too painful, but sounds like that is not an option for you.

user949300
  • 15,364
  • 7
  • 35
  • 66