views:

165

answers:

2

Hi,

Are servlets the only way one can write web applications in Java ?

+11  A: 

No. Servlets are just the most convenient way to write a Web application in Java. If you think about it: what is a Web application? Quite simply it is an application that can receive an HTTP request and send back an HTTP response. Common models for achieving this are:

  1. To use some kind of wrapper that invokes a script for each request. This was the first model and has a standard called CGI (Common Gateway Interface). The wrapper in this case is a Web server; or
  2. To have persistent code within such a wrapper that can service HTTP requests (rather than being transient like CGI scripts).

There are variations upon this theme (eg FastCGI for (1)). Servlets are an example of (2). The reason 99% of all Java Web applications and frameworks use servlets is because servlets are standard (they have a specification endorsed by Sun and a reference implementation) and they're so low-level that pretty much whatever you want can be built on top of them.

cletus
thank you that helps !!
Anand
A: 

That servlets are most convenient way to write Web apps in Java is arguable. They certainly have been around for a long time, but there are alternatives; take a look at restlets for example: http://www.restlet.org/

David Soroko
They're built **on top** of the Servlet API and it again boils down to the question: "are servlets the only way?".
BalusC
Oh I hadn't heard about restlet until you mentioned
Anand
There is a brief restlet summary here: http://www.restlet.org/about/introduction . The original implementation was indeed "on top". The current one is not.
David Soroko