Introduction to the Servlet
The servlet is a server-side applet, but not necessarily a Web server applet, he can also be a small program on the SMTP side, or mail server applet, it is only sun for Java EE set a standard, sun for SE provides a powerful class library, But Sun did not provide a class library for Java EE, which could not be provided, and he only provided such a set of standards. Three-stream companies to sell products, second-rate companies to sell services, first-class companies to sell standards, Sun may be a first-class company, but is now being acquired by Oracle ...
Javax.servlet.Servlet is an interface that defines the standards of such a program, and all servers that implement the Servlet program must implement this standard system, and the Tomcat server we use often implements such a standard system.
Genericservlet implements the Servlet interface, and he is an abstract class, and we write the HTTP protocol program, generally using the HttpServlet class, which inherits the Genericservlet, And the HttpServlet class overloads the Service (Servletrequest,servletresponse) method in Genericservlet, turning it into a service (HttpServletRequest, HttpServletResponse), which is more in line with the standards of the Web protocol, and it is implemented simply by forcing ServletRequest and Servletresponse classes to HttpServletRequest, HttpServletResponse.
So we write a servlet program directly inherit HttpServlet class can, and rewrite a method on the line, is generally doget,dopost, do not recommend rewriting the service method, because the service will go to determine what the browser is the way to request, Then call doget or Dopost, or other methods.
Because Tomcat implements a standard system of servlet, there must be a Servlet-api.jar file in Tomcat's Lib directory, which is the servlet system implemented by the Tomcat server.
Two. Web server program Structure
1.web Program Home directory, recorded as Hello
2. The main directory must have a INF directory, INF directory must have a standard web.xml file, the simplest format is as follows:
View plain <?xml version= "1.0" encoding= "iso-8859-1"?> <web-app xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "version=" 2.5 "> </web-app>
This is the simplest Web program, of course static Web pages are not discussed
3. The general Web program in the INF directory will also have a classes directory, dedicated to the compiled class file, there will be a Lib folder, to run the Web program necessary to the jar pack
Three. First Servlet program
1.HelloWorldServlet class:
View Plain Package com.servlet;