ServletConfig | ServletContext | Servlets Tutorial For Beginner |Advance Java Tutorial In Hindi -7
ServletConfig Vs ServletContext:- Both are the interfaces Whose Implementation is providing by Various venders accordingly. The Object of Servlet Context is only once for whole web application created by web container at the time of deployment of web application. The Object of ServletConfig is separate for each servlet created by web container at the time of creating the object of servlet. Note:-We can say that one servlet config per servlet and one servlet context per web application. Why ServletContext:-? Usage of ServletContext Interface There can be a lot of usage of ServletContext object. Some of them are as follows: 1. The object of ServletContext provides an interface between the container and servlet. 2. The ServletContext object can be used to get configuration information from the web.xml file. 3. The ServletContext object can be used to set, get or remove attribute from the web.xml file. 4.The ServletContext object can be used to provide inter-application communication Commonly used methods of ServletContext interface There is given some commonly used methods of ServletContext interface. 1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name. 2. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters. 3. public void setAttribute(String name,Object object):sets the given object in the application scope. 4. public Object getAttribute(String name):Returns the attribute for the specified name. 5. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects. 6. public void removeAttribute(String name):Removes the attribute with the given name from the servlet context. ________________________________________ How to get the object of ServletContext interface 1. getServletContext() method of ServletConfig interface returns the object of ServletContext. 2. getServletContext() method of GenericServlet class returns the object of ServletContext. Syntax of getServletContext() method public ServletContext getServletContext() Example of getServletContext() method //We can get the ServletContext object from ServletConfig object ServletContext application=getServletConfig().getServletContext(); //Another convenient way to get the ServletContext object ServletContext application=getServletContext(); ________________________________________ Syntax to provide the initialization parameter in Context scope The context-param element, subelement of web-app, is used to define the initialization parameter in the application scope. The param-name and param-value are the sub-elements of the context-param. The param-name element defines parameter name and and param-value defines its value. This object can be used to get configuration information from web.xml file. If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time. Advantage of ServletConfig The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web.xml file. Methods of ServletConfig interface 1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name. 2. public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names. 3. public String getServletName():Returns the name of the servlet. 4. public ServletContext getServletContext():Returns an object of ServletContext. ________________________________________ How to get the object of ServletConfig 1. getServletConfig() method of Servlet interface returns the object of ServletConfig. Syntax of getServletConfig() method public ServletConfig getServletConfig(); Example of getServletConfig() method ServletConfig config=getServletConfig(); //Now we can call the methods of ServletConfig interface ________________________________________ Syntax to provide the initialization parameter for a servlet The init-param sub-element of servlet is used to specify the initialization parameter for a servlet. Stay tuned with me for more upcoming Videos, don't forget to subscribe my YouTube Channel and share your valuable comments in Comment section down below, i'll be happy t o help you all, your Surya ☺ Connect on Socials:- Facebook:- https://www.facebook.com/AndroJavaTec... Twitter:- https://twitter.com/AndroJavaTech4U Google Plus:- https://plus.google.com/u/0/+AndroJav... LinkedIn:- https://www.linkedin.com/in/androjava...
ServletConfig Vs ServletContext:- Both are the interfaces Whose Implementation is providing by Various venders accordingly. The Object of Servlet Context is only once for whole web application created by web container at the time of deployment of web application. The Object of ServletConfig is separate for each servlet created by web container at the time of creating the object of servlet. Note:-We can say that one servlet config per servlet and one servlet context per web application. Why ServletContext:-? Usage of ServletContext Interface There can be a lot of usage of ServletContext object. Some of them are as follows: 1. The object of ServletContext provides an interface between the container and servlet. 2. The ServletContext object can be used to get configuration information from the web.xml file. 3. The ServletContext object can be used to set, get or remove attribute from the web.xml file. 4.The ServletContext object can be used to provide inter-application communication Commonly used methods of ServletContext interface There is given some commonly used methods of ServletContext interface. 1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name. 2. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters. 3. public void setAttribute(String name,Object object):sets the given object in the application scope. 4. public Object getAttribute(String name):Returns the attribute for the specified name. 5. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects. 6. public void removeAttribute(String name):Removes the attribute with the given name from the servlet context. ________________________________________ How to get the object of ServletContext interface 1. getServletContext() method of ServletConfig interface returns the object of ServletContext. 2. getServletContext() method of GenericServlet class returns the object of ServletContext. Syntax of getServletContext() method public ServletContext getServletContext() Example of getServletContext() method //We can get the ServletContext object from ServletConfig object ServletContext application=getServletConfig().getServletContext(); //Another convenient way to get the ServletContext object ServletContext application=getServletContext(); ________________________________________ Syntax to provide the initialization parameter in Context scope The context-param element, subelement of web-app, is used to define the initialization parameter in the application scope. The param-name and param-value are the sub-elements of the context-param. The param-name element defines parameter name and and param-value defines its value. This object can be used to get configuration information from web.xml file. If the configuration information is modified from the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time. Advantage of ServletConfig The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web.xml file. Methods of ServletConfig interface 1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name. 2. public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names. 3. public String getServletName():Returns the name of the servlet. 4. public ServletContext getServletContext():Returns an object of ServletContext. ________________________________________ How to get the object of ServletConfig 1. getServletConfig() method of Servlet interface returns the object of ServletConfig. Syntax of getServletConfig() method public ServletConfig getServletConfig(); Example of getServletConfig() method ServletConfig config=getServletConfig(); //Now we can call the methods of ServletConfig interface ________________________________________ Syntax to provide the initialization parameter for a servlet The init-param sub-element of servlet is used to specify the initialization parameter for a servlet. Stay tuned with me for more upcoming Videos, don't forget to subscribe my YouTube Channel and share your valuable comments in Comment section down below, i'll be happy t o help you all, your Surya ☺ Connect on Socials:- Facebook:- https://www.facebook.com/AndroJavaTec... Twitter:- https://twitter.com/AndroJavaTech4U Google Plus:- https://plus.google.com/u/0/+AndroJav... LinkedIn:- https://www.linkedin.com/in/androjava...