javaweb项目中常用知识

来源:互联网 发布:电玩城源码 编辑:程序博客网 时间:2024/06/13 13:13

1.获取项目根路径

public static String getRootPath(HttpServletRequest request, boolean flag) {
String basePath = request.getContextPath();
basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + basePath
+ (flag ? "/" : "");
return basePath;
}


2.获取配置文件中的配置

public class PropertyTools {
//配置文件名称
private final static String PROPERY_NAME="properties/default.properties";
//静态资源文件
private static Properties staticProperties;

//初始化变量
static{
//如果静态存储
if(staticProperties == null){
initPropertiy();
}
}

/**
* 获取配置文件中的配置
* @param key 配置名称
* @return
*/
public static String getValue(String key){
return staticProperties.getProperty(key);
}


/**
* 存储MAP初始化方法
*/
private static synchronized void initPropertiy(){
if(staticProperties == null){
try {
//实例化资源
Resource resource = new ClassPathResource(PROPERY_NAME);
//实例化配置文件
staticProperties = new Properties();
//读取资源数据
staticProperties.load(resource.getInputStream());

catch (IOException e) {
e.printStackTrace();
}
}
}
}


0 0