Access Servlet API by Action
The Action in Struts2 is not coupled with any Servlet API, so that the framework is more flexible and easier to test.
For Web application controllers, it is almost impossible to access the ServletAPI. In Web applications, ServletAPI is usually accessed by HttpServletRequest, HttpSession, and ServletContext. These three interfaces represent the request, session, and application of the embedded objects in JSP respectively.
Struts 2 provides an ActionContext class. The action of Struts 2 can be used to access the Servlet API
The ActionContext class provides a static method getContext (), which returns an instance of ActionContext. This instance provides some methods to access Servlet APIs.
Method Name |
Function Description |
GetContext () |
Static method to obtain the system's ActionContext instance |
GetSession () |
Returns a Map object stored in the HttpSession instance. |
SetSession (Map session) |
Directly input a Map instance to convert the total key and value of the map instance to the attribute name and attribute value of the session. |
GetApplication () |
Returns a Map object stored in the ServletContext instance. |
SetApplication () |
Directly input a Map instance to convert the key and value of the instance to the attribute name and attribute value of the application. |
GetParameters () |
Obtain all request parameters. Similar to calling the getParameterMap method of the HttpServletRequest object |
ActionContext accesses the Servlet API through ActionContext in the Action class. This Action demonstrates setting the attribute values for the request, session, and application ranges.
1 ActionContext ctx = ActionContext. getContext (); 2 3 // get Session 4 Map session = ctx. getSession (); 5 6 // obtain Application 7 Map application = ctx. getApplication ();
Struts 2 can be completely separated from the Servlet API, so that it can be run out of the Web container, and can be used to test the Action from the Web container; allows you to operate attributes of the request, session, and application ranges in a simple way.
Action directly accesses Servlet API
Although Struts 2 provides ActionContext to access Servlet APIs, such access does not directly obtain the instance of Servlet APIs. Struts 2 provides several interfaces for Action to directly access Servlet APIs.
Implementation Interface Name |
Interface Description |
ServletContextAware |
The Action that implements this interface can directly access the ServletContext instance of the Web application. |
ServletRequestAware |
The Action that implements this interface can directly access the HttpServletRequest instance requested by the user. |
ServletResponseAware |
The Action that implements this interface can directly access the HttpServletResponse response of the server. |
1 private HttpServletResponse response; 2 // rewrite ServletResponseAware Interface 3 public void setServletResponse (HttpServletResponse response) {4 this. response = response; 5 6}
As shown above, to implement the ServletResponseAware interface, you only need to implement the following methods:
1 public void setServletResponse(HttpServletResponse response);
Similarly, if an Action implements ServletRequestAware, You need to implement the following methods:
1 public void setServletRequest(HttpServletRequest request);
Similarly, if an Action implements ServletContextAware, You need to implement the following methods:
1 public void setServletContext(ServletContext context);
Even if we obtain the httpServletRespose object in the Action class of struts 2, do not directly generate a response to the client in the Action.
Use ServletActionContext to access Servlet APIs
To directly access the Servlet API, struts 2 also provides a ServletAction tool class, which includes the following static methods:
Return Value |
Method Name |
Function Description |
HttpServletRequest |
GetRequest () |
Obtain the HttpServletRequest object of the Web application. |
HttpServletResponse |
GetResponse () |
Obtain the HttpservletResponse object of the Web application. |
ServletContext |
GetServletContext () |
Obtain the ServletContext object of the Web application. |
PageContext |
GetServletContext () |
Obtain the ServletContext object of the Web application. |
With the help of the ServletActionContext tool, Action can access Servlet APIs in a simpler way.
Read Li Gang's lightweight java EE Enterprise Application practices