?
Struts
源碼的切片學習(三)
——
ActionServlet
的初始化細節
?
Author
???? :????? zhyiwww
E-Mail
????? :????? zhyiwww@163.com
Date
????????? :????? 2007-1-10
轉載請注明出處
m.tkk7.com/zhyiwww
(copyright by @ zhangyi)
?
在
ActionServlet
初始化的過程中,有一個函數
initServlet()
用來初始化此
ActionServlet,
主要是初始化其
url,
也就是到底此
ActionServlet
接收和處理那些
url
。
?
我們先看一下代碼:
protected void initServlet() throws
ServletException {
?
???????
// Remember our servlet name
???????
this.servletName = getServletConfig().getServletName();
?
???????
// Prepare a Digester to scan the web application deployment descriptor
??????
?Digester digester = new Digester();
??????
?
digester.push(this);
???????
digester.setNamespaceAware(true);
???????
digester.setValidating(false);
?
???????
// Register our local copy of the DTDs that we can find
???????
for (int i = 0; i < registrations.length; i += 2) {
???????????
URL url = this.getClass().getResource(registrations[i+1]);
???????????
if (url != null) {
???????????????
digester.register(registrations[i], url.toString());
???????????
}
??????
?}
?????????? /********************************************
????????????????
上面的紫色的代碼是把當前的
ActionServlet
實例放入到解析堆棧中。這一步很重要。
????????????????
正是
actionServlet
放入了堆棧,下面的調用所取得配置參數才正是
actionSerlvet
的配置參數,而不是其他的
servlet
的配置參數。
??????????
這個地方是一個難點。
?????
????? ********************************************/
????????????????????
?
???????
// Configure the processing rules that we need
????
???digester.addCallMethod("web-app/servlet-mapping",
??????????????????????????????
"addServletMapping", 2);
???????
digester.addCallParam("web-app/servlet-mapping/servlet-name",
0);
???????
digester.addCallParam("web-app/servlet-mapping/url-pattern",
1);
????????????????????
????????????? /************************************************************
????????????????????
上面的代碼執行的時候,
digester
就會調用
actionServlet
中的
addServletMapping()
方法,并傳進去兩個參數。
????????????????????
在這個方法里面,就初始化了
servlet
的
url,
,也就是那種后綴的
ur
要由
actionServlet
來接收和處理。
????????????????????
例如:
*.do
還是
/action/*.do
等。
?????????????
這個
servlet
的
url
保存在
actionServlet
的
servletMapping
參數里面。
?????????
????????????? ************************************************************/
?
???????
// Process the web application deployment descriptor
???????
if (log.isDebugEnabled()) {
???????????
log.debug("Scanning web.xml for controller servlet mapping");
???????
}
?
???
????InputStream input =
???????????
getServletContext().getResourceAsStream("/WEB-INF/web.xml");
?
???????
if (input == null) {
???????????
log.error(internal.getMessage("configWebXml"));
???????????
throw new
ServletException(internal.getMessage("configWebXml"));
???????
}
?
???????
try {
???????????
digester.parse(input);
?
???????
} catch (IOException e) {
???????????
log.error(internal.getMessage("configWebXml"), e);
???????????
throw new ServletException(e);
?
???????
} catch (SAXException e) {
???????????
log.error(internal.getMessage("configWebXml"), e);
???????????
throw new ServletException(e);
?
???????
} finally {
???????????
try {
??????????????? input.close();
???????????
} catch (IOException e) {
???????????????
log.error(internal.getMessage("configWebXml"), e);
??????????????? throw new ServletException(e);
???????????
}
???????
}
?
???????
// Record a servlet context attribute (if appropriate)
???????
if (log.isDebugEnabled()) {
???????????
log.debug("Mapping for servlet '" + servletName + "' =
'" +
??????????????? servletMapping +
"'");
????
???}
?
???????
if (servletMapping != null) {
????????
???getServletContext().setAttribute(Globals.SERVLET_KEY,
servletMapping);
???????
}
?
}
?
上面的過程就是
initServlet
的初始化過程,其中的難點就是對
digester.push()
的理解。
???
附:??? public void addServletMapping(String servletName, String urlPattern) {
??????? if (log.isDebugEnabled()) {
??????????? log.debug("Process servletName=" + servletName +
????????????????????? ", urlPattern=" + urlPattern);
??????? }
??????? if (servletName == null) {
??????????? return;
??????? }
??????? if (servletName.equals(this.servletName)) {
??????????? this.servletMapping = urlPattern;
??????? }
??? }
|----------------------------------------------------------------------------------------|
版權聲明 版權所有 @zhyiwww
引用請注明來源 http://m.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2007-01-29 17:18
zhyiwww 閱讀(2064)
評論(1) 編輯 收藏 所屬分類:
j2ee