Javaweb Learn what a servlet is, how to use a servlet, why this is used, the virtual path of the servlet (2)

Source: Internet
Author: User

 1. What is a servlet?    * Server-side Java programs, Servlets need to be handed to the server to run. * Java program related to Javax.servlet.Servlet interface 2, how to use servlet? "Must" * Create a WEB project * Create a class and implement a servlet interface * to the server to run/web-inf/web.xml<!--Register Servlets <servlet> Tags: servlet registration <servlet-class> Tags: determining where a servlet class is located                        * Content: The full pathname <servlet-name> tag of the servlet class that needs to be registered: an alias for the servlet, provided for use by other programs * Content: Custom * required, must be unique -        <servlet>            <Servlet-name>HelloWorld</Servlet-name>            <Servlet-class>Com.yxl.HelloWorld</Servlet-class>        </servlet>        <!--servlet Mappings <servlet-mapping> Tags: mapping an existing servlet <servlet-name> tag: determining the Used The name of the servlet * content: From the previously configured <servlet><servlet-name> content <url-pattern> Tags: ok Browser Access Path * Content: Custom * Requirements: Must start with "/", name unique -        <servlet-mapping>            <Servlet-name>HelloWorld</Servlet-name>            <Url-pattern>/hello</Url-pattern>        </servlet-mapping>* Test: Http://localhost:8080/day05/hello 3, why use this?            * Javax.servlet.Servlet life cycle: * init (servletconfig servletconfig) * Servlet initialization method, number of executions: 1 times * Javax.servlet.ServletConfig Description of configuration information for current servlet * SERVLETCONFIG.GETSERVLETNAME (): Returns the name of the current servlet: <servlet><Servlet-name>contents of configuration * Servletconfig.getinitparameter ("paramname"): Returns the value of the specified initialization parameter name, reading the configuration file Web. XML * <servlet>                            <Servlet-name>helloWorld222</Servlet-name>                            <Servlet-class>Cn.itcast.HelloWorld2</Servlet-class>                            <!--Configuring the initialization parameter list -                            <Init-param>                                <!--the name of the initialization parameter -                                <Param-name>Gf</Param-name>                                <!--Initialize the value of the parameter -                                <Param-value>Fengjie</Param-value>                            </Init-param>                        </servlet>                * Servletconfig.getinitparameternames (): Returns the name of all initialization parameters of the current servlet * return value: Java.util.Enumera tion-hasmoreelements (), nextelement () *servletcontext ServletContext = Servletconfig.getservletconte XT (): ServletConfig save ServletContext Reference to current Web project [suspense] *service (servletrequest,servletresponse) "* *" * Current Ser  Vlet method called on request and Response * 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.cata Lina.connector.RequestFacade * Relationship: * Public class Requestfacade implements HttpS  Ervletrequest {* Public interface HttpServletRequest extends ServletRequest {* Summary: Requestfacade--HttpServletRequest--ServletRequest * Results: Javax.servlet.http.HttpServlet Request Request = (HttpSErvletrequest) req; * Response * Interface: Javax.servlet.ServletResponse * Implementation class: Org.apache.catalina.connector.Re Sponsefacade * Relationship: * Public class Responsefacade implements HttpServletResponse {* Public interface HttpServletResponse extends Servletresponse {* Summary: respons Efacade--HttpServletResponse-Servletresponse * Results: Javax.servlet.http.HttpServletResponse        Response = (httpservletresponse) resp;         * Destroy () * Current servlet destruction method, Recycle resource * Javax.servlet.GenericServlet class "See" This,super * generic Servlet implementation class, Protocol Independent * This class is abstract class, with abstract method service (servletrequest,servletresponse) * Public abstract Void Service (ServletRequest re        Q, 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. (ServletConfig instance object is cached) * public void init (ServletConfig config) throws servletexception {This                . config = config;              This.init ();            } * This class is recommended to overwrite 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 for reasons that require subclasses to make HttpServlet class method overrides * There is no reason for abstract methods: 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, which returns the request method of the current servlet, facilitates and handles different requests * Summary: "* *" * Create a class that inherits the HttpServlet class * Based on requirements, Cover Dopost and Doget method * need to be configured in Web. * Test * NOTE: * If initialization work is required: overwriteInit () method * If the initialization work takes a lot of time, you need to set the servlet to initialize when the server starts.  *<servlet><Load-on-startup>2</Load-on-startup>* Configure current servlet load order at server startup, value: 0-6, lower value, priority load 4, servlet virtual path refers to<servlet-mapping>            <Servlet-name>HelloWorld</Servlet-name>            <Url-pattern>/hello</Url-pattern>        </servlet-mapping>in<Url-pattern>Path 1, only use/or * start 2,/and *. Cannot exist at the same time 3,/or/* indicates any 4, only *. To indicate a wildcard character

Javaweb Learn what a servlet is, how to use a servlet, why this is used, the virtual path of the servlet (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.