美文网首页
JavaWeb学习之Servlet(二)——servletCon

JavaWeb学习之Servlet(二)——servletCon

作者: yaannis | 来源:发表于2017-05-01 11:13 被阅读0次

    1.在servlet配置中添加初始参数(如下配置了用户名和密码两个参数)

     <servlet>
            <servlet-name>FirstServlet</servlet-name>
            <servlet-class>WelcomeServlet</servlet-class>
    
            <init-param>
                <param-name>username</param-name>
                <param-value>root</param-value>
            </init-param>
            <init-param>
                <param-name>pwd</param-name>
                <param-value>passwordddd</param-value>
            </init-param>
    
        </servlet>
        <servlet-mapping>
            <servlet-name>FirstServlet</servlet-name>
            <url-pattern>/firstWelcome</url-pattern>
        </servlet-mapping>
    

    2.在servlet的init方法中获取参数名和值

    @Override
        public void init(ServletConfig servletConfig) throws ServletException {
            System.out.println("init");
            Enumeration<String> params = servletConfig.getInitParameterNames();
            while (params.hasMoreElements()){
                String name = params.nextElement();
                String value = servletConfig.getInitParameter(name);
                System.out.println(name+":"+value);
            }
        }
    

    3.servletConfig还可以获取servlet的配置名称,即servlet-name

    String servletName = servletConfig.getServletName();
    

    相关文章

      网友评论

          本文标题:JavaWeb学习之Servlet(二)——servletCon

          本文链接:https://www.haomeiwen.com/subject/awuftxtx.html