1. Introduce ServletContext
ServletContext is officially called servlet context. The server will create an object for each project, which is the ServletContext object. This object is globally unique, and all servlets within the project share this object. So it is called global application sharing object.
2. Usage
All of the following post methods are registered in the same servlet
<servlet> <servlet-name>test</servlet-name> <servlet-class>com.workhah.servlet.ServletContextTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping>
(1) getInitParameterNames and getInitParameter
- Get a list of all parameter names
java.util.Enumeration getInitParameterNames() - Gets the parameter value according to the specified parameter name
java.lang.String getInitParameter(String name)
web.xml
<context-param> <param-name>t1</param-name> <param-value>1</param-value> </context-param> <context-param> <param-name>t2</param-name> <param-value>2</param-value> </context-param>
servlet
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get servletcontext ServletContext servletContext = getServletContext(); /* Get a list of all parameter names java.util.Enumeration getInitParameterNames() Gets the parameter value according to the specified parameter name java.lang.String getInitParameter(java.lang.String name) */ // Get all global configuration parameter names first Enumeration<String> names = servletContext.getInitParameterNames(); // Traversal iterator while(names.hasMoreElements()){ // Gets the parameter name of each element String parameName = names.nextElement(); // Get parameter value according to parameter name String parameValue = servletContext.getInitParameter(parameName); // Print System.out.println(parameName+"="+parameValue); } }
The output result is:
t1=1
t2=2
(2) getMajorVersion and getMinorVersion
- Returns the major version of the Java Servlet API supported by this servlet container.
int getMajorVersion() - Returns the minimum version number of the Servlet API supported by this servlet container.
int getMinorVersion()
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get servletcontext ServletContext servletContext = getServletContext(); /* Returns the major version of the Java Servlet API supported by this servlet container. int getMajorVersion() Returns the minimum version number of the Servlet API supported by this servlet container. int getMinorVersion() */ System.out.println(servletContext.getMajorVersion()); System.out.println(servletContext.getMinorVersion()); }
(3) getRealPath and getContextPath
-
Get current project name
String getRealPath(String path) -
Obtain the absolute path of the resource on the server according to the relative path
String getContextPath()
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get servletcontext ServletContext servletContext = getServletContext(); // Page display content resp.getOutputStream().write(("Project Name:"+ servletContext.getContextPath() + "\n" + servletContext.getRealPath("/file/computer.png") ).getBytes()); }
The result is:
(4)getRequestDispatcher
RequestDispatcher getRequestDispatcher(String name)
Returns the RequestDispatcher of the servlet or JSP with the specified name or path. If the RequestDispatch cannot be created, null is returned. If you specify a path, it must start with "/" and be relative to the top of the servlet context. This is what we often call request forwarding
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get servletcontext ServletContext servletContext = getServletContext(); // Forward request to index JSP (url unchanged) servletContext.getRequestDispatcher("/index.jsp").forward(req, resp); }
result:
(5) getMimeType and getResourceStream
- Returns the MIME type of the specified file name. Typically, it is based on the file extension, not the contents of the file itself (it may not have to exist). If the MIME type is unknown, null can be returned. The return values include text/plain, text/html, image/jpeg, image/png, application/json, and so on.
String getMimeType(String fileName)
For more information about MIME, please refer to here - By default, resources are fetched from the web root directory and returned in the form of byte stream
InputStream getResourceAsStream(String path)
Click here for specific examples
(6) setAttribute and removeAttribute and getAttribute
- Add a new attribute with a specified name and value, or set an existing attribute to the specified value.
void setAttribute(String s,Object o) - Gets the value of the property with the specified name
Object getAttribute(String s) - Removes the property with the specified name
void removeAttribute(String s)
The setAttribute, getAttribute and removeAttribute of servletContext are similar to those of request and session, but the life cycle is different. The cycle ends when the server is shut down. In fact, servletContext and application are the same, which is equivalent to a class creating two variables with different names. The difference between the two is that application is used in jsp and servletContext is used in servlet.