The servlet context refers to the only object created for each web application that meets the requirements of the ServletContext interface after the container is started. Characteristics of servlet context: 1. A web application corresponds to a servlet context; 2. As long as the container is not closed, the servlet context will always exist.
Servlet Context
After the container is started, a unique ServletContext interface requirement will be created for each web application. The object is generally called the Servlet context.
(Video tutorial recommendation: java course)
Servlet context characteristics
Uniqueness: one web application corresponds to one Servlet context.
Persistence: As long as the container is not closed and the application is not uninstalled, the Servlet context will always exist.
How to get the Servlet context
HttpSession, GenericServlet provides the getServletContext method to get the context
Note: GenericServlet is the parent class of HttpServlet
Example: Use Servlet context to read global initialization parameters
(1) Configure global initialization parameters
<context-param> <param-name>company</param-name> <param-value>PHP中文网</param-value> </context-param>
(2) Read
Read global initialization parameters through the methods provided by ServletContext
String company = sctx.getInitParameter("company");
Related recommendations: java introductory tutorial
The above is the detailed content of What is servlet context. For more information, please follow other related articles on the PHP Chinese website!