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:
<Context-param>
<Param-Name> Context/Param </param-Name>
<Param-value> avalible during application </param-value>
</Context-param>
(2) servlet-specific parameters can only be obtained in the init () method of servlet,Configure the following in Web. xml:
<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> you can use the following code in the servlet:
Package com. Wes. 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 ("the following two parameters param1 are stored in servlet ");
System. Out. println (this. getinitparameter ("param1 "));
System. Out. println ("the following parameters are stored in servletcontext ");
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.