Struts2-ation Accessing the Servlet API

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.