Servlet is a server-side Java application independent of platform and protocol. It can generate dynamic Web pages.
I. Overview
Servlet is a server-side Java application independent of platform and protocol. It can generate dynamic Web pages.
Servlet is a Java application on the server inside the Web server. Unlike the traditional Java application started from the command line, Servlet is loaded by the Web server, the Web server must contain a Java virtual machine that supports Servlet.
Comparison between Java Servlet and Applet:
Similarities:
* They are not independent applications and do not have the main () method.
* They are not called by users or programmers, but by another application (container.
* They all have a life cycle, including the init () and destroy () methods.
Differences:
* The Applet has a good graphical interface (AWT) and runs on the client together with the browser.
* The Servlet does not have a graphical interface and runs on the server.
Comparison between Java Servlet and CGI (Common Gateway Interface:
Compared with traditional CGI and many other CGI-like technologies, Java Servlet is more efficient, easier to use, more powerful, more portable, and more investment-saving. In the future technological development, Servlet may completely replace CGI.
* Efficient
In traditional CGI, each request starts a new process. If the CGI program runs for a short period of time, the overhead required to start the process may exceed the actual execution time. In Servlet, each request is processed by a lightweight Java thread (rather than a heavyweight operating system process ).
In traditional CGI, if there are N concurrent requests to the same CGI program, the CGI program code is repeatedly loaded N times in the memory. For Servlet, the request is processed by N threads and only one Servlet code is required. In terms of performance optimization, Servlet also has more options than CGI.
* Convenient
Servlet provides a large number of Utility Routines, such as automatically parsing and decoding HTML form data, reading and setting HTTP headers, processing cookies, and tracking session status.
* Powerful functions
In Servlet, many tasks that are difficult to accomplish by using traditional CGI programs can be easily completed. For example, a Servlet can directly interact with a Web server, but a common CGI program cannot. Servlet can also share data among various programs, making database connection pools and other functions easy to implement.
* Good portability
Servlet is compiled in Java, and the Servlet API has perfect standards. Therefore, servlets written for the IPlanet Enterprise Server can be transplanted to Apache, Microsoft IIS, or WebStar without any material changes. Almost all mainstream servers support servlets directly or through plug-ins.
* Investment savings
Not only are there many low-cost or even free Web servers available for personal or small-scale websites, but if the existing server does not support Servlet, this feature is often free (or requires a very small investment ).
Comparison between Java Servlet and JSP (assumerver Pages:
JavaServer Pages (JSP) is a technology that implements the hybrid coding of common static HTML and dynamic HTML. JSP does not add any functions that cannot be implemented by Servlet in essence. However, it is more convenient to compile static HTML in JSP, and you do not need to use the println statement to output each line of HTML code. More importantly, with the separation of content and appearance, different types of tasks in page creation can be easily separated: for example, HTML design is performed by page designers, at the same time, we also set aside space for Servlet programmers to insert dynamic content.
Java Servlet API 2.2
Java Servlet API 2.2 consists of two Java packages: javax. servlet and javax. servlet. http (also including javax. servlet. jsp package, not discussed in this article ).
The javax. servlet package provides the Servlet interface necessary to control the Servlet lifecycle, which must be implemented during Servlet compilation.
The javax. servlet. http package provides an abstract class and a general tool class derived from the Servlet interface for processing HTTP requests. All Servlet objects must implement the Servlet interface. In most cases, it is used as the javax that has implemented the Servlet interface. servlet. genericServlet and javax. servlet. http. httpServlet subclasses of these two abstract classes indirectly implement the Servlet interface.
Javax. servlet package-defined classes and interfaces:
Interface RequestDispatcher
// Defines an object used to receive a request from a customer and send the request to any specified resource on the server, such as a Servlet, JSP, or HTML file.
Interface Servlet
// Defines the methods that must be implemented by all servlets.
Interface ServletConfig
// Define the Servlet config object. The Servlet Engine transmits information to the Servlet during Servlet initialization.
Interface ServletContext
// Defines a series of methods for the Servlet to communicate with the running environment.
Interface ServletRequest
// Defines the object used to pass the customer request information to the Servlet.
Interface ServletResponse
// Defines an object that is used by the Servlet to send a response to the customer.
Interface SingleThreadModel
// Ensure that the Servlet processes only one request at any time.
Class GenericServlet
// Inherits the Servlet interface and defines a universal Servlet that is not related to the Protocol.
Class ServletInputStream
// Defines an input stream for the Servlet to read the binary data requested by the customer.
Class ServletOutputStream
// Defines an output stream used by the Servlet to send binary data to the customer.
Class ServletException
// Defines an exception that can be thrown when the Servlet encounters a problem.
Class UnavailableException
// Defines an exception that is used by the Servlet to indicate that it is permanently or temporarily unavailable.
Javax. servlet. http package-defined classes and interfaces:
Interface HttpServletRequest
// Inherits the ServletRequest interface and provides request information for HTTPServlet.
Interface HttpServletResponse
// Inherits the ServletResponse interface to provide support for HTTP servlet OUTPUT response information.
Interface HttpSession
// Provides support for maintaining the session Status of HTTP users.
Interface HttpSessionBindingListener
// Notify an object when it is added to or deleted from a session.
Interface HttpSessionContext
// Defined by Servlet 2.1, this object is no longer supported in the new version.
Class Cookie
// Use Cookie technology in Servlet
Class HttpServlet
// Defines an abstract class that inherits the GenericServlet abstract class and should be inherited by HTTPServlet.
Class HttpSessionBindingEvent
// Defines an object. When an object implementing the HttpSessionBindingListener interface is added to a session or deleted from the session, a handle of the object is received.
Class HttpUtils
// Provides a series of methods to facilitate the compilation of HTTPServlet.
The following describes the http servlet application programming interface provided by javax. Servlet. HTTP.
HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, which is a subclass of GenericServlet that uses a special method to process HTML tables. HTML forms are defined by the <FORM> and </FORM> tags. A form typically contains input fields (such as text input fields, check boxes, radio buttons, and selection lists) and buttons used to submit data. When the information is submitted, they also specify which Servlet (or other program) the server should execute ). HttpSe