An item of ServletContext is created with the aid of using the web container at time of deploying the project. This item may be used to get configuration statistics from the net.xml report. There is the simplest one ServletContext item consistent with Web application.
If any statistics is shared to many servlets, it's far higher to offer it from the net.xml to report the use of the element.
It is easy to carry on if any information is shared to all servlets. It's miles higher to make it available for all the servlets. We offer these records from the web.xml file, so if the records are changed, we do not want to adjust the servlet. Thus it gets rid of the maintenance problem.
Servlet context objects have a lot of uses. Some of them are given below:
Some Commonly used methods of servletContext interface are:
public ServletContext getServletContext()
//We can get the ServletContext object from ServletConfig object ServletContext application=getServletConfig().getServletContext(); //Another convenient way to get the ServletContext object ServletContext application=getServletContext();
The context-param detail, subelement of web-app, is used to outline the initialization parameter withinside the utility scope. The param-call and param-fee are the sub-factors of the context-param. The param-call detail defines parameter call and and param-fee defines its usefulness.
<web-app> ...... <context-param> <param-name>parametername</param-name> <param-value>parametervalue</param-value> </context-param> ...... </web-app>
In this example, we are getting the initialization parameter from the web.xml record and printing the price of the initialization parameter. Notice that the item of ServletContext represents the utility scope. So if we extrade the price of the parameter from the web.xml record, all of the servlet training gets the modified price. So we do not want to alter the servlet. So it's far higher to have the not unusual place records for maximum of the servlets withinside the web.xml record via way of means of context-param element. Let's see the easy example:
DemoServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); //creating ServletContext object ServletContext context=getServletContext(); //Getting the value of the initialization parameter and printing it String driverName=context.getInitParameter("dname"); pw.println("driver name is="+driverName); pw.close(); }}
Web.xml
<web-app> <servlet> <servlet-name>sonoojaiswal</servlet-name> <servlet-class>DemoServlet</servlet-class> </servlet> <context-param> <param-name>dname</param-name> <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value> </context-param> <servlet-mapping> <servlet-name>sonoojaiswal</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping> </web-app>
In this example, we are getting all of the initialization parameters from the web.xml file. For getting all of the parameters, we've got used the getInitParameterNames() approach withinside the servlet class.
DemoServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); ServletContext context=getServletContext(); Enumeration<String> e=context.getInitParameterNames(); String str=""; while(e.hasMoreElements()){ str=e.nextElement(); out.print("
"+context.getInitParameter(str)); } }}
Web.xml
<web-app> <servlet> <servlet-name>sonoojaiswal</servlet-name> <servlet-class>DemoServlet</servlet-class> </servlet> <context-param> <param-name>dname</param-name> <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value> </context-param> <context-param> <param-name>username</param-name> <param-value>system</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>oracle</param-value> </context-param> <servlet-mapping> <servlet-name>sonoojaiswal</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping> </web-app>