Accessing the servlet API in Struts2

Source: Internet
Author: User

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

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.