The action in Struts2 is not coupled to any servlet API, but it is almost impossible for the controller of the Web application to access the Servlet API, such as the need to track the HTTP session state. A Actioncontext class is provided in Struts2 that STRUTS2 's action can use to access the servlet API.
Several common methods are included in the Actioncontext class:
object get (Object key): This method is similar to calling HttpServletRequest's getattribute (String name) method.
map Getapplication: Returns a Map object that simulates the ServletContext instance of the app.
Static Actioncontext getcontext (): Gets the Actioncontext instance of the system.
Map getparameters (): Gets all the request parameters. Similar to the Getparametermap () method that invokes the HttpServletRequest object.
map getsession (): Returns a Map object that simulates the HttpSession instance.
void Setapplication (Map application): Directly into a map instance, key-value in the map instance is converted to application property name, attribute value.
void Setsession (map session): Directly into a map instance, the map instance of the Key-value pair into the session's property name, property values.
Example:
public class Textaction implements action{
Public String Execute () throws exception{
Actioncontext Ctx=actioncontext.getcontext (); Get Actioncontext Instance
Integet counter= (Integer) ctx.getapplication (). Get ("counter"); Gets the property value of the application range
return SUCCESS;
}
}
Although STRUTS2 provides actioncontext to access the Servlet API, this access is not directly an instance of the Servlet API, and the following interfaces are provided in order to access the servlet api,struts2 in action:
Servletcontextaware: The action that implements the interface can directly access the ServletContext instance of the Web App.
Servletrequestaware: The action that implements the interface can directly access the HttpServletRequest instance of the Web App.
Servletresponseaware: The action that implements the interface can directly access the HttpServletResponse instance of the server response.
In addition, in order to have direct access to the servlet Api,struts2 also provides a Servletactioncontext tool class that contains several static methods such as the following:
static PageContext Getpagecontext (): Gets the PageContext object for the Web App.
static Httpservletrequset getrequets (): Gets the Httpservletrequset object for the Web App.
static Httpservletrsponse GetResponse (): Gets the HttpServletResponse object for the Web App.
static ServletContext Getservletcontext (): Gets the ServletContext object for the Web App.
Accessing the servlet API in Struts2