Java Web Learning Notes: Servlet life cycle and access from the front desk to form the background according to form

1. instantiation

Create a Servlet when the first request arrives, the server automatically call the constructor method of ServletDemo3 Servlet (), executed once

2. Initialize

Preceding the first transmission request, the server calls the init method of the Servlet automatically performed once

3. request arrives

The server automatically calls the service method of the Servlet, anterior send a request, called once to perform multiple, provides for HttpServlet doGet () method and doPost () method service () method actually includes two
4 server destruction

When the server calls deStroy destroyed servlet instance () method server automatically calls

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/yaya")
public class ServletDemo3 implements Servlet {
   public ServletDemo3() {
	System.out.println("实例化我只执行一次噢");
}
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("销毁");
		
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("这是第一次访问 我只执行一次");
		
	}

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("这是第n次访问 我每次都执行");
	}

}

Front page of the access method

1. According to @WebServlet at the start of the background ( "/ yaya") access

<form action="yaya" method="post">
 <input type="submit">

2. If the background is not @WebServlet ( "/ yaya"), this time need to change the web.xml Here Insert Picture Description
to join

<servlet>
 	<servlet-name>123</servlet-name>
 	<servlet-class>lin.ServletDemo3</servlet-class>  <!--包名.类名 -->
</servlet>
<servlet-mapping>
	<servlet-name>123</servlet-name>
	<url-pattern>/yaya</url-pattern> <!-- form中action提交的东西 -->
</servlet-mapping>
``
Released six original articles · won praise 2 · Views 153

Guess you like

Origin blog.csdn.net/qq_42950149/article/details/103358608