[Java Web] Java Server Page Summary (Servlet Chapter)

Learning jsp, more or less you will come into contact with it. When you Servletfirst come into contact Servletwith it, I think it is a very magical thing. Just put it web.xmlthere and configure it inside, and it can be used.

As the understanding of it deepens, it is understood that it Servletis actually a specification , a specification formulated to unify the calls of Webthe container manufacturers to the JavaWebprogram. With this specification, programmers can focus on implementing functions.

The life cycle


ServletThe life cycle is mainly divided into three stages:

  1. Initialization phase: init()method;
  2. Invocation phase: service()method, doGet()method, doPost()method;
  3. Destruction Phase: destroy()Method.

init initialization

init()The method is used as Servletan 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 Webloaded 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 Servletthe core of the whole, and all requests must first arrive at this method.

After the user request arrives at the Webcontainer, the webcontainer generates HttpServletRequestthe instantiated object of HttpServletResponsethe 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 .POSTdoGet()doPost()

doGet()Method: This method is called when the HTTPrequest is GETmade. Generally, if the request type is not set, the default is GET.

doPost()Method: This method is called when the HTTPrequest is POSTmade. 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 , Servletit will enter the destruction phase, and then it will be marked as garbage and recycled by the jvmgarbage 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.xmlconfigure 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.0that, we only need to Servletadd such an annotation to the header to complete Servletthe declaration:

@WebServlet("/Test")    //括号里的为该Servlet的调用路径
public class Test extends HttpServlet {

}

Function realization


ServletIn fact, the writing method of the function implementation in jspis not much different from the usage of the writing method in in, where HttpServletRequestthe instantiated object of HttpServletResponsethe class and the instantiated object of the class can be equivalent to jspthe built-in object requestand response.

In addition, the database connection (JDBC) will be summarized in a later article .

Guess you like

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