Method 1. An ActionContext is returned through ActionContext. getActionContext ().
Obtain
The code is as follows: |
Copy code |
ActionContext ac = ActionContext. getContext (); Ac. get ("tip"); // The default value is to obtain the attributes in the request and the tip attributes of the built-in request object. If you want to obtain attributes of other ranges Ac. getApplication (). get ("tip") // Obtain the tip attribute of the application range Ac. getApplication (). put ("tip", "abc"); // Set the tip attribute of the application range to abc. Ac. getSession (). get ("tip"); // gets the tip attribute of the session range Ac. getSession (). put ("tip", "abc"); // Set the tip attribute of the session range to abc. |
Map application = ac. getApplication ();
Note: The above method obtains the map object, which is the real application simulated by struts and is encapsulated in the struts framework, this feature decouples action and servlet APIs.
Method 2. Implement the following interface
ServletContextAware: this interface can be used to access the ServletContext instance of the Web application.
ServletRequestAware: this interface can be used to access the HttpServletRequest instance of the Web application.
ServletResponseAware: this interface can be used to access the HttpServletResponse instance of the Web application.
Example:
The code is as follows: |
Copy code |
Public class LoginAction extends ActionSupport implements ServletRequestAware { HttpServletRequest hsr = null; @ Override Public void setServletRequest (HttpServletRequest request ){ Hsr = request; } @ Override Public String execute () throws Exception { Hsr. getAttribute ("tip"); // Obtain the tip attribute of the request range. } } |
Other interfaces are similar to this;
Note: the obtained result is a pure jsp built-in object, which is not encapsulated by struts. That is to say, our actions can directly use Servlet APIs, and at the same time produce coupling between them.
Method 3. ServletActionContext
Using this static class, you can obtain the built-in jsp objects between them.
The code is as follows: |
Copy code |
ServletActionContext. getServletContext (); ServletActionContext. getRequest (); ServletActionContext. getRequest (). getSession (); |
Note: I personally think this method is more convenient. If you want to use the api in servlet directly, we recommend this method.