Javaweb Project Gets the configuration file under the WebContent web-inf __web

Source: Internet
Author: User
Tags serialization
1 Get the configuration file under the WebContent path
<span style= "FONT-SIZE:18PX;" >string Path = ParseReader.class.getResource ("/"). GetPath ();
String Websiteurl = Path.replace ("/build/classes", ""). Replace ("%20", "")
					+ "webcontent/web-inf/";
System.out.println (Websiteurl);</span>

The result printed is/d:/workspace/com.phy.smart.pipe.monitor/webcontent/web-inf/

/d:/workspace/com.phy.smart.pipe.monitor is my current project local address

The above idea is based on the load of Parsereader class to obtain the address of the current project, where/build/classes is a Web project compiled after Tomcat deployment. class file path, to be replaced by empty,%20 as a space,%20 is actually a space, After serialization, it becomes%20.%20 is actually a space, and after serialization it becomes%20.

Then you can arbitrarily get the file path under your webcontent. 2. Get the configuration file under the SRC path

public class Propertiesreader {public
	static String USERNAME = "Nancht";
	public static String PASSWD = "vprjg8#h==";
	private static properties props = new properties ();

	static {
		try {
			InputStream in = new Classpathresource ("/ws.properties")
					. getInputStream ();
			Props.load (in);

		} catch (FileNotFoundException e) {
			e.printstacktrace ();
		} catch (IOException e) {
			e.printstacktrace ();
		}
	}
}

3. The following summary of three methods reads:

Use ServletContext to read resource files in different locations of the Web project public class ServletDemo11 extends HttpServlet {public void doget (httpservletrequest
	Request, HttpServletResponse response) throws Servletexception, IOException {test5 (); //Read the configuration file under src private void Test5 () throws IOException {InputStream in = This.getservletcontext (). getresourceasst
		Ream ("/web-inf/classes/db.properties");  Properties prop = new properties ();
		
		
		Map Prop.load (in);
		String url = prop.getproperty ("url");
		String username = prop.getproperty ("username");
		
		String Password = prop.getproperty ("password");
		System.out.println (URL);
		SYSTEM.OUT.PRINTLN (username);
	SYSTEM.OUT.PRINTLN (password); //Read the configuration file below src (traditional way undesirable) private void test4 () throws IOException {FileInputStream in = new FileInputStream ("Classe
		S/db.properties ");
	System.out.println (in);
		///Read configuration file The Third way private void Test3 () throws IOException {ServletContext context = This.getservletcontext (); URL url = context.getresource ("/Resource/db.properties ");
		
	InputStream in = Url.openstream ();
		//The second way to read the configuration file private void Test2 () throws IOException {ServletContext context = This.getservletcontext ();  String Realpath = Context.getrealpath ("/db.properties"); C:\\sdsfd\sdf\db.properties//Get to Operation filename realpath=abc.properties String filename = realpath.substring (realpath.la
		Stindexof ("\") +1);
		
		
		System.out.println ("The file currently read is:" + filename);
		FileInputStream in = new FileInputStream (Realpath);
		Properties prop = new properties ();
		
		Prop.load (in);
		String url = prop.getproperty ("url");
		String username = prop.getproperty ("username");
		
		String Password = prop.getproperty ("password");
		SYSTEM.OUT.PRINTLN ("The document contains the following data:");
		System.out.println (URL);
		SYSTEM.OUT.PRINTLN (username);
		
	SYSTEM.OUT.PRINTLN (password);
		///Read configuration file The first way private void Test1 () throws IOException {ServletContext context = This.getservletcontext (); InputStream in = Context.getresourceasstream ("/db.propErties ");  Properties prop = new properties ();
		
		
		Map Prop.load (in);
		String url = prop.getproperty ("url");
		String username = prop.getproperty ("username");
		
		String Password = prop.getproperty ("password");
		System.out.println (URL);
		SYSTEM.OUT.PRINTLN (username);
	SYSTEM.OUT.PRINTLN (password); 
		public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
	Doget (request, response);
 }

}




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.