One, servlet concept: servlet (Server Applet), full name Java Servlet, no Chinese translation. is a server-side program written in Java. Its main function is to interactively browse and modify data to generate dynamic Web content. The narrow-sense servlet refers to the Java language implementation of an interface, the generalized servlet refers to any implementation of the Servlet interface class, in general, people will understand the servlet as the latter. The servlet runs in a Java-enabled application server. From the implementation, the servlet can respond to any type of request, but in most cases the servlet is used only to extend the HTTP protocol-based Web server. Second, the first servlet program:
Create a HelloServlet class, inherit the Servlet interface, and implement the method of the interface. public class HelloServlet implements servlet{...}
PackageCom.iflytek.servlet;Importjava.io.IOException;ImportJavax.servlet.Servlet;ImportJavax.servlet.ServletConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classHelloServletImplementsServlet { PublicHelloServlet () {System.out.println ("Constructor ..."); } @Override Public voidInit (servletconfig config)throwsservletexception {System.out.println ("Init ..."); } @Override Public voidService (ServletRequest req, servletresponse Res)throwsservletexception, IOException {System.out.println ("Service ..."); } @Override PublicServletConfig Getservletconfig () {System.out.println ("Getservletconfig ..."); return NULL; } @Override PublicString Getservletinfo () {System.out.println ("Getservletinfo ..."); return NULL; } @Override Public voiddestroy () {System.out.println ("Destroy ..."); }}
Iii. Configuring the Web. xml file (configuration and mapping servlet)
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5"> <!--Configuring and Mapping Servlets - <!--Configuring the Servlet - <servlet> <Servlet-name>HelloServlet</Servlet-name> <Servlet-class>Com.iflytek.servlet.HelloServlet</Servlet-class> </servlet> <!--Mapping Servlet - <servlet-mapping> <Servlet-name>HelloServlet</Servlet-name> <Url-pattern>/hello</Url-pattern> </servlet-mapping> </Web-app>
Four, start the Tomcat server, enter Http://localhost:8080/test001_Servlet/hello in the Address bar. The console output information is as follows:
V. Servlet life cycle
1. Constructor: called only once. An instance of the servlet is created only the first time the servlet is requested. Call the constructor. This shows the single instance of Serlvet!
2. Init method: called only once. Called immediately after a good instance has been created. Used to initialize the current Servlet.
3. Service: called multiple times. Each request invokes the service method. Actually used in response to a request.
4. Destroy: called only once. Called before the WEB app where the current Servlet resides is unloaded. Used to release resources occupied by the current Servlet.
Vi. the source code of the Servlet interface:
PackageJavax.servlet;Importjava.io.IOException; Public InterfaceServlet { Public voidInit (servletconfig config)throwsservletexception; Publicservletconfig getservletconfig (); Public voidService (ServletRequest req, servletresponse Res)throwsservletexception, IOException; PublicString getservletinfo (); Public voiddestroy ();}
My first servlet program and the servlet's life cycle function