13

I need to program a small server to handle requests from an Android application. (The user sends a number to the server, the server does some math on the numbers received from each user - the average, let's say - and returns it).

I've just read this simple introduction from Oracle (http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html).

It explains how to leave a server listening on a specific door. Normal java, no special application needed (such as Tomcat).

I know people generally use 'servlets' (with Tomcat) to accomplish this kind of thing. Why is that? What are the advantages? Can't i just program a sample server like the one described in that simple tutorial, and leave it always running on a computer?

Márcio Paiva
  • 983
  • 9
  • 25

4 Answers4

15

Using a socket is very simple. At first.

You create your server class, you have it bind to a port and away you go.

The first hurdle you will hit, covered in the comments, is multi-threading. But a simple producer/consumer pattern will solve that for you in no time.

The next problem you will hit is protocol.

  • Who talks first?

  • How do you respond?

  • How do you deal with an invalid request?

  • What happens if the stream collapses during a request?

  • Do you open a new socket for each request or does a client hold onto a socket and write multiple requests?

  • Maybe you want some sort of non-blocking IO?

This is where HTTP comes in, it is a protocol for communicating over TCP/IP (actually over anything, you could use bits of paper and a bike). It defines answers to all the above questions (and many more).

So, you run a webserver (tomcat, glassfish) and it deals with the raw sockets and sending the right information.

A servlet is an abstraction, when Tomcat has a connection and it has negotiated compression, encryption etc it will pass a request onto the servlet.

The servlet doesn't have to worry about the raw socket, it reads the request and writes a response.

It's worth pointing out that HTTP isn't the only protocol, it's just the one happens to be used for web-browsing. And so the one used by web-servers.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • +1 for HTTP over bits of paper and a bike. It would be interesting to see that happening. Also, I shall quote that in the future, if you grant such permission. – afsantos Apr 18 '13 at 18:45
10

You do not need servlets.

Servlets are helpful because they manage the socket handling for you but the drawback is you need a container (such as Tomcat) to run your servlets on. You may want to look at Netty which is really built to do the kind of work you are talking about.

rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
4

Servlets are the standard way of handling the HTTP protocol in Java.

If you want to use HTTP for your client-server communication and you want to use Java on the server side, then you should use servlets.

Servlets are a simple and effective solution for communicating in Internet with Java. For your needs Netty, as suggested by rancidfishbreath, is a valid alternative but I recommend servlets with Tomcat because it's a simpler solution. Having to use Tomcat is not a problem: it's light and free.

Discard the idea of using raw sockets for the reasons already explained by bmorris591.

Pino
  • 7,468
  • 6
  • 50
  • 69
0

You may want to look at something other than Java as the server-side solution. Perhaps Node.js, PHP, or maybe even Ruby on Rails might be a better, no-bells-and-whistles options. I can probably list 100 more options (Python, Django, etc, etc, etc) that would be way more simpler than trying to learn Java and Servlets and all that comes along with it. You don't necessarily need to have a Java app talking to a Java app, and there are loads of options for getting simple HTTP servers running custom solutions that don't require a huge learning curve.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • I thought Ruby on Rails and Django would be more complex, made to be especially suitable to full-featured websites. Isn't that the case? – Márcio Paiva Apr 18 '13 at 19:11
  • I found Ruby on Rails pretty easy. It, from what I remember (did it last 3+ years ago) was very PHP-like, with lots of auto code generation. However, the point is that you don't really need Java at all. Find something easy and use it. And if you want to stick with Java, there are tools for that too, like Spring Roo, that can generate all the boiler plate code and give you a pretty decent working head start. – CodeChimp Apr 18 '13 at 19:26
  • The question is specific: `for server-side Java programming`. – Viccari Apr 18 '13 at 19:49
  • He already knows Java syntax, he only needs to learn servlets, not Java and servlets. – Pino Apr 18 '13 at 19:54
  • From the context of asking if he needs Servlets, it doesn't sound like the OP is 100% comfortable with Java. And besides, "Java syntax" is mostly C-like, same as many other languages. If there were a solution out there that was easier to use, took less coding, and provided what the OP needed, it's worth mentioning. I am not trying to talk the OP out of using Java, but developers who are highly familiar with Java don't usually ask questions like "Do you need Servlets for server-side Java programming". It's not like Servlets are new or anything. – CodeChimp Apr 19 '13 at 11:00