The Servlet APIs that are commonly accessed in WEB applications are HttpServletRequest, HttpSession, and ServletContext, which represent the request, session, and appli of the JSP built-in objects, respectively, in the three interfaces cation.
One, through the Actioncontext class of struts to access (indirect access).
EG1:
Action file:
Struts.xml file:
JSP page:
Second, struts through Servletcontextaware, Servletrequestaware, Servletresponseaware interface direct access.
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 HttpServletResponse instance of the server response.
EG2:
Implementation steps: Implement the Servletresponseaware interface, only need to implement the public void Setservletresponse (HttpServletResponse response) method, There is a httpservletresponse parameter in the method that represents the response of the Web app to the client. The wording is shown in EG2. Similarly, if you implement the Servletrequestaware interface and the Servletcontextaware interface, the operation is the same as the previous Servletresponseaware operation.
Third, struts accesses the Servlet API through the tool class Servletactioncontext (not recommended)
Static PageContext Getpagecontext (): Gets the PageContext object for 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-ation Accessing the Servlet API