|Lesson 8||Advantages of Servlets|
|Objective||Explain 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:
- Generate pages based on information from a form, as CGI can
- Maintain state as simply as with ASP
- Separate your HTML look-and-feel from your executable code as SSI EXEC pages do
- 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.
- Webserver handles servlet request.
- A new process is spawned
- A thread is started for this request
- Another user is at this site at the same time
- This user makes a request to run the same servlet.
- No additional processes are started
- Another thread is started for this request
- First servlet thread returns HTML to the user
- The thread ends, but the servlet is still running
- Second servlet thread returns HTML to the user
- The second thread ends, but the servlet is still running.
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.