5-10Servlet

5-10Servlet,第1张

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
    

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存