I. use servlet 2.5 to initialize parameters
Servlet 2.5 is configured based on xml, so it is configured in xml.
Servlet 3.0 started to support annotation configuration.
There are two kinds of configurations: valid in the current Servlet and valid in the entire web container.
When the entire web container is valid, both 2.5 and 3.0 (and above) must be configured in xml.
Use < context param > < context param > when the configuration of the entire web container is valid.
When the current Servlet is configured to be valid, use < init param > < init param > in the corresponding < Servlet > < Servlet >.
The configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>globalParamName</param-name> <param-value>globalParamValue</param-value> </context-param> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>TestServlet</servlet-class> <init-param> <param-name>servletParamName</param-name> <param-value>servletParamValue</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>
Read:
public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TestServlet() { } @Override public void init() throws ServletException { super.init(); String value = super.getInitParameter("servletParamName"); System.out.println("current Servlet Of servletParam The value is"+value); ServletContext sc = super.getServletContext(); String gValue = sc.getInitParameter("globalParamName"); System.out.println("current Web Container gobalValue by"+gValue); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2. Use Servlet3.0 to initialize parameters
initParams= {@WebInitParam(name="servletnameParam30",value="servletValue30")}
name and value correspond respectively
<param-name> <param-value>
@WebServlet(value="/TestServlet",loadOnStartup=1,initParams= {@WebInitParam(name="servletnameParam30",value="servletValue30")}) public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TestServlet() { // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { super.init(); String value = super.getInitParameter("servletnameParam30"); System.out.println("current Servlet Of servletParam30 The value is"+value); ServletContext sc = super.getServletContext(); String gValue = sc.getInitParameter("globalParamName"); System.out.println("current Web Container gobalValue by"+gValue); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }