解决开发中多用户修改消息,或者多用户修改配置文件的冲突问题。
注意在粗体部分实现一次将所有的配置资源文件都读取到内存中去。任何一个用户只需要自己舔加自己的配置文件就可以了。
最后统一在实现配置文件的整合。
写一个BaseHttpServlet:
package base.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
* Servlet implementation class CcxHttpServlet
*/
public class BaseHttpServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -5793303331880998441L;
protected Logger log = Logger.getLogger(this.getClass());
private final String MESSAGE_RESOURCE_FILE = "/com/xxx/project/i18/message.properties";
protected final static Properties messages = new Properties();
static{
// TODO you need change according to different project
String messageDir = "/com/ccx/information/i18";
URL url = BaseHttpServlet.class.getResource(messageDir);
File mf = new File(url.getFile());
if(mf.isDirectory()){
File[] l = mf.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.endsWith(".properties");
}
});
for(int i=0;i<l.length;i++){
File f = l[i];
try {
messages.load(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @see HttpServlet#HttpServlet()
*/
public BaseHttpServlet() {
super();
}
/**
* forword to page
*
* @param request
* @param response
* @param uri
* ,the target page
* @throws ServletException
* @throws IOException
*/
protected void farward(HttpServletRequest request,
HttpServletResponse response, String uri) throws ServletException,
IOException {
RequestDispatcher dis = request.getRequestDispatcher(uri);
if (dis == null) {
// to error page
output(response, "error : no page found");
} else {
dis.forward(request, response);
}
}
/**
* print information to client
*
* @param response
* @param notice
* @throws IOException
*/
protected void output(HttpServletResponse response, String notice)
throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("<span style=\"color:red\">");
out.println(notice);
out.print("</span>");
out.flush();
out.close();
}
/**
* send redirect to target page
*
* @param response
* @param uri
* @throws ServletException
* @throws IOException
*/
protected void redirect(HttpServletResponse response, String uri)
throws ServletException, IOException {
response.sendRedirect(uri);
}
public void init() throws ServletException {
super.init();
}
}
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2010-03-20 18:20
zhyiwww 阅读(416)
评论(0) 编辑 收藏 所属分类:
j2ee