Before going ServletContext Example, see the difference between <init-param> and <context-param>, both written in web.xml file.
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.
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>.
Following ServletContext Example illustrates
The aim of this program is the servlet reads the weight of gold article from the client HTML file and the gold rate from <context-param> and estimates the bill amount payable.
Client Program: ClientData.html
web.xml entry for the Servlet
Welcome to Tomcat
Welcome to Tomcat
goldrate
3100.5
klm
ContextReading
klm
/conread
Servlet Program: ContextReading.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ContextReading extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// reading gold weight from client file, ClientData.html
String gold = req.getParameter("t1");
int gw = Integer.parseInt(gold);
// reading gold rate from of web.xml file
ServletContext sc1 = getServletContext();
String rate = sc1.getInitParameter("goldrate");
double gr = Double.parseDouble(rate);
// estimate and make bill
double total = gw * gr;
out.println("Your Gold Purchase Information:
");
out.println("Gold Weight: " + gw + " gms.
");
out.println("Gold Rate Rs." + gr + "/gm.
");
out.println("To pay Rs." + total);
out.close();
}
}
Following is the client Screen: ClientData.html
Output screen when submit is clicked after entering gold weight.

Screenshot of ServletContext Example
How to update data servlet context
Hello Mr Rao,
What you think the Gold rate should be kept as a context parameter? What happened if the gold rate got change? Do we need to restart the application to reflect the new rate?
The changed context parameter can be accessed by other servlets even without stopping the server. Are u not getting?
Hi sir,
I read ServeletContext interface explanation, there it is mentioned we create ServletContext object through ServletConfig object. i.e.,
ServletConfig config1 = getServletConfig();
ServletContext context1 = config1.getServletContext();
But in the above example the object for ServletContext is created as below
ServletContext sc1 = getServletContext();
without ServletConfig object creation.
though i have not executed the above code. Can you please confirm whether ServletContext object is created through ServletConfig object or directly as the code mentioned in the above example.
There is a method in ServletConfig interface getServletContext() which can be used straight away without creating ServletConfig object.
Hello sir hope you doing good , the website is too good we can understand easily each and every concept you written is super .. thank you