Explain details of several ways to access the Servlet API in Struts2
Request and Response objects are common in normal web development, but in the Struts2 framework, because Action can interact with JSP pages in data, these two objects are usually not used. If you want to use these two objects in the Struts2 program, there are also solutions
Struts2's Action is not directly coupled to any Servlet API, which is an improvement of Struts2, because the Action class is no longer coupled to the Servlet API, making it easier to test the Action. But how to access it?
The Servlet APIs that usually need to be accessed in web applications are the three classes HttpServletRequest, HttpSession, and ServletContext, representing the request, session, and application in the built-in JSP objects.
Method 1: (It is generally recommended to use IOC method, you can only get request, but you can't get response)
Struts2 provides the ActionContext class to access the Servlet API through the ActionContext class.
Below are several common methods included in the ActionContext class.
1. public Object get(Object key): Get the value of the key in HttpServletRequest;
2. void put(String key,Object value): Set the value of the key in HttpServletRequest to value;
3. public Map getApplication(): Get the Map object encapsulating the ServletContext;
4. void setApplication(Map application): Set ServletContext instance;
5. Static ActionContext getContext(): a static method to obtain the system's ActionContext instance;
6. Map getParameters(): Similar to the getParametersMap method in HttpServletRequest;
7. public Map getSession(): Get the Map object encapsulated by HttpSession;
8. void setSession(Map session): directly pass into a Map instance and convert the key-value pair in the Map instance into the attribute name and attribute value of the session;
Method 2: (not recommended, troublesome, non-IOC method, large coupling with Servlet API)
Although Struts2 provides ActionContext to access the Servlet API, this access cannot directly obtain the Servlet API. In order to directly access the Servlet API in Action, Struts2 also provides the following interface.
1. ServletContextAware: The action that implements this interface can directly access the ServletContext instance of the web application;
2. ServletRequestAware: The action that implements this interface can directly access the instance of the HttpServletRequest of the user request object;
3. ServletResponseAware: The Action implementing this interface can directly access the instance of HttpServletResponse that the server responds;
Method 3: (Not IOC method, highly recommended)
Struts2 also provides a ServletActionContext, whose static methods are: getPageContext(), getRequest(), getResponse(), and getServletContext().
1、HttpServletRequest request=();
2、HttpServletResponse response=();
3、().setAttribute("username","admin");
4、("password","123456");
The above are several ways to access the Servlet API in Struts2. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!