Several ways to access Servlet API in Struts2

One: through the ActionContext class

Struts2 provides the ActionContext class, through which struts2 can access the Servlet API.

The ActionContext class provides the following methods:

1: Object get(Object key): similar to calling the getAttribute(String name) method of HttpServletRequest

2:Map getApplication(): Returns a Map object that simulates the application's ServletContext instance.

3:static ActionContext getContext(): This is a static method to get the ActionContext instance of the system

4:Map getParameters(): Get all the request parameters, similar to calling the getParameterMap() method of the HttpServletRequest object

5:Map getSession() returns a Map object that simulates an HttpSession instance

6:void setApplication(Map application): Pass in a Map instance and convert the key-value of the Map instance into the attribute name-value of the application.

7:void setSession(Map session): Pass in a Map instance and convert the key-value of the Map instance into the attribute name-value of the application.

Example: Implementing a simple prompt operation through the ActionContext class



Two: Action directly accesses the Servlet API

Struts2 provides the following interfaces so that Action can directly access the Servlet API:

1: ServletContextAware: Actions that implement this interface can directly access the ServletContext instance of the Web application

2: ServletRequest Aware : Actions that implement this interface can directly access the ServletRequest instance of the Web application.

3: ServletResponse Aware : Actions that implement this interface can directly access the ServletRsponse instance of the Web application.

Taking the ServletResponse Aware interface as an example to implement this interface, we need to implement the response parameter in the ServletResponse (HttpServletResponse response) method to respond to the user from the Web client. We should first set this parameter to the Action parameter attribute so that it can be used in the execute() method. The HttpServletResponse object, as shown in the figure: Add cookies by implementing the ServletContextAware interface




This execute() implements adding cookies via ServletActionContext().getResponse().

Three: Use ServletActionContext to access Servlet API

Struts2 also provides a ServletActionContext utility class for accessing the Servlet API.

This class contains the following static methods:

1: static PageContext getPageContext(): Get the PageContext object of the Web application.

 2: static HttpServletRequest getRequest(): Get the HttpServletRequest object of the Web application .

1: static HttpServletResponse getResponse(): Get the HttpServletResponse object of the Web application .

1: static ServletContext getServletContext(): Get the ServletContext object of the Web application .

Example: Adding Cookies via ServletActionContext.getResponse() Method


In the end, the desired result can be achieved:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325998210&siteId=291194637