The servlet APIs that are commonly accessed in Web applications are HttpServletRequest, HttpSession, and ServletContext, which represent the request in the JSP built-in object, the three interfaces Session and application.
1. Use the Actioncontext class provided by STRUTS2 to access the servlet API. Here are a few common methods that 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.
eg
To set the properties of a application range through Actioncontext
Actioncontext CTX = actioncontext.getcontext (); Ctx.getApplication.put ("name", "AAA");
To set the properties of the request scope via Actioncontext
Ctx.put ("name", "BBB");
2. Although STRUTS2 provides actioncontext to access the Servlet API, this access is not directly an instance of the Servlet API, in order to access the Serlvet API directly in action. The STRUTS2 provides several interfaces as follows :
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 requested by the user.
Servletresponseaware: The action that implements the interface can directly access the Httpserlvetresponse instance of the server response.
3. Accessing the servlet API using Servletactioncontext
In order to have direct access to the servlet Api,struts2 also provides a Servletactioncontext tool class that contains the following static methods.
Static PageContext Getpagecontext (): Gets the PageContext object of the Web App.
Static HttpServletRequest getrequest (): Gets the HttpServletRequest object for the Web App.
Static HttpServletResponse GetResponse (): Gets the HttpServletResponse object for the Web App.
Static ServletContext Getservletcontext (): Gets the ServletContext object for the Web App.
STRUTS2 Note--action accessing the servlet API