{"id":1665,"date":"2016-09-20T07:26:54","date_gmt":"2016-09-20T07:26:54","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1665"},"modified":"2016-09-20T07:26:54","modified_gmt":"2016-09-20T07:26:54","slug":"servlet-config-and-servlet-context","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/servlet-config-and-servlet-context\/","title":{"rendered":"Servlet Config and Servlet Context"},"content":{"rendered":"<p><strong>ServletConfig<\/strong><\/p>\n<ol>\n<li>ServletConfig is one of the pre-defined interface.<\/li>\n<li>ServletConfig object is used for developing flexible servlets.<\/li>\n<li>ServletConfig objct exist one per servlet program.<\/li>\n<li>An object of ServletConfig created by the container during its initialization phase.<\/li>\n<li>An object of ServletConfig is available to the servlet during its execution, once the servlet execution is completed, automatically ServletConfig interface object will be removed by the container.<\/li>\n<li>An object of ServletConfig interface contains <init-param> details at web.xml, of a particular servlet.<\/li>\n<li>The moment when we are using an object of ServletConfig, we need to configure the web.xml by writing <init-param> tag under <servlet> tag of web.xml.<\/li>\n<li>When ever compiler executes init() mehod then the ServletConfig will be created in general.<\/li>\n<li>An object of ServletConfig contain the <init-param> data in the form of key,value pairs, here the keys represents init param names and values are its values, which are represented in the web.xml file<\/li>\n<\/ol>\n<p><strong>How to Get ServletConfig Object into Servelt<\/strong><br \/>\n<em>Method1<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nServletConfig conf = getServletConfig();\r\n<\/pre>\n<p><em>Method2<\/em><br \/>\nServletConfig object will be available in init() method of the servlet.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void init(ServletConfig config)\r\n{\r\n\/\/ \u2026\u2026\u2026\u2026\u2026\u2026\u2026\r\n}\r\n<\/pre>\n<p><strong>How to Retrieve Data from ServletConfig Interface Object<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic String getInitParameter(\u201cparam name\u201d);\r\npublic Enumeration getInitParameterNames();\r\n<\/pre>\n<p><strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;web-app&gt; \r\n  &lt;servlet&gt;\r\n     &lt;servlet-name&gt;onServletConfig&lt;\/servlet-name&gt;\r\n     &lt;servlet-class&gt;java4s.OnServletConfig&lt;\/servlet-class&gt; \r\n     &lt;init-param&gt;\r\n        &lt;param-name&gt; n1 &lt;\/param-name&gt;\r\n        &lt;param-value&gt; 100 &lt;\/param-value&gt;\r\n     &lt;\/init-param&gt;\r\n    .\r\n    .\r\n&lt;\/web-app&gt;\r\n<\/pre>\n<p><strong>Test.java<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nServletConfig conf=getServletConfig(); \r\nString s1=conf.getInitParameter(&quot;n1&quot;);\r\n <\/pre>\n<p><strong>ServletContext<\/strong><br \/>\nAn object of ServletContext is created by the web container at time of deploying the project. This object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application. <\/p>\n<p><strong>Methods of ServletContext interface<\/strong><\/p>\n<ol>\n<li>public String getInitParameter(String name):Returns the parameter value for the specified parameter name.<\/li>\n<li>public Enumeration getInitParameterNames():Returns the names of the context&#8217;s initialization parameters.<\/li>\n<li>public void setAttribute(String name,Object object):sets the given object in the application scope.<\/li>\n<li>public Object getAttribute(String name):Returns the attribute for the specified name.<\/li>\n<li>public Enumeration getInitParameterNames():Returns the names of the context&#8217;s initialization parameters as an Enumeration of String objects.<\/li>\n<\/ol>\n<p><strong>web.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;web-app&gt;  \r\n     ......  \r\n          \r\n      &lt;context-param&gt;  \r\n        &lt;param-name&gt;parametername&lt;\/param-name&gt;  \r\n        &lt;param-value&gt;parametervalue&lt;\/param-value&gt;  \r\n      &lt;\/context-param&gt;  \r\n     ......  \r\n    &lt;\/web-app&gt;  \r\n<\/pre>\n<p><strong>Test.java<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  ServletContext context=getServletContext();    \r\n\r\n  \/\/Getting the value of the initialization parameter and printing it  \r\n  String driverName=context.getInitParameter(&quot;parametername&quot;);  \r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ServletConfig ServletConfig is one of the pre-defined interface. ServletConfig object is used for developing flexible servlets. ServletConfig objct exist one per servlet program. An object of ServletConfig created by the container during its initialization phase. An object of ServletConfig is available to the servlet during its execution, once the servlet execution is completed, automatically ServletConfig&hellip; <a href=\"https:\/\/codethataint.com\/blog\/servlet-config-and-servlet-context\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1665","post","type-post","status-publish","format-standard","hentry","category-servlets"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1665"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1665\/revisions"}],"predecessor-version":[{"id":1666,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1665\/revisions\/1666"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}