init-param and context-param

前端 未结 5 1416
说谎
说谎 2020-12-02 04:29

What is the difference between and !?

5条回答
  •  醉酒成梦
    2020-12-02 05:28

    Consider the below definition in web.xml

    
        HelloWorld
        TestServlet
        
            myprop
            value
        
    
    

    You can see that init-param is defined inside a servlet element. This means it is only available to the servlet under declaration and not to other parts of the web application. If you want this parameter to be available to other parts of the application say a JSP this needs to be explicitly passed to the JSP. For instance passed as request.setAttribute(). This is highly inefficient and difficult to code.

    So if you want to get access to global values from anywhere within the application without explicitly passing those values, you need to use Context Init parameters.

    Consider the following definition in web.xml

     
          
               myprop
               value
          
     
    

    This context param is available to all parts of the web application and it can be retrieved from the Context object. For instance, getServletContext().getInitParameter(“dbname”);

    From a JSP you can access the context parameter using the application implicit object. For example, application.getAttribute(“dbname”);

提交回复
热议问题