Web. Xml can define two parameters:<context-param> and </init-param>
(1) Parameters within the application range, stored in the ServletContext, are configured in Web. Xml as follows:
1 < context-param > 2 > context/param</ param-name > 3 < param-value > avalible during Application</ Param-value > 4 </ context-param >
(2) The parameters within the Servlet range can only be obtained in the servlet's init () method, which is configured in Web. Xml as follows:
1 <servlet> 2 <Servlet-name>Mainservlet</Servlet-name> 3 <Servlet-class>Com.wes.controller.MainServlet</Servlet-class> 4 <Init-param> 5 <Param-name>param1</Param-name> 6 <Param-value>Avalible in servlet init ()</Param-value> 7 </Init-param> 8 <Load-on-startup>0</Load-on-startup> 9 </servlet>
In a servlet, you can use the code separately:
Java code
Package com.test;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
public class Testservlet extends HttpServlet ... {
Public Testservlet () ... {
Super ();
}
public void Init () throws servletexception ... {
System.out.println ("The following two parameters param1 are stored in the servlet");
System.out.println (This.getinitparameter ("param1"));
System.out.println ("The following parameters are stored in the ServletContext");
System.out.println (Getservletcontext (). Getinitparameter ("Context/param"));
}
}
The first parameter in the servlet can be obtained by Getservletcontext (). Getinitparameter ("Context/param")
The second parameter can only be obtained by This.getinitparameter ("param1") in the servlet's init () method
Init-param belongs to a servlet, and Context-param belongs to the entire application, not only in Servlets, but also in JSP files.
The config in the JSP is equivalent to the Servletcontext,<%=config.getservletcontext () here. Getinitparameter ("...")%>.
Action in Servletactioncontext.getservletcontext (). Getinitparameter ("...").
Get the values defined by Context-param and Init-param in Web. xml