Two parameters can be defined in web. xml:
1. parameters within the application range are stored in servletcontext and configured in web. xml as follows:
Copy codeThe Code is as follows:
<Context-param>
<Param-name> context/param </param-name>
<Param-value> avalible during application </param-value>
</Context-param>
2. servlet parameters can only be obtained in the servlet init () method. The configuration in web. xml is as follows:
Copy codeThe Code is 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 servlet, you can use the following code:
Copy codeThe Code is 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 through getServletContext (). getInitParameter ("context/param") in servlet.
The second parameter can only be obtained through this. getInitParameter ("param1") in the servlet init () method.