{"id":376,"date":"2003-05-06T00:01:38","date_gmt":"2003-05-06T04:01:38","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=376"},"modified":"2003-05-06T00:01:38","modified_gmt":"2003-05-06T04:01:38","slug":"application-settings-when-using-servlets","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/05\/06\/application-settings-when-using-servlets\/","title":{"rendered":"Application settings when using servlets"},"content":{"rendered":"<p>I posted <a href=\"http:\/\/cephas.net\/blog\/archives\/000340.html#000340\">some notes<\/a> a couple weeks ago about .NET configuration files, came across the code for doing the same thing in a servlet container tonight.  Basically, you get a ServletConfig object using the getServletConfig() method of the Servlet interface and then call getInitParameter(String initParamName) on that.  For example, let&#8217;s say that I put a global database connection string in my web.xml deployment descriptor:<\/p>\n<p>&lt;web-app&gt;<br \/>\n&nbsp;&nbsp;&lt;servlet&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;servlet-name&gt;myservlet&lt;\/servlet-name&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;servlet-class&gt;MyServlet&lt;\/servlet-class&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;init-param&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;param-name&gt;connection_string&lt;\/param-name&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;param-value&gt;user id=sa;password=mypassword;initial catalog=mydb;data source=mydbserver;Connect Timeout=30&lt;\/param-value&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/init-param&gt;<br \/>\n&nbsp;&nbsp;&lt;\/servlet&gt;<br \/>\n&lt;\/web-app&gt;<\/p>\n<p>Then, in my servlet code init() method, I could write this:<\/p>\n<p>ServletConfig sc = getServletConfig();<br \/>\nString connection_string = sc.getInitParameter(&#8220;connection_string&#8221;);<\/p>\n<p>After retrieving the value from the ServletConfig object, you&#8217;ll probably want to store it for later use in the ServletContext object:<\/p>\n<p>ServletContext c = getServletContext();<br \/>\nc.setAttribute(&#8220;connection_string&#8221;, connection_string);<\/p>\n<p>Now I can get the connection string in a JSP or a servlet later using this short snippet:<\/p>\n<p>String myCS = (String)getServletContext().getAttribute(&#8220;connection_string&#8221;);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I posted some notes a couple weeks ago about .NET configuration files, came across the code for doing the same thing in a servlet container tonight. Basically, you get a ServletConfig object using the getServletConfig() method of the Servlet interface and then call getInitParameter(String initParamName) on that. For example, let&#8217;s say that I put a &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/05\/06\/application-settings-when-using-servlets\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Application settings when using servlets<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/376"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}