您好,登錄后才能下訂單哦!
需求說明
做項目時,為了省事,起初把初始化的配置都放在每個類中 static加載,初始化配置一多,就想把它給整理一下,這里使用servlet中的init方法初始化。
web.xml說明
首先了解下web.xml中元素的加載順序:
完整加載順序:ServletContext -> context-param -> listener-> filter -> servlet
配置實(shí)現(xiàn)
InitServlet.java:
/** * 初始化系統(tǒng)參數(shù) * 創(chuàng)建者 科幫網(wǎng) * 創(chuàng)建時間 2017年5月10日 * */ public class InitServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void init(){ try { if(Constants.PAY_URL.size()==0){ List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl(); for(CommonEntity entity:listPayUrl){ Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName()); } } LogUtil.info("佛祖保佑 永不宕機(jī) 永無BUG :初始化系統(tǒng)數(shù)據(jù)數(shù)量:"+Constants.PAY_URL.size()); Configs.init("zfbinfo.properties"); LogUtil.info("初始化支付寶配置信息"); SDKConfig.getConfig().loadPropertiesFromSrc(); LogUtil.info("初始化銀聯(lián)支付配置信息"); } catch (Exception e) { e.printStackTrace(); } } /** * 重新加載配置文件 * @Author 科幫網(wǎng) * @param request * @param response * @throws ServletException * @throws IOException * @Date 2017年5月10日 * 更新日志 * 2017年5月10日 張志朋 首次創(chuàng)建 * */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Constants.PAY_URL = new ConcurrentHashMap<String, String>(); List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl(); for(CommonEntity entity:listPayUrl){ Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName()); } LogUtil.info("初始化系統(tǒng)數(shù)據(jù)數(shù)量:"+Constants.PAY_URL.size()); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
web.xml:(部分配置)
<!-- 初始基礎(chǔ)化數(shù)據(jù)--> <servlet> <servlet-name>InitServlet</servlet-name> <servlet-class>com.acts.web.common.servlet.InitServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>InitServlet</servlet-name> <url-pattern>/InitServlet</url-pattern> </servlet-mapping>
servlet介紹
什么是servlet
servlet是sun公司為開發(fā)動態(tài)web而提供的一門技術(shù),用戶若想用發(fā)一個動態(tài)web資源(即開發(fā)一個Java程序向?yàn)g覽器輸出數(shù)據(jù)),需要完成以下2個步驟:
按照一種約定俗成的稱呼習(xí)慣,通常我們也把實(shí)現(xiàn)了servlet接口的java程序,稱之為Servlet。
servlet的運(yùn)行過程
servlet初始化
配置load-on-startup后,servlet在startup后立即加載,但只是調(diào)用servlet的init()方法,用以初始化該servlet相關(guān)的資源。初始化成功后,該servlet可響應(yīng)web請求;如未配置load-on-startup,容器一般在第一次響應(yīng)web請求時,會先檢測該servlet是否初始化,如未初始化,則調(diào)用servlet的init()先初始化,初始化成功后,再響應(yīng)請求。
PS:一般我們在開發(fā)web應(yīng)用時,都會配置這個參數(shù),有兩個好處:
關(guān)于load-on-startup一些官網(wǎng)說明:
If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed.
注意
使用servlet時,一般都是繼承httpServlet,然后分別實(shí)現(xiàn)doGet或者doPost方法,但是在這里面要注意的是,這servlet并不是線程安全的,多線程單實(shí)例執(zhí)行的,當(dāng)并發(fā)訪問同一個資源的話(成員變量等等),就有可能引發(fā)線程安全問題。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。