cool hit counter Servlet life cycle_Intefrankly

Servlet life cycle


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/46446961

Although in the university of jsp professional course "heard" Servlet, but at that time the awareness of Servlet, indeed stop just to stay in the "heard" stage. I remember when I first arrived at the company's internship, reported a Servlet error, the master asked me Servlet configuration is right? I came to say, "What configuration? ", it turns out that I gradually deepened my knowledge of Servlets in the course of doing DRP the other day. The following talks about the understanding of Servlet.

(The article uses log in to illustrate the business logic of。)

Initially, java web development, Client and Server work in the way that the client submits a request to the Servlet (such as submitting a user name, password), the Servlet processing (such as calling business logic), the results of the processing stitched into HTML format to return to the client:.

Client:login.html

<html>
    <head>Servlet Test</head>
    <body>
        <form method="post" action="/servlet/LoginServlet">
            <input type="input" name="userName"/>
            <input type="input" name="password"/>
            <input type="submit" name="submit"/>
        </form>
    </body>
</html>

Servlet:LoginServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 *  log in
 * @author Danny
 */
public class LoginServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.doGet(request, response);
		
response.setContentType("text/html;chartset=utf-8");
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		boolean isLoginSuccessful=Manager.Login(userName,password);
		if(isLoginSuccessful){
		response.getWriter().print(" Landing successful!");
		}else{
		response.getWriter().print(" landing failure!");
		}	
	}
}

Manager code omitted~~

In addition to this configure the Servlet in web.xml:.

  <servlet>
  	<servlet-name>LoginServlet</servlet-name> <!--Servlet your name, Can be named any way you want-->
  	<servlet-class>com.danny.servlet.LoginServlet</servlet-class> <!--Servlet The real path where-->
  </servlet>
  <servlet-mapping>
  	<servlet-name>ClientIdValidateServlet</servlet-name> <!--Servlet name, Must be consistent with the aboveServlet The names of the-->
  	<url-pattern>/servlet/LoginServlet</url-pattern><!--Servlet Virtual Path, Can be named any way you want,form ofaction is the value of the property that it-->
  </servlet-mapping>

(The meaning of each tag in the web.xml configuration file can be found in the comments that follow)

Each Servlet is a java class that inherits from the GenerateServlet class under the javax.servlet package. Its life cycle is as follows.

⑴ After the client sends the request,Servlet container( as ifTomcat) Read the configuration file, willServlet Load into memory and instantiate( anServlet Instantiate only once)。

Servlets can be instantiated in two ways.

① (located) atTomcat Instantiated at startup, In the configuration file<servlet></servlet> Add to the tag<load-on-startup>1</load-on-startup>。Servlet Containers are instantiated by property value from smallest to largest, that is<load-on-startup> The smaller the value of the property the more it is instantiated first, have a value of0 ofServlet Finally instantiated by。 owing toServlet Instantiation takes longer, So the advantage of this method is that the user waits less。

② invokeServlet Instantiated when,<load-on-startup> Attribute values less than0 time,Tomcat It doesn't start right.Servlet Perform instantiation,<load-on-startup> The default value of the attribute is less than0。

⑵ When instantiated, invokeinit() approach( This method inherits fromGenerateServlet kind) Perform initialization。

(3) Create HttpResponse, HttpRequest object, execute Service (HttpServletRequest req, HttpServletResponse res) method, according to the type of client request (post/get) to determine whether to call doPost () method or call doGet () method next. (HttpServletRequest is responsible for receiving data from the page, HttpServletResponse is responsible for writing data to the page)

⑷ When the server stops, invokedestory() approach, obliterateServlet an actual example。


Recommended>>
1、Blockchain Bitcoin price stops falling as consensus expands for reciprocal bottoming
2、On the sword of the top international tournament DEFCON Ming Dynasty Wanda An Yuan laboratory won the best AI attack and defense award
3、What is Pressone
4、The national Jedi survival server is ready but is your computer configuration enough
5、Huawei announced its AI strategy yesterday if you dont know AI X youre going to be out of business

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号