1. What is a servlet?
* Server-side Java programs, Servlets need to be handed to the server to run.
* Java programs related to the Javax.servlet.Servlet interface
* Sun provides a set of servlet interfaces (APIs) specifically implemented by Web servers (e.g. tomcat, webLogic, JBoss, etc.)
2. What if a servlet is used? "Must"
* Create a Web project
* Create a class and implement the Servlet interface
* Give the server run/web-inf/web.xml
<!--registered Servlet
<servlet> Tags: servlet registration
<servlet-class> Tags: determine where the servlet class is located
* Content: The full path name of the servlet class that needs to be registered
<servlet-name> Tags: an alias for the servlet, provided to other programs to use
* Content: Custom
* required, must be unique
-
<servlet>
<servlet-name>MyFristServlet</servlet-name>
<servlet-class>com.cjq.servlet.runprocess.MyFristServlet</servlet-class>
</servlet>
<!--
Mapping of Servlets
<servlet-mapping> Tags: mapping an existing servlet
<servlet-name> Tags: Determine the name of the existing servlet used
* Content: Content from pre-configured <servlet><servlet-name>
<url-pattern> Tags: determining the path of browser access
* Content: Custom
* Required: Must start with "/", name unique
-
<servlet-mapping>
<servlet-name>MyFristServlet</servlet-name>
<url-pattern>/myfristservlet</url-pattern>
</servlet-mapping>
* Test: Http://localhost/day02/MyFristServlet (Port 80)
3. Why use this?
* life cycle of Javax.servlet.Servlet:
* Init (servletconfig)
* Servlet initialization method, number of executions: 1 times
* Javax.servlet.ServletConfig Description of configuration information for the current servlet
* Getservletname (): Returns the name of the current servlet:<servlet><servlet-name> configuration
* Getinitparameter ("paramname"): Returns the value of the specified initialization parameter name
* <servlet>
<servlet-name>ReadConfigServlet</servlet-name>
<servlet-class>com.cjq.servlet.readConfig.ReadConfigServle</servlet-class>
<!--configuration initialization parameter list--
<init-param>
<!--the name of the initialization parameter--
<param-name>name</param-name>
<!--the value of the initialization parameter--
<param-value> Cao Jianqiang </param-value>
</init-param>
</servlet>
* Getinitparameternames (): Returns the name of all initialization parameters for the current servlet
* Return Value: java.util.Enumeration--hasmoreelements (), Nextelement ()
* Getservletcontext (): ServletConfig Save a reference to the current Web project's ServletContext [scratch]
* Service (Servletrequest,servletresponse) "* *"
* methods that are called when the current servlet requests and responds
* ServletRequest: Store all user's request information
* Servletresponse: Store all information that the server responds to the user
* Summary
* Request
* Interface: Javax.servlet.ServletRequest
* Implementation class: Org.apache.catalina.connector.RequestFacade (for Tomcat)
Relationship
* Public class Requestfacade implements HttpServletRequest {
* Public interface HttpServletRequest extends ServletRequest {
* Summary: Requestfacade-HttpServletRequest-ServletRequest
* Result: Javax.servlet.http.HttpServletRequest request = (httpservletrequest) req;
* Response
* Interface: Javax.servlet.ServletResponse
* Implementation class: Org.apache.catalina.connector.ResponseFacade
Relationship
* Public class Responsefacade implements HttpServletResponse {
* Public interface HttpServletResponse extends Servletresponse {
* Summary: Responsefacade-HttpServletResponse-Servletresponse
* Results: javax.servlet.http.HttpServletResponse response = (httpservletresponse) resp;
* Destroy ()
* Current Servlet destruction method, recycling resources
* Javax.servlet.GenericServlet class "See" This,super
* Generic servlet implementation class, independent of Protocol
* This class is abstract class with abstract method service (Servletrequest,servletresponse)
* Public abstract Void Service (ServletRequest req, servletresponse Res) throws Servletexception, IOException;
* This class implements the Servlet interface, which also implements the ServletConfig interface
* Public abstract class Genericservlet implements Servlet, ServletConfig
* This class caches the values of the actual parameters of the Init method. (The ServletConfig instance object is cached)
* public void init (ServletConfig config) throws servletexception {
this.config = config;
this.init ();
}
* This class is recommended to override this method in the absence of a parametric Init method that all servlet initialization works.
* Otherwise, overriding the Init method with parameters requires adding Super.init (config) to the first line of the method;
* public void init (ServletConfig config) throws servletexception {
Super.init (config); This line code must exist
Then it's your own content.
}
* Javax.servlet.http.HttpServlet Class
* This class is an abstract class, but there is no abstract method.
* Abstract class reason, need subclass to make HttpServlet class method overwrite
* No abstract method: When subclasses override the corresponding way of the parent class, there is no mandatory requirement
* This class is an implementation class for a servlet related to Protocol (HTTP)
* Request = (httpservletrequest) req;
Response = (httpservletresponse) res;
* Request.getmethod () method, returns the current Servlet request method, facilitates and handles different requests
* Summary: "* *"
* Create a class that inherits the HttpServlet class
* Covering Dopost and Doget methods as required
* Need to be configured in Web. xml
* Test
Note
* If initialization work is required: Overwrite init () method
* If initialization work takes a lot of time, you need to set up the servlet to initialize at server startup.
* <servlet><load-on-startup>2</load-on-startup>
* Configure the current servlet load order at server startup, value: 0-6, the smaller the value, the first load
4. The virtual path of the servlet
1. Only use/or * start
2,/and *. Cannot exist at the same time
3,/or/* indicates any
4, only *. indicates wildcard characters
The re-experience of Servlet server-side programming