Javaweb项目中,jsp页面读取 WEB-INF/classes 下的 properties文件

WEB-INF/classes 目录下有  attribute.properties  文件,有 Title 值。

1、单个jsp页面中,直接读取

参考文章:http://blog.sina.com.cn/s/blog_7d73052b01013rx3.html

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8" import="java.io.*,java.util.* "%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title></title>  
 <%  
Properties pro = new Properties();  
String realpath = request.getRealPath("/WEB-INF/classes/");  
try
{  
	
	FileInputStream in = new FileInputStream(realpath+"/attribute.properties");  
	pro.load(in);  
}  
catch(FileNotFoundException e)
{  
    out.println(e);  
}  
catch(IOException e)
{
	out.println(e);
}  
String title = pro.getProperty("Title"); 
%>
</head>  
<body>  
<p><%=title %></p>
</body>  
</html> 

2、写一个Java类专门负责读取,在jsp页面中,调用这个类的方法。

参考文章:http://bbs.csdn.net/topics/300020717

http://blog.csdn.net/friendan/article/details/19839767

http://www.iteye.com/problems/33670

PropertiesUtil.java

package utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
 
public class PropertiesUtil {
	
  public static String getFilePath(){
	  String path=Thread.currentThread().getContextClassLoader().getResource("").toString(); 
	   
	  path=path.replace('/', '\\'); // 将/换成\  
	  path=path.replace("file:", ""); //去掉file:
	  path=path.replace("%20", " "); //将%20换成空格
	  path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...  
	  
	  return path;
  }
  
 //根据key读取value
 public static String getValueByKey(String fileName,String key) {
  
	 Properties props = new Properties();
        try {
         InputStream in = new BufferedInputStream (new FileInputStream(PropertiesUtil.getFilePath()+fileName));
         props.load(in);
         String value = props.getProperty (key);
            System.out.println(key+value);
            return value;
        } catch (Exception e) {
         e.printStackTrace();
         return null;
        }
 }
  
 
}
前台jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8" import="java.io.*,java.util.*,utils.* "%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title></title>  
 <%
   	PropertiesUtil p = new PropertiesUtil();
    String title = p.getValueByKey("attribute.properties", "Title");
   %>
</head>  
<body>  
<p><%=title %></p>
</body>  
</html>  






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值