Detailed description of JSP development Servlet rewriting init () method instance, servletinit

Source: Internet
Author: User

Detailed description of JSP development Servlet rewriting init () method instance, servletinit

Detailed description of JSP development Servlet rewriting init () Method Instance

When writing a Servlet, we sometimes need to rewrite the initialization method of the Servlet. Then, whether to rewrite init (ServletConfig config) or init () is a problem. To understand this, you must first know the connection between several Servlet classes and interfaces:

ServletConfig interface: four methods are available: getServletName (); getServletContext (); getInitParameter (String name); and getInitParameterNames. Servlet interface: contains the init (ServletConfig config) method | ---> GenericServlet abstract class: private transient ServletConfig config; Attribute | ---> HttpServlet class

By viewing the source code, it is found that there is no init method in HttpServlet. in the abstract class of GenericServlet, the Servlet # init (ServletConfig config) method is implemented. In addition, a new init () method without parameters is added. Besides, the init method without parameters is called in the init method with parameters.

It should be known that during Servlet initialization, init (ServletConfig config) will be automatically called, and Container will automatically collect some Servlet configuration information and generate a ServletConfig instance, by calling the four getXXX methods of the Instance (four methods in the ServletConfig Interface), we can obtain the configuration information of the Servlet.

How can we obtain this instance? It is obtained through Servlet # getServletConfig (). This method is also implemented in GenericServlet, as follows:

public void init(ServletConfig config) throws ServletException {   this.config = config;   this.init(); } public ServletConfig getServletConfig() {   return config; }   

I think the emergence of init () is to solve a problem, that is, some people first rewrite the init (ServletConfig config) method, but always forget to call "super. init (config );". Imagine that if this happens, the Servlet configuration information collected by the container cannot be initialized to the config attribute of GenericServlet, so that when getServletConfig () is called () but only one null value can be obtained. In this way, the Servlet configuration information cannot be obtained.

The advent of the init () method without parameters in GenericServlet solves the embarrassment of manually failing to obtain the ServletConfig object. We can see that the init method containing the parameter calls the init method without the parameter. This allows us to rewrite the init () method without the parameter when processing the Servlet initialization parameter. The config object initialization operation is still completed when the Container calls init (ServletConfig config), and then calls the init () method you have rewritten to complete other initialization operations.

In summary, there are no strict rules on whether to override the init method with or without parameters. If you rewrite the init method with parameters, you must first call the init method with parameters of the parent class. The two types of calls are as follows:

public void init(ServletConfig config) throws ServletException {     super.init(config);     //...code... } public void init() throws ServletException {     //...code... } 

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.