Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need servlets for server-side Java programming?

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?

like image 855
Márcio Paiva Avatar asked Apr 18 '13 14:04

Márcio Paiva


People also ask

Is Java servlet server-side?

Java servlets are server-side programs (running inside a web server) that handle clients' requests and return a customized or dynamic response for each request.

Is JSP used for server-side programming?

Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.

Why do we need servlets in Java?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

Is servlet a server side scripting language?

Servlet technology is used to create a web application (resides at server side and generates a dynamic web page). Servlet technology is robust and scalable because of java language. Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side programming language.


2 Answers

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.

like image 123
Boris the Spider Avatar answered Sep 21 '22 13:09

Boris the Spider


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.

like image 31
rancidfishbreath Avatar answered Sep 18 '22 13:09

rancidfishbreath