Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the init points of Servlet?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the init points of Servlet". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the init points for attention of Servlet?"

There are two init () methods in the GenericServleta class of Servlet, and you should pay attention to the two methods when using them:

If we write a Servlet class directly by implementing the Servlet interface, we need to implement the five methods defined in the Servlet interface. In order to simplify the writing of Servlet, we are provided with an abstract class GenericServlet in the javax.servlet package, which provides a simple implementation of the other four methods except the service () method. The GenericServlet class defines a generic Servlet that does not depend on specific protocols, which implements the Servlet interface and the ServletConfig interface.

If we are going to write a generic Servlet, we just need to inherit from the GenericServlet class and implement the abstract method service ().

In the GenericServlet class, two overloaded init () methods (source code in Tomcat) are provided:

Public void init (ServletConfig config) throws ServletException {

This.config = config

This.init ()

}

Public void init () throws ServletException {

}

The first init () method is the implementation of the init () method in the Servlet interface. In this method, you first save the ServletConfig object in a transient instance variable, and then call the second init () method with no parameters.

Usually when writing Servlet classes that are integrated from GenericServlet, you only need to override the first init () method with no parameters.

If the second init () method is overridden, a super.init (config) code call should be included in the subclass's method. (super.init (config) is GenericServlet .init (config), that is, this.config = config and this.init (). Without these two lines of code, because there is such a code in Servlet: this.getServletConfig (). GetServletName (), the code in getServletConfig () is return config, if the programmer rewrites the init method with parameters and forgets super.init (config), then config is null, getServletName () is null, and a null pointer exception occurs. So to prevent this error, programmers generally rewrite the method init (). Note: Tomcat calls the method init (ServletConfig config). )

Init (servletconfig config)

This.config = config

Init ()

Rewriting must be super.init (config)

Otherwise, you cannot use this.getServletConfig.

Of course, getInitParameter () cannot be used.

We just need to rewrite init ()

Such as:

Public void init (ServletConfig config) throws ServletException {

/ / TODO Auto-generated method stub

Super.init (config)

String num=config.getInitParameter ("num")

System.out.println ("initializer is:" + num)

}

At this point, I believe you have a deeper understanding of "what are the init points of Servlet?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report