page content: Java Servlets

Java Servlets

Java servlets are a quick, efficient way to integrate Java with HTML, and so to create versatile dynamic web pages for any purpose.

Java servlets allow a request for a web page (or any <URL>) to be processed by a <Java> application on the <server side>. They are Sun's answer (the company behind Java) to the problematic <CGI> engine.

A little CGI overview

CGI  allows specific page requests to be processed by any application, as long as the application file is sitting in the <cgi-bin> folder. The application is called directly through the <URL>, and is given arguments in the body of the URL (the "Get" method) or "behind the scenes" of the page request (the "Post" method). These arguments allow the application to interact with the outside world, mainly with the user who sent the URL request.

For example, an application for looking up countries can be given as parameter a country code, and return an HTML page with all that country's statistics. Another application, for user management, can be given a username and password, and if they match, return that user's private account page.

The pitfalls of CGI

Every time a CGI application is called, it is run as an independent application on the remote server. This can quickly overload the server, causing it to perform slowly and even crash. Worse, if a CGI application has an internal error, it can crash—and take down the entire server with it. In short, classic CGI comes at the expense of stability, security, and performance.

Java servlets' answer

A Java servlet is a class object—a piece of code that defines a collection of functions and variables—which runs on the remote server's servlet engine. As long as it's running in the servlet engine, it is active and listening for requests.

Java servlets can be called from URLs with parameters, the same way as CGI. But here the difference begins. In CGI, this call would have activated a new, independent application to handle the request. With Java servlets, the application is already running. It creates a new thread (a copy of itself in terms of capabilities, without actually duplicating the code), and uses this thread to process the request and to send back a response, usually in <HTML>. Multi-threading saves a lot of CPU time, and if a certain thread crashes, nothing happens to the server itself; at most, the servlet will crash inside the servlet engine. At best, the thread will die but leave the servlet running, without even interferin with any of the other threads.

The life of a Java servlet

A servlet lives through the following stages:

  1. A web container (in the servlet engine) loads the servlet class and initializes it.

 

  1. The servlet waits for any client requests.
  1. Once a valid request is received, the servlet creates a new thread for handling it. One of the servlet's functions is called and executed (which function—depending on the request). The result, usually HTML code, is sent back to the client.

 

  1. Once the client is served, the thread that had handled his request is destroyed and no longer takes up any resources.

 

***SOCIALIZEIT***