init-param vs context-param Servlets


Before going into init-param vs context-param, let us know the meaning and purpose of init-param and context-param in Servlet coding.
1. What is <init-param>?
 
  portnumber 
  8888 

We have seen the above code in Servlet web xml init param Example using ServletConfig where the "portnumber" and "8888" are accessible for the particular servlet "ReadInitParamValues". Other servlets in the container cannot access this data. That is, the <init-param> is accessible to one servlet only or it can be said the <init-param> data is private for a particular servlet.

2. What is <context-param>?

Now is there anyway, the data of <init-param> is accessible to all the servlets under execution in the container? Yes, there exists; we write the <init-param> outside of any <servlet> tag in the same web.xml file. For this, <context-param> (instead of <init-param>) is used as follows.


  goldrate
  3100.5

Because the tag <context-param> exists, in the web.xml, outside any servlet tag (say, global area), every servlet can access it.

Finally, <init-param> data is local for a particular servlet and <context-param> data is global for all servlets. This is the difference between <init-param> and <context-param>.

init-param vs context-param Example codes:

The <init-param> is discussed in Servlet init param Example and
<context-param> is discussed with with example in Reading Servlets Context Parameter with Example.

Now let us summarize the similarities and differences of init-param vs context-param.
ServletConfig (of init-param) ServletContext (of context-param)
This is an interface from javax.servlet package. This is also an interface from javax.servlet package.
Written in web.xml file. Also written in web.xml file.
Method to read is getInitParameter() of ServletConfig. The same method, getInitParameter(), exists in ServletContext also to read.
Code maintenance is easier as and when <param-value> is changed, the servlet need not be compiled again. Ofcourse, with <context-param> also.
For each <init-param>, there will be one <param-name> and one </param-value>. The same thing here also. For each <context-param>, there will be one <param-name> and one </param-value>.
One <servlet> can contain any number of <init-param> tags. One web.xnl file can have any number of <context-param> tags also.
Used for providing initialization data for a particular servlet. Used for providing common information for all the servlets under execution.
Written inside <servlet> tag. Written inside <context-param> tag.
It is local data for a specific servelt. It is global data sharable by all servlets. This data can be used to communicate with servlet container.
Important methods are getServletName(), getServletContext(), getInitParameter() and getInitParameterNames(). Important methods are getInitParameter(), getInitParameterNames(), getRequestDispatcher(), setAttribute() and getAttribute().
A separate ServletConfig object is available for each Servlet Only one ServletContext object is available for the whole application.

Realtime examples:

  1. ServletConfig: Suppose I would like to know the amount payable for the number of mangoes purchased. I read the number of mangoes from the client html file. Then, which is the better place to have the cost of mango placed? Moreover, the cost of mango changes everyday. May be database table. If it is placed in a database table, for reading it is required a JDBC program with an extra overhead of database connection. The best place is web.xml file which does not require compilation every time the mango cost changes. Here, the servlet can be initialized with mango cost by the time client sends the number of mangoes. But remember, this mango cost is readable by one servlet only because the mango cost is placed within the <servlet> tag.
  2. ServletContext: I take the same above scenario with different perspective. Imagine there are multiple fruit vendors (each fruit shop is represented by one servlet) and all require the cost of one mango. That is, the mango cost should be global. The place to put the cost of mango is in which exists outside any <servlet> tag. As it is outside any <servlet> tag, every servlet under execution can access.

4 thoughts on “init-param vs context-param Servlets”

  1. sir, i just want to know a real time example of ServletConfig and ServletContext which will helpul to develop a real time example in corporate..please not same example as above Mangoes.
    thanks and regards
    Mohamed Sadam.

  2. sandeep kumar rai

    Sir,can you please give me a good example of ServletConfig and ServletContext .
    i know that one is locally access and another one is globally access.
    ” i mean to say a real world scenario were these two things are put into implementation. ” :-D ..

    1. 1. ServletConfig: Suppose I would like to know the amount payable for the number of mangoes purchased. I read the number of mangoes from the client html file. Then, which is the better place to have the cost of mango placed? Moreover, the cost of mango changes everyday. May be database table. If it is placed in a database table, for reading it is required a JDBC program with an extra overhead of database connection. The best place is web.xml file which does not require compilation every time the mango cost changes. Here, the servlet can be initialized with mango cost by the time client sends the number of mangoes. But remember, this mango cost is readable by one servlet only because the mango cost is placed within the tag.

      ServletContext: I take the same above scenario with different perspective. Imagine there are multiple fruit vendors (each fruit shop is represented by one servlet) and all require the cost of one mango. That is, the mango cost should be global. The place to put the cost of mango is in which exists outside any tag. As it is outside any tag, every servlet under execution can access.

Leave a Comment

Your email address will not be published.