Reference article: https://blog.csdn.net/qq_36371449/article/details/80314024
***
1, Introduction to ServletContext
1. Concept
-
ServletContext is also called Servlet context. When the WEB container starts, it will create a corresponding ServletContext object for each WEB application, which represents the current WEB application. This object is globally unique and shared by all servlets of the project class, which is called global application shared object
-
The reference of ServletContext object is maintained in ServletConfig object. When writing servlet, developers can use ServletConfig The getservletcontext method obtains the ServletContext object.
-
All servlets in a WEB application share the same ServletContext object, so the communication between Servlet objects can be realized through the ServletContext object. ServletContext objects are also commonly referred to as context domain objects.
2. Function
- Is a domain object
- Global configuration parameters can be read
- You can search the resource files under the current project directory
- You can get the name of the current project
3. Access
3.1 get in the implementation class
- Obtained through getServletContext() provided by GenericServlet
ServletContext servletContext = getServletContext();
- Get through getServletContext() provided by ServletConfig
ServletContext servletContext = getServletConfig().getServletContext();
- Obtained through HttpServletRequest
ServletContext servletContext1 = req.getServletContext();
- Get through HttpSession
ServletContext servletContext = req.getSession().getServletContext();
3.2 get in Spring container
- In the WEB environment, starting tomcat will create a ServletContext object, and then Spring will inject this object into the Spring container. We just need to get it through annotation
@Autowired private ServletContext servletContext;
2, ServletContext usage
1. As the scope object
1.1 scope introduction
Domain object is the storage space created by the server in memory, which is used to transfer and share data between different dynamic resources (servlet s).
1.2 scope method
- Which scope object calls the method will operate on the corresponding scope data
Scope related methods | effect |
---|---|
Object setAttribute("key") | Get a value from it |
void setAttribute("key", Object data) | Store key value pair data into the scope |
void removeAttribute("key") | Delete key value pair data of scope |
1.3 code implementation of domain object
- Realize multiple servlets, realize data sharing through ServletContext object, and use ServletContext object to store the data to be shared in the Service method of InitServlet
/*Get ServletContext object*/ ServletContext context = this.getServletContext(); //Save shared data context.setAttribute("name", "haha"); // In other servlets, use the Servlet Context object to obtain shared data /*Get ServletContext object*/ ServletContext context = this.getServletContext(); //Get shared data String name = context.getAttribute("name"); System.out.println("Shared content value is:"+name);
2. Get the initialization parameters of WEB application.
2.1 method
- Gets the parameter value according to the specified parameter name
getServletContext().getInitParameter(name);
- Get a list of all parameter names
getServletContext().getInitParameterNames();
2.2 code implementation
- web. Global parameters in XML file
<!-- Global configuration parameters, because they do not belong to any one servlet,But all servlet Can pass servletContext Read this data --> <context-param> <param-name>param1</param-name> <param-value>value1</param-value> </context-param> <context-param> <param-name>param2</param-name> <param-value>value2</param-value> </context-param>
- Read web Global parameters in XML file
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Use servletContext to read global configuration parameter data //Core method /*getServletContext().getInitParameter(name);//Gets the parameter value according to the specified parameter name getServletContext().getInitParameterNames();//Get a list of all parameter names*/ //Print all parameters //1. Get the names of all global configuration parameters first Enumeration<String> enumeration = getServletContext().getInitParameterNames(); //2. Iterator traversal while(enumeration.hasMoreElements()){ //Gets the parameter name of each element String parameName = enumeration.nextElement(); //Get parameter value according to parameter name String parameValue = getServletContext().getInitParameter(parameName); //Print System.out.println(parameName+"="+parameValue); } }
3. Obtain the resource file under the current project
3.1 method
- Obtain the absolute path of the resource on the server according to the relative path
getServletContext().getRealPath(path),
- Get the input byte stream of resources on the server according to the relative path
getServletContext().getResourceAsStream(path)
4. Get the name of the current project
- Gets the name of the current project
getServletContext().getContextPath();
summary
The above is a summary of the introduction and usage of ServletContext. The code is for reference only. Welcome to discuss and exchange.