servlet basis (life cycle, inherited basis)

1. Writing custom servlet, inheritance:

Servlet is an interface developed servlet has three ways, one is to achieve the Servlet interface, one is inherited GernericServlet Another is inherited HttpServlet.

HttpServlet is related to hypertext protocol http servlet, GernericServlet protocol-independent and is generic servlet.

      mysetvlet inherited HttpServlet, rewrite doPost () and doGet () method, HttpServlet achieve GernericServlet class, GernericServlet implements the Servlet interface, Servlet interface defines the init () initialization method, service () method, destroy also called), before (initialization examples of the method of construction, service () method of determining or post requests get request

 

2. Configure web.xml

<? XML Version = "1.0" encoding = "UTF-. 8"?>
<Web-App Version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
    xmlns: the 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 ">
    
    <-! a first configuration servlet ->
    <servlet>
        <-! servlet to a name, the name of the variable in accordance with the naming convention may be defined names. ->
        <the servlet-name> OneServlet </ the servlet-name>
        ! <- This class is described in the project path containing the class name and the package name of the class where the class configuration as following:
        for TEST05 class is located package name, OneServlet class name ->
        <the servlet-class> test05.OneServlet </ the servlet-class>
        
        <!

        /////// greater than 0 loads the Tomcat servlet, integers are higher priority
        <Load-ON-Startup>. 1 </ Load-ON-Startup>
        
    </ the servlet>
    
    <-! How to Access Configuration this servlet ->
    <servlet-Mapping>
        <-! quote the name of the servlet class defined above ->
        <servlet-name> OneServlet </ servlet-name>
        <-! url to access the configuration name of the servlet class name According to the variable naming convention can customize the name.
        As follows: / one, this name will be used in class when accessing OneServlet 
        example: 127.0.0.1: 8080 / Project Name / One ->
        <url-pattern> / One </ url-pattern>
    </ Mapping-the servlet>
    
< / web-app>

Guess you like

Origin blog.csdn.net/song_chengbo/article/details/96865883
Recommended