1 分别单独给某个Servlet 以及 ServletContext 设置配置信息并获取,截图展示结果
@WebServlet(name="configServlet2",urlPatterns = "/config2",
initParams = {
@WebInitParam(name="username",value = "qwerty"),
@WebInitParam(name="password",value = "1111111")
})
public class ConfigServlet2 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
String username = servletConfig.getInitParameter("username");
System.out.println("获取到的用户名"+username);
Enumeration initParameterNames = servletConfig.getInitParameterNames();
while (initParameterNames.hasMoreElements()){
String s=initParameterNames.nextElement();
System.out.println("获取到的参数名"+s+": " +servletConfig.getInitParameter(s));
}
}
@WebServlet(name="contextServlet",urlPatterns = "/context")
public class ContextServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取配置信息
ServletContext context = getServletConfig().getServletContext();
System.out.println(context);
System.out.println("========");
Enumeration initParameterNames = context.getInitParameterNames();
while (initParameterNames.hasMoreElements()){
String s = initParameterNames.nextElement();
System.out.println(s+"对应的值:"+context.getInitParameter(s));
}
username
root
password
000000000
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)