init-param and context-param

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

What is the difference between and !?

5条回答
  •  有刺的猬
    2020-12-02 05:18

    and are static parameters which are stored in web.xml file. If you have any data which doesn't change frequently you can store it in one of them.

    If you want to store particular data which is confined to a particular servlet scope, then you can use .Anything you declare inside is only accessible only for that particular servlet.The init-param is declared inside the tag.

    
         HelloWorldServlet
         HelloWorldServlet
         
             Greetings
             Hello
         
    
    

    and you can access those parameters in the servlet as follows:

    out.println(getInitParameter("Greetings"));
    

    If you want to store data which is common for whole application and if it doesn't change frequently you can use instead of servletContext.setAttribute() method of the application context. For more information regarding usage of VS ServletContext.setAttribute() have a look at this question. context-param are declared under the tag web-app. You can declare and access the as follows

    
        
            Country
            India
        
        
            Age
            24
        
    
    

    Usage in the application either in a JSP or Servlet

    getServletContext().getInitParameter("Country");
    getServletContext().getInitParameter("Age");
    

提交回复
热议问题