Servlet is a Java application independent of platform and protocol, which can generate a dynamic web page. Servlet is a Java application located within the server side within the web server. Unlike the traditional Java application that is launched from the command line, the servlet is loaded by the web server, which must contain Java virtual machines that support servlet. Align = "marginwidth =" 0 "marginheight =" 0 "src =" http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html "frameborder =" 0 "width =" 360 "Scrolling =" no "height =" 300 "> Java servlet comparison: Similarity: * They are not independent applications, no main () method. * They are not called by the user or programmer, but is called another application (container). * They have a survival cycle, including init () and destroy () methods. Different: * Applet has a good graphical interface (AWT), running with the browser, running on the client. * Servlet does not have a graphical interface and run on the server. Comparison of Java Servlet and CGI (Common Gateway Interface): Java Servlet has higher efficiency, more easily useful, more powerful, more powerful, better, better, better portability than traditional CGI and many other similar CGI technology. More savings. In the future technology development, servlets may completely replace CGI. * Efficient in traditional CGIs, each request is to start a new process. If the CGI program itself is shorter, the overhead required to start the process is likely to exceed the actual execution time. In the servlet, each request is processed by a lightweight Java thread (not the heavyweight operating system process). In the traditional CGI, if there is a request to the same CGI program, the code of the CGI program is repeated in memory in memory; for servlet, the request is N thread, only one servlet Class code. Servlet has more options than CGI in terms of performance optimization. * Convenient servlets provide a large number of utility routines, such as automatically resolve and decode HTML form data, read, and set HTTP headers, process cookies, track session status, etc. * Powerful in servlets, many tasks that use traditional CGI programs can be done easily. For example, servlets can interact directly with the web server, while ordinary CGI programs cannot. Servlets are also capable of sharing data between individual programs, making it easy to implement the functions such as database connection pools. * The portable servlet is written in Java, and the Servlet API has a complete standard. Therefore, servlet written for IPlanet Enterprise Server does not require any substantial changes to Apache, Microsoft IIS or WebStar. Almost all mainstream servers support the servlet directly or through the plugin.
* Save investment Not only there are many cheap or even free web servers available for personal or small-scale websites, and for existing servers, if it does not support servlet, add this part of the function is often free (or only need Extreme investment). Comparison of Java Servlets and JSP (JavaServer Pages): JavaServer Pages (JSP) is a technique that implements normal static HTML and dynamic HTML mixed encoding, and JSP does not increase any functionality that cannot be implemented in nature. However, writing static HTML in JSP is more convenient, do not have to use the PrintLn statement to output each line HTML code. More importantly, with the separation of content and appearance, the task of different nature in page production can be conveniently separated: For example, the page designer is HTML design, and the space for the servlet programmer is inserted into the dynamic content.
The class and interface of Java Servlet API 2.2 make up two Java packets, namely: javax.servlet and javax.servlet.http (also include javax.servlet.jsp package, not within this article discussions). The Javax.Servlet package provides the Servlet interface necessary to control the Servlet lifecycle, which must be implemented when writing servlets. The Javax.Servlet.htttp package provides an abstract class and a general tool class that derived from the servlet interface. All servlet objects are implemented as a servlet interface. In most cases, the servlet interface is indirectly implemented as a javax.servlet.GenericServlet that has implemented servlet interfaces. Javax.Servlet package definition classes: interface requestDispatcher // Define an object to accept requests from the customer and send requests to any specified resource on the server, such as a servlet, jsp or html file. Interface servlet // Defines how all servlets must implement. Interface ServletConfig // Defines the Servlet Config object, transfer information to servlet when servlet engine is initialized in servlet. Interface servletContext // defines a series of methods to communicate with the servlets. Interface servletRequest // Defines an object to pass the customer request information to the servlet. Interface servletResponse // Defines an object that is used by servlet to send a response to the customer. Interface SingleThreadModel // is used to ensure that servlets are processed only at any time. The class genericservlet // inherits the servlet interface, defines a universal, servlet that is unrelated to the protocol. Class ServletInputStream // Defines an input stream for binary data to read the customer request from the servlet. Class servletOutputStream // Defines an output stream for transmitting binary data to the customer by servlet. Class servletexception // Defines an exception that can be thrown when servlet encounters problems. Class UnavailableException // Defines an exception that is used to specify it from a servlet that it is always or not available. Javax.servlet.http package definition classes defined class and interface: interface httpservletRequest // inherits the servletRequest interface to provide information for HTTPSERVLET. Interface httpservletResponse // inherits the servletResponse interface to provide support for HTTPSERVLET output response information. Interface httpsession // provides support for the session state of the HTTP user. Interface httpsessionBindingListener // enables an object to get notifications when adding a session or from a session. Interface httpsessionContext // is defined by servlet 2.1, which is not supported in the new version.
Class cookie // uses the cookie technology class httpservlet // in servlet to define an abstract class, inheriting the GenericServlet abstract class, should be inherited by httpservlet. Class httpsessionBindingEvent // defines an object that a series of clauses, Class Httputils //, received a series of clauses of such objects when an object that implements the HTTPSessionBindingListener interface is added or removed from //. Write an HTTPSERVLET method.
The following mainly introduces the HTTP Servlet application programming interface provided by javax.servlet.http. HTTP Servlet uses an HTML form to send and receive data. To create an HTTP Servlet, extend the HTTPServlet class, which is a subclass of the genericServlet for the HTML table with a dedicated method. The HTML form is defined by