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>