Java Servlets   «Prev  Next»

Lesson 8Advantages of Servlets
ObjectiveExplain the Advantages of Java Servlets

Explain the Advantages of Java Servlets using the Java Servlet API

Servlets, like CGI, SSI, and ASP, are code that runs on the server, before the user sees any output. They are written in Java, which means that you can use them on any platform that supports Java, including Linux, Windows 7 Windows 10 and many more.
There are servlet-supporting Web servers at all price ranges (including free), and you can install add-on software that adds servlet support to many existing Web servers.
With servlets you can:
  1. Generate pages based on information from a form, as CGI can
  2. Maintain state as simply as with ASP
  3. Separate your HTML look-and-feel from your executable code as SSI EXEC pages do
  4. Access databases simply
In addition, because your code is written in Java rather than C, Perl, or VBscript, the code is easier to maintain since it is object-oriented code.

Servlets are more efficient

Servlets are more efficient than CGI. Once a servlet has run once, it remains in memory.
If another user needs the same servlet, it starts almost immediately without having to wait while the process spawns
(Since this is not Perl, the Perl interpreter is not loaded, either).
If two users need the same servlet, only one copy runs, and the different requests are handled by different threads, allowing your Web site to scale more gracefully than if you chose another server-side solution.

  1. Webserver handles servlet request.
  2. A new process is spawned
  3. A thread is started for this request
  4. Another user is at this site at the same time
  5. This user makes a request to run the same servlet.
  6. No additional processes are started
  7. Another thread is started for this request
  8. First servlet thread returns HTML to the user
  9. The thread ends, but the servlet is still running
  10. Second servlet thread returns HTML to the user
  11. The second thread ends, but the servlet is still running.

Java Servlets
Client Server Communication
Servlet versus CGI
Servlets are safer than other server-side solutions. One reason some hosting providers are unwilling to permit certain kinds of server-side programming is that a mistake on your part could bring down the whole shared server. That can never happen with servlets, thanks to the security and safety features built into the Java language.
In the next lesson, the technology that makes Java servlets work will be discussed.