ServletContext read resource file

ServletContext read resource file

1, built .properties file because the file storage location, may lead not to export the problem, the solution is to modify the pom.xml file mentioned earlier

2, reading resource files

3, Registration + maps

1, in resources, build properties file (the norm)

1, built in resources (to be built here, and will not go wrong)

  

 

 2, generate location observed by db.properities: class default path is the path in class

2, in java, build properties file (not the norm)

1, not resources under construction, but built in java servlet in (built here, is not standardized and may not be exported to a problem, the solution in 2)

  

 2, and observed under db.properities aa.properities generates location: a generation, and not generating a

 

3, there is no reason for that is generated can not be exported, the solution is: in pom.xml, in the build configuration resources, the resources to prevent the export failures. (Note that since this project is the current, not on the main project of the pom.xml, should be placed in the current project pom.xml)

 1 <resources>
 2             <resource>
 3                 <directory>src/main/resources</directory>
 4                 <includes>
 5                     <include>**/*.properties</include>
 6                     <include>**/*.xml</include>
 7                 </includes>
 8             </resource>
 9             <resource>
10                 <directory>src/main/java</directory>
11                 <includes>
12                     <include>**/*.properties</include>
13                     <include>**/*.xml</include>
14                 </includes>
15                 <filtering>true</filtering>
16             </resource>
17         </resources>
View Code

4, and observed under db.properities aa.properities generating positions: both generated in the classpath

3, reading resource files

 

 1 package com.wang.servlet;
 2 
 3 import javax.servlet.RequestDispatcher;
 4 import javax.servlet.ServletContext;
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.util.Properties;
12 
13 public classServletDemo05 the extends the HttpServlet {
 14      @Override
 15      protected  void the doGet (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException {
 16  
. 17          the InputStream IS = the this .getServletContext () the getResourceAsStream ( "/ the WEB-INF / classes / the db.properties." );
 18          // first / current representative of the web application, to find and step down until you find the location profile
 19          // returns a stream 
20 is          the Properties prop = new new the Properties ();
 21 is          prop.load (iS);
 22          String = prop.getProperty the User ( "username");
23         String pwd = prop.getProperty("password");
24         resp.getWriter().print(user+":"+pwd);
25 
26     }
27 
28     @Override
29     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
30         doGet(req, resp);
31     }
32 }
View Code

4, Registration + maps

1     <!--注册和映射-->
2     <servlet>
3         <servlet-name>sd5</servlet-name>
4         <servlet-class>com.wang.servlet.ServletDemo05</servlet-class>
5     </servlet>
6     <servlet-mapping>
7         <servlet-name>sd5</servlet-name>
8         <url-pattern>/sd5</url-pattern>
9     </servlet-mapping>
View Code

5, run

6, just read is in resources, construction of properties file; now read the next java, built properties file

 

 

Guess you like

Origin www.cnblogs.com/WZ-BeiHang/p/12563495.html