STRUTS2 Note--action 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 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

Related Article

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.