Do I need servlets for server-side Java programming?

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.


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.


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.