ActionContext,ServletContext和ServletActionContext有什麼區別?
他們之間有很多區別。
ServletContext
從ServletContext
的包( javax.servlet.ServletContext
)我們可以知道它是標準的JavaEE
WebApplication類庫。
ServletContext提供了標準的Servlet執行時環境。 實際上是servlet與web容器進行通訊的一些方法。
publicinterfaceServletContext{// Returns the URL prefix for the ServletContext. public String getServletContextName(); //Returns the context-path for the web-application. public String getContextPath(); //Returns the ServletContext for the uri. public ServletContext getContext(String uri); //Returns the real file path for the given uri. public String getRealPath(String uri); public RequestDispatcher getNamedDispatcher(String servletName); public RequestDispatcher getRequestDispatcher(String uri);
ServletContext包含在ServletConfig中,ServletConfig通常從servlet或filter的init()
方法讀取:
servletConfig.getServletContext() filterConfig.getServletContext()
ActionContext
來自Struts,但是起初來自Struts1和Struts2,它們是不同的。
從Struts1:
一個servlet
(servlet
org.apache.struts.action.ActionServlet)處理所有的*.do
動作。
從Struts2: filter
(org.apache.struts2.dispatcher.FilterDispatcher)處理所有請求。
因為struts1屬於servlet範圍。 struts1動作本質上是servlet。
struts2動作是普通的Java bean,出了servlet限制。
在strtus2動作出來之後,ActionContext構成了丟失的WEB環境。
ActionContext主要功能:
- 提供WEB上下文。
- 解決執行緒安全問題。
- 解決與其他Framework不相容的問題(如:webLogic))
ServletActionContext
正如你所說,ServletActionContext是ActionContext的子類。 它的功能是從ActionContext開始,封裝方法,使其更加簡單直觀。
我們還可以研究其原始碼:
publicclassServletActionContextextendsActionContextimplementsStrutsStatics{//HTTP servlet request public static void setRequest(HttpServletRequest request) { ActionContext.getContext().put(HTTP_REQUEST, request); } public static HttpServletRequest getRequest() { return (HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST); } //HTTP servlet response public static void setResponse(HttpServletResponse response) { ActionContext.getContext().put(HTTP_RESPONSE, response); } public static HttpServletResponse getResponse() { return (HttpServletResponse) ActionContext.getContext().get(HTTP_RESPONSE); } //servlet context. public static ServletContext getServletContext() { return (ServletContext) ActionContext.getContext().get(SERVLET_CONTEXT); } public static void setServletContext(ServletContext servletContext) { ActionContext.getContext().put(SERVLET_CONTEXT, servletContext); }
從上面我們可以知道ServletActionContext擴充套件了ActionContext。