Arul Murugan
Arul Murugan

Reputation: 65

What is context param in web.xml?

What is <context-param> in web.xml? Why do we use it?

For instance, what does the following do?

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet-servlet.xml</param-value>
</context-param>

Upvotes: 6

Views: 18283

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 149155

In a spring web application, contextConfigLocation context param gives the location of the root context.

Your config is strange, for a spring-mvc application, because by default, servletname-servlet.xml (where servletname is the name of a DispatcherServlet servlet) is the child application context for the servlet.

What is current (and recommended by Spring documentation) is to have a root context that will contain the model layer (service, persistence and business beans) and a servlet context that will contain the controller and view layer (controller, view resolvers, interceptors). The rule is that bean in servlet context can use beans of root context but the reciprocal is false.

Upvotes: 5

nobalG
nobalG

Reputation: 4630

Some time you will be in a situation where you want to set some parameter and want to access it through out your whole web application.Then is the time when context parameters specified in the web.xml come into play.It comes with an advantage(along with its availability through out the web-app) that you just needs to do change in the web.xml file only,whenever you want to change that particular value.You specify the context-param like

<servlet>
        <servlet-name>ServletName</servlet-name>
        <servlet-class>com.mypackage.MyServlet</servlet-class>
    </servlet>

    <context-param>
            <param-name>email</param-name>
            <param-value>[email protected]</param-value>
    </context-param>

and can access it like

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{

        PrintWriter pw = response.getWriter();
        pw.println(getServletContext().getInitParameter("email"));

    }

Upvotes: 3

Kalpesh Soni
Kalpesh Soni

Reputation: 7287

like a key value pair

they can be used to read some value anywhere in the web app

See http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/

Upvotes: 1

Related Questions