有两种:
第一种:
通过ServletContext,web.xml配置如下
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
ServletContext test = getServletContext();
String password= test.getInitParameter("password");
}
注意是ServletContext 这个类,本人就有第二种混淆了。
第二种
通过ServletConfig,web.xml配置如下
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
ServletConfig sc = getServletConfig();
String user= sc .getInitParameter("user");
}
这种事要取servlet中的初始值的。所以初始值要写进servlet中。