Learning jsp
, more or less you will come into contact with it. When you Servlet
first come into contact Servlet
with it, I think it is a very magical thing. Just put it web.xml
there and configure it inside, and it can be used.
As the understanding of it deepens, it is understood that it Servlet
is actually a specification , a specification formulated to unify the calls of Web
the container manufacturers to the JavaWeb
program. With this specification, programmers can focus on implementing functions.
The life cycle
Servlet
The life cycle is mainly divided into three stages:
- Initialization phase:
init()
method; - Invocation phase:
service()
method,doGet()
method,doPost()
method; - Destruction Phase:
destroy()
Method.
init initialization
init()
The method is used as Servlet
an initialization method, which runs only once in the entire life cycle. By default, it is the first time a user calls this method Servlet
. It can also be configured to be Web
loaded when the container starts. After that, whenever a user calls this method Servlet
, the A new thread will be spawned.
call phase
Let's talk about service()
the method first. This method is Servlet
the core of the whole, and all requests must first arrive at this method.
After the user request arrives at the Web
container, the web
container generates HttpServletRequest
the instantiated object of HttpServletResponse
the class and the instantiated object of the class, and passes them into the service()
method, and then calls the method or method respectively according to GET
/ request .POST
doGet()
doPost()
doGet()
Method: This method is called when the HTTP
request is GET
made. Generally, if the request type is not set, the default is GET
.
doPost()
Method: This method is called when the HTTP
request is POST
made. In addition, the reason why the doPost()
method is called first in the method is doGet()
just to make the fault tolerance rate larger in my opinion.
destroy destroy
destroy()
After the method is executed , Servlet
it will enter the destruction phase, and then it will be marked as garbage and recycled by the jvm
garbage collection mechanism.
Usually at this stage we perform some finishing touches such as closing the database, cleaning up threads, etc.
configure
Before Servlet 3.0
, we need to web.xml
configure this in the file:
<!-- servlet 标签必须和 servlet-mapping 标签成对使用,否则将无法使用 -->
<!-- servlet 标签和 servlet-mapping 标签的 servlet-name 必须一致 -->
<!-- servlet-name 可以随便取,但是不能和别的 Servlet 的 servlet-name 重复 -->
<!-- servlet-class 是该 Servlet 的完全限定名 -->
<!-- url-pattern 是该 Servlet 的调用路径,通常都是以 '/' 为开头,少数特殊情况下用 *.xxx 的形式 -->
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.vingyun.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
After Servlet 3.0
that, we only need to Servlet
add such an annotation to the header to complete Servlet
the declaration:
@WebServlet("/Test") //括号里的为该Servlet的调用路径
public class Test extends HttpServlet {
}
Function realization
Servlet
In fact, the writing method of the function implementation in jsp
is not much different from the usage of the writing method in in, where HttpServletRequest
the instantiated object of HttpServletResponse
the class and the instantiated object of the class can be equivalent to jsp
the built-in object request
and response
.
In addition, the database connection (JDBC) will be summarized in a later article .