Read the. properties file with ServletContext

Source: Internet
Author: User

Here is the main introduction of how ServletContext from the. properties file with the key to get the value.

ServletContext read the. Properties file is generally placed in the location of: 1 directly under the Webroot. 2. Place under a folder under Webroot. 3. Place under a folder under Web-inf.

Pay special attention not to put the. properties file directly under Web-inf. (as the result of this is null)

The rectangle of the red border in the following diagram is a few of these positions:

The contents of the A.properties, B.properties, c.properties, d.properties files are as follows:

sixi=townrapeflower=ouyangfeng

The code for the A.properties, B.properties, c.properties, d.properties files is as follows:

1  PackageCom.qls.counter;2 /**3 * Use ServletContext to read configuration a.properties, b.properties,4 * c.properties, D.properties5 * and prove not to write the file directly under the Web-inf, can be written under the Web-inf under a folder below. 6  */7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 ImportJava.io.PrintWriter;Ten Importjava.util.Properties; One  A Importjavax.servlet.ServletException; - ImportJavax.servlet.http.HttpServlet; - Importjavax.servlet.http.HttpServletRequest; the ImportJavax.servlet.http.HttpServletResponse; -  -  Public classTestextendsHttpServlet { -  +      -      Public voiddoget (httpservletrequest request, httpservletresponse response) +             throwsservletexception, IOException { A  atResponse.setcontenttype ("Text/html;charset=utf-8"); -PrintWriter out =Response.getwriter (); -         //get input stream: -InputStream in = This. Getservletconfig (). Getservletcontext (). getResourceAsStream ("/a.properties"); -InputStream in2 = This. Getservletcontext (). getResourceAsStream ("/web-inf/b.properties"); -InputStream in3 = This. Getservletcontext (). getResourceAsStream ("/chenzhen/d.properties"); inInputStream in4 = This. Getservletcontext (). getResourceAsStream ("/web-inf/ouyangfeng/c.properties"); -         //Create Properties toProperties prop=NewProperties (); +         //Load Input stream - prop.load (in);//in2 in3 in4 in turn to verify.  theOut.println ("sixi=" +prop.getproperty ("Sixi")); *Out.println ("<br/>"); $Out.println ("rapeflower=" +prop.getproperty ("Rapeflower"));Panax Notoginseng     } -  the      +      Public voidDoPost (httpservletrequest request, httpservletresponse response) A             throwsservletexception, IOException { the  +Response.setcontenttype ("Text/html;charset=utf-8"); -PrintWriter out =Response.getwriter (); $          This. Doget (request, response); $     } -  -}/*Output: the This is the output result of in in3 in4 - Sixi=townWuyi Rapeflower=ouyangfeng the the output of the in2 is: - Sixi=null Wu Rapeflower=null - **///:~

From the above output can prove that this article at the beginning of the content, do not put the file directly under the Web-inf.

As for how to read a configuration file from ServletContext, it can be summarized as four words: get input stream, create attribute, load input stream, get attribute value.

Read the. properties file with ServletContext

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.