-1

In Java EE, we often use ActionContext,ServletContext and ServletActionContext, they have similar name, but I don't know the difference between them.

I only know the ServletActionContext inherit the ActionContext. Someone can explain them?

s-n-2
  • 405
  • 1
  • 6
  • 24

1 Answers1

1

There have many difference between them.

ServletContext

From the ServletContext's package(javax.servlet.ServletContext) we can know it is standard JavaEE WebApplication class library.

ServletContext provides the standard Servlet run-time environment. Actually is some methods of servlet communicate with web container.

public interface ServletContext {

 // 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 is included in the ServletConfig, and ServletConfig often read from servlet or filter's init() method:

servletConfig.getServletContext()
filterConfig.getServletContext()

ActionContext

Comes from Struts, but at first comes from Struts1 and Struts2 they are different.

From Struts1:
A servlet (servlet org.apache.struts.action.ActionServlet) handle all the *.do action.

From Struts2:
The filter(org.apache.struts2.dispatcher.FilterDispatcher) handle all the request.

Because struts1 belongs to servlet scope. struts1 action's essentially is servlet.
struts2 action is normal Java bean, out of the servlet limitations.
The ActionContext makes up the lost WEB environment after the strtus2 action out of the stand servlet framework.

The ActionContext main function:

  • Provide the WEB context.
  • Solve the thread security issue.
  • Solve the incompatibility problem with other Framework(such as: webLogic))

ServletActionContext

As you say, ServletActionContext is ActionContext's subclass. Its functions is starting at ActionContext, and encapsulate the methods, make it more simple and intuitive.

We can also study its source code:

public class ServletActionContext extends ActionContext implements StrutsStatics {

      //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);
      }

From above we can know the ServletActionContext extends ActionContext.

aircraft
  • 25,146
  • 28
  • 91
  • 166