ServletConfig and servletContext in servlet

ServletConfig object : servlet configuration object, which mainly encapsulates the initialization parameters of servlet into this object.

There may be multiple servletConfig objects in a website, and a servletConfig object encapsulates the configuration information of a servlet.

You can configure the initialization parameters through the <init-param></init-param> tag in web.xml, and get the initialization parameters through the getInitParameter(String name) or getInitParameterNames() method.

When the current Servlet executes the init initialization method, it is an object belonging to the current servlet .

Note : The initialization parameters must be used in the current servlet program.

ServletContext object : servlet context object.

The servlet container loads the web application at startup and creates a unique servlet context object for each web application

A website will only create one servletContext object, which represents the environmental information of the entire website.

You can configure global parameters through the <context-param></context-param> tag in web.xml. The servletContext object is obtained through the servletConfig object, and the getServletContext() method is called.

It is generated after the web server is started, so in each servlet, the ServletContext object can be obtained through getServletConfig0.getServletContext()

Shared data can be stored in ServletContext

Note : If a certain parameter is used by the entire website program, then the parameter can be set as a global parameter, because it can be obtained through the servletContext domain object. There are three domain objects in servle, namely: ServletContext, HttpServletRequest and HttpSession.
 

Guess you like

Origin blog.csdn.net/qq_30436011/article/details/129393764