Web. two parameters can be defined in 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> <param-Name> URL </param-Name> <param-value> JDBC: mysql: // localhost/wjlmgqs </param-value> </init-param> <load-on-startup> 0 </load-on-startup> </servlet>
(1) parameters within the application range are stored in servletcontext and configured in Web. XML as follows:
XML Code
<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 servlet Init () method. The configuration in Web. XML is as follows:
XML Code
In servlet, you can use the following code:
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 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.
Init-Param belongs to a servlet, and context-Param belongs to the whole application, which can be obtained not only in the servlet, but also in the JSP file.
In JSP, config is equivalent to servletcontext, <% = config. getservletcontext (). getinitparameter ("...") %>.
Servletactioncontext. getservletcontext (). getinitparameter ("...").