JavaWeb servlet learning colored pen notes

1. Configure and map this servlet in the web.xml file

<servlet>

    <servlet-name>helloServlet</servlet-name> (servlet registered name)

    <servlet-class>test.javaweb.HelloServlet</servlet-class>(servlet的全类名)

</servlet>

<servlet-mapping>

    <servlet-name>helloServlet</servlet-name> (needs to be consistent with the text node of the servlet-name child node of a servlet node)

    <url-pattern>/hello</url-pattern> (mapping specific access paths)

2. Servlet life cycle methods: The following methods are all called by the servlet container.

(1) Constructor: It is called only once, and only when the servlet is requested for the first time, the servlet instance is created and the constructor is called, which means that the servlet is a single instance!

(2) init method: It is called only once, and is called immediately after the instance is created to initialize the current servlet.

(3) service: called multiple times, the service method will be called for each request, and the timing is used to respond to the request.

(4) Destory: It is called only once, and is called before the WEB application where the current servlet is located is unloaded to release the resources occupied by the current servlet.

3.load-on-startup parameters:

(1) Configured in the servlet node:

<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class> HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

You can specify the time when the servlet is created. The smaller the value, the earlier it will be created. If it is 0 to a positive number, the instance will be created when the servlet container is loaded.

If negative, it will be created on the first request.


The servlet program must be started and run through the servlet container and stored in the web-inf/class directory of the WEB application directory

The servlet program must be registered and mapped in the web.xml file of the WEB application before it can be loaded by the servlet engine and accessed by the outside world.

A <servlet> element is used to register a servlet and it contains two main sub-elements <servlet-name> and <servlet-class> which are the registration name and the full class name respectively

A <servet-mappng> element is used to map an external access path of a registered servlet. It contains two sub-elements: <servlet-name> and <url-pattern> are the registered name and external access path of the servlet, respectively

The same servlet can be mapped to multiple urls, that is, a servlet can have multiple <servlet-mapping> matching it, and multiple <servlet-name> can be the same.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769629&siteId=291194637