2021-03-08 19:59发布
方式一:采用ServletContext读取
获取配置文件的realpath,然后通过文件流读取出来或者通过方法getReasurceAsStream()。
因为是用ServletContext读取文件路径,所以配置文件可以放入在WEB-INF的classes目录中,也可以在应用层级及WEB-INF的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在WEB-INF及Web-Root下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
1.首先创建一个动态的javaweb项目,项目目录如下:
2.创建一个servlet(FileReader.java)
package
com.xia.fileReader;
import
java.io.FileInputStream;
java.io.IOException;
java.io.InputStream;
java.io.InputStreamReader;
java.text.MessageFormat;
java.util.Properties;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
public
class
FileReader
extends
HttpServlet {
private
static
final
long
serialVersionUID = 1L;
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
/**
* response.setContentType("text/html;charset=UTF-8");目的是控制浏览器用UTF-8进行解码;
* 这样就不会出现中文乱码了
*/
response.setHeader(
"content-type"
,
"text/html;charset=UTF-8"
);
readSrcDirPropCfgFile(response);
//读取src目录下的db1.properties配置文件
response.getWriter().println(
""
readWebRootDirPropCfgFile(response);
//读取WebRoot目录下的db2.properties配置文件
readSrcSourcePackPropCfgFile(response);
//读取src目录下的config目录中的db3.properties配置文件
readWEBINFPropCfgFile(response);
//读取WEB-INF目录下的JDBC目录中的db4.properties配置文件
}
readSrcDirPropCfgFile(HttpServletResponse response)
IOException {
String path =
"/WEB-INF/classes/db1.properties"
;
InputStream in =
this
.getServletContext().getResourceAsStream(path);
Properties props =
new
Properties();
props.load(in);
String driver = props.getProperty(
"jdbc.driver"
String url = props.getProperty(
"jdbc.url"
String username = props.getProperty(
"jdbc.username"
String password = props.getProperty(
"jdbc.password"
"读取src目录下的db1.properties配置文件"
response.getWriter().println(MessageFormat.format(
"driver={0},url={1},username={2},password={3}"
driver,url, username, password));
readWebRootDirPropCfgFile(HttpServletResponse response)
IOException{
"/db2.properties"
"读取WebRoot目录下的db2.properties配置文件"
readSrcSourcePackPropCfgFile(HttpServletResponse response)
"/WEB-INF/classes/config/db3.properties"
String realPath =
.getServletContext().getRealPath(path);
InputStreamReader reader =
InputStreamReader(
FileInputStream(realPath),
"UTF-8"
props.load(reader);
"读取src目录下的config目录中的db3.properties配置文件"
readWEBINFPropCfgFile(HttpServletResponse response)
"/WEB-INF/JDBC/db4.properties"
System.out.println(
"realPath:"
+realPath);
"contextPath:"
+
.getServletContext().getContextPath());
"读取WEB-INF目录下的JDBC目录中的db4.properties配置文件"
doPost(HttpServletRequest request, HttpServletResponse response)
最多设置5个标签!
方式一:采用ServletContext读取
获取配置文件的realpath,然后通过文件流读取出来或者通过方法getReasurceAsStream()。
因为是用ServletContext读取文件路径,所以配置文件可以放入在WEB-INF的classes目录中,也可以在应用层级及WEB-INF的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在WEB-INF及Web-Root下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
1.首先创建一个动态的javaweb项目,项目目录如下:
2.创建一个servlet(FileReader.java)
package
com.xia.fileReader;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
java.text.MessageFormat;
import
java.util.Properties;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
FileReader
extends
HttpServlet {
private
static
final
long
serialVersionUID = 1L;
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
/**
* response.setContentType("text/html;charset=UTF-8");目的是控制浏览器用UTF-8进行解码;
* 这样就不会出现中文乱码了
*/
response.setHeader(
"content-type"
,
"text/html;charset=UTF-8"
);
readSrcDirPropCfgFile(response);
//读取src目录下的db1.properties配置文件
response.getWriter().println(
"
"
);
readWebRootDirPropCfgFile(response);
//读取WebRoot目录下的db2.properties配置文件
response.getWriter().println(
"
"
);
readSrcSourcePackPropCfgFile(response);
//读取src目录下的config目录中的db3.properties配置文件
response.getWriter().println(
"
"
);
readWEBINFPropCfgFile(response);
//读取WEB-INF目录下的JDBC目录中的db4.properties配置文件
}
public
void
readSrcDirPropCfgFile(HttpServletResponse response)
throws
IOException {
String path =
"/WEB-INF/classes/db1.properties"
;
InputStream in =
this
.getServletContext().getResourceAsStream(path);
Properties props =
new
Properties();
props.load(in);
String driver = props.getProperty(
"jdbc.driver"
);
String url = props.getProperty(
"jdbc.url"
);
String username = props.getProperty(
"jdbc.username"
);
String password = props.getProperty(
"jdbc.password"
);
response.getWriter().println(
"读取src目录下的db1.properties配置文件"
);
response.getWriter().println(MessageFormat.format(
"driver={0},url={1},username={2},password={3}"
,
driver,url, username, password));
}
public
void
readWebRootDirPropCfgFile(HttpServletResponse response)
throws
IOException{
String path =
"/db2.properties"
;
InputStream in =
this
.getServletContext().getResourceAsStream(path);
Properties props =
new
Properties();
props.load(in);
String driver = props.getProperty(
"jdbc.driver"
);
String url = props.getProperty(
"jdbc.url"
);
String username = props.getProperty(
"jdbc.username"
);
String password = props.getProperty(
"jdbc.password"
);
response.getWriter().println(
"读取WebRoot目录下的db2.properties配置文件"
);
response.getWriter().println(MessageFormat.format(
"driver={0},url={1},username={2},password={3}"
,
driver,url, username, password));
}
public
void
readSrcSourcePackPropCfgFile(HttpServletResponse response)
throws
IOException {
String path =
"/WEB-INF/classes/config/db3.properties"
;
String realPath =
this
.getServletContext().getRealPath(path);
InputStreamReader reader =
new
InputStreamReader(
new
FileInputStream(realPath),
"UTF-8"
);
Properties props =
new
Properties();
props.load(reader);
String driver = props.getProperty(
"jdbc.driver"
);
String url = props.getProperty(
"jdbc.url"
);
String username = props.getProperty(
"jdbc.username"
);
String password = props.getProperty(
"jdbc.password"
);
response.getWriter().println(
"读取src目录下的config目录中的db3.properties配置文件"
);
response.getWriter().println(MessageFormat.format(
"driver={0},url={1},username={2},password={3}"
,
driver,url, username, password));
}
public
void
readWEBINFPropCfgFile(HttpServletResponse response)
throws
IOException {
String path =
"/WEB-INF/JDBC/db4.properties"
;
String realPath =
this
.getServletContext().getRealPath(path);
System.out.println(
"realPath:"
+realPath);
System.out.println(
"contextPath:"
+
this
.getServletContext().getContextPath());
InputStreamReader reader =
new
InputStreamReader(
new
FileInputStream(realPath),
"UTF-8"
);
Properties props =
new
Properties();
props.load(reader);
String driver = props.getProperty(
"jdbc.driver"
);
String url = props.getProperty(
"jdbc.url"
);
String username = props.getProperty(
"jdbc.username"
);
String password = props.getProperty(
"jdbc.password"
);
response.getWriter().println(
"读取WEB-INF目录下的JDBC目录中的db4.properties配置文件"
);
response.getWriter().println(MessageFormat.format(
"driver={0},url={1},username={2},password={3}"
,
driver,url, username, password));
}
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
}
}
一周热门 更多>