Two parameters can be defined in web.xml:
1. Parameters within the application scope are stored in the servletcontext and configured as follows in web.xml:
Copy the code code as follows:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
2. Parameters within the servlet scope can only be obtained in the init() method of the servlet and are configured as follows in web.xml:
Copy the code code as follows:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
In the servlet, it can be accessed separately through code:
Copy the code code as follows:
package com.qisentech.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class MainServlet extends HttpServlet {
public MainServlet() {
super();
}
public void init() throws ServletException {
System.out.println(this.getInitParameter("param1"));
System.out.println(getServletContext().getInitParameter("context/param"));
}
}
The first parameter can be obtained in the servlet through getServletContext().getInitParameter("context/param")
The second parameter can only be obtained in the init() method of the servlet through this.getInitParameter("param1")