zhyiwww
          用平實的筆,記錄編程路上的點點滴滴………
          posts - 536,comments - 394,trackbacks - 0

          解決開發中多用戶修改消息,或者多用戶修改配置文件的沖突問題。
          注意在粗體部分實現一次將所有的配置資源文件都讀取到內存中去。任何一個用戶只需要自己舔加自己的配置文件就可以了。
          最后統一在實現配置文件的整合。


          寫一個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.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2010-03-20 18:20 zhyiwww 閱讀(425) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 鄂温| 东阿县| 壶关县| 建昌县| 古蔺县| 绥化市| 安顺市| 正蓝旗| 凤台县| 阳信县| 兴仁县| 阳西县| 苍梧县| 凯里市| 宜昌市| 来安县| 镇坪县| 新邵县| 汾阳市| 轮台县| 神农架林区| 邵阳县| 肃宁县| 潮安县| 扶余县| 双峰县| 塘沽区| 乳山市| 安塞县| 西藏| 缙云县| 锡林浩特市| 大名县| 开鲁县| 沾化县| 滨州市| 进贤县| 湘潭市| 嵊州市| 河北区| 连山|