I. Overview
A servlet is a platform-and protocol-independent, server-side Java application that generates dynamic Web pages.
A servlet is a server-side Java application located inside a Web server, unlike a traditional Java application that starts from the command line, which is loaded by a Web server that must contain a Java virtual machine that supports the servlet.
Java Servlet comparison with applets:
Similarities and differences:
* They are not standalone applications and do not have the main () method.
* They are not invoked by a user or programmer, but by another application (container).
* They all have a life cycle that contains the init () and Destroy () methods.
The difference:
* Applet has a good graphical interface (AWT), with the browser, running on the client.
* The Servlet does not have a graphical interface to run on the server side.
Comparison of Java Servlet and CGI (Common Gateway Interface):
Compared to traditional CGI and many other CGI-like technologies, the Java servlet is more efficient, easier to use, more powerful, more portable, and less expensive to invest. In the course of future technology development, the servlet may completely replace the CGI.
* Efficient
In traditional CGI, each request starts a new process, and if the CGI program itself is executing for a short time, the overhead of starting the process is likely to exceed the actual execution time. In the servlet, each request is handled by a lightweight Java thread (rather than a heavyweight operating system process).
In traditional CGI, if there are n concurrent requests for the same CGI program, the CGI program's code is repeatedly loaded n times in memory, and for the servlet, the request is n threads, and only one servlet class code is required. In terms of performance tuning, the servlet also has more options than CGI.
* Convenient
The servlet provides a number of utility routines, such as automatically parsing and decoding HTML form data, reading and setting HTTP headers, processing cookies, tracking session state, and so on.