Servlet reads the papers summary

Servlet file access to resources the way summary

A recent study JavaWeb, but also because of the way the resource file ServletContext get upset,
one of the most straightforward question is: Why are there so many ways? Too easily confused!

Indeed, after seeing the video tutorial, first felt in the Web project you want to get information and resource files normal Java project is not the same, because this is not Java Web engineering works, even if our files in the src However, the real relative path is not in src, the real absolute path is not working in the Java space. Since our Web project is entrusted to run in Tomcat, the path that runs under Tomcat project in the classes directory of reasons, so this is why I have summarized! The following focus:

  • ServletContext - common way to read the project file

    • First of all, if we are under the src directory, create a project in EclipseWeb properties configuration file, and write information to read, is the most primitive method of using the Properties is loaded FileInputStream (PS: This is our learning method commonly used JavaSE ) However, this method has the following limitations and Precautions:
    1. Examples of InputStream code is too long-winded , the file path is too long ( not a relative path )
    2. Method for the following codes:
      Properties properties = new Properties();
      InputStream is = new FileInputStream("D:/apache-tomcat-7.0.77/wtpwebapps/Servlet/WEB-INF/classes/peizhi.properties");
      properties.load(is);
      String name = properties.getProperty("name");	//这里以获取name为例
      System.out.println("name="+name);
      
  • ServletContext - get resource file (a)

    • This convenient method than the above method is still Properties loaded InputStream to achieve, also joined the ServletContext object to get the InputStream, so do not fill out a path like the above, this method has the following attention points:
    1. To use the ServletContext getRealPath ( "WEB-INF / classes / peizhi.properties");
    2. The method for the directory pointed to: project name directory
    3. So path: WEB-INF / classes / peizhi.properties
    4. Method for the following codes:
      Properties properties = new Properties();
      ServletContext sc = getServletContext();
      InputStream is = null;
      String path = sc.getRealPath("WEB-INF/classes/peizhi.properties");
      //System.out.println("path="+path);
      is = new FileInputStream(path);
      properties.load(is);
      String name = properties.getProperty("name");
      System.out.println("name="+name);
      
  • ServletContext - get resource files (two)

    • This convenient method than the first method (second and the like), note that this method has the following points:
    1. ServletContext to use the method of getResourceAsStream
    2. getResourceAsStream method points to a directory in the root directory of the Tomcat project, namely wtpwebapps \ project name
    3. So path: WEB-INF / classes / peizhi.properties
    4. Method for the following codes:
      ServletContext sc = getServletContext();
      Properties properties = new Properties();
      InputStream is = null;
      is = sc.getResourceAsStream("WEB-INF/classes/peizhi.properties");
      properties.load(is);
      String name = properties.getProperty("name");
      System.out.println("name="+name);
      
  • ServletContext - get the resource file using the ClassLoader

    • This method is similar to the above two methods, but also the use of properties and InputStream, just use ClassLoader to accomplish
    1. this.getClass().getResourceAsStream("…/name.properties");指向:classes\Servlet
    2. this.gerClass().getClassLoader().getResourceAsStream(“name
      .properties”);指向:classes
    3. Method for the following codes:
      Properties properties = new Properties();
      InputStream is = null;
      is = this.getClass().getClassLoader().getResourceAsStream("peizhi.properties");
      properties.load(is);
      String name = properties.getProperty("name");
      System.out.println("name="+name);
      

    Finally: I was a sophomore chicken dish, hoping for some like me have doubts students learn Web help. PS: the first to write blog, actually wrote nearly two hours!
    I'm so tired

Published 15 original articles · won praise 18 · views 4582

Guess you like

Origin blog.csdn.net/oZuoShen123/article/details/101098543