Describes several methods for accessing Servlet APIS by Action in Struts2, struts2servlet

Source: Internet
Author: User

Describes several methods for accessing Servlet APIS by Action in Struts2, struts2servlet

Several methods for accessing Servlet APIS by Action in Struts2

In general web development, Request and Response objects are common. However, in the Struts2 framework, because Action can interact with JSP pages, these two objects are usually not used. If you want to use these two objects in the Struts2 program, there is also a solution

The Action of Struts2 is not directly coupled with any Servlet API. This is an improvement of Struts2 because the Action class is no longer coupled with the Servlet API and can be easily tested. But how to access it?

The Servlet APIs that Web applications usually need to access are HttpServletRequest, HttpSession, and ServletContext, which respectively represent the request, session, and application in the JSP built-in objects.

Method 1: (generally, we recommend that you use the IOC method. You can only obtain the request, but not the response method)

Struts2 provides the ActionContext class to access the Servlet API through the ActionContext class.

Below are several common methods contained in the ActionContext class.

1. public Object get (Object key): get the key value in HttpServletRequest;
2. void put (String key, Object value): Set the key value in HttpServletRequest to value;
3. public Map getApplication (): obtains the Map object encapsulated with ServletContext;
4. void setApplication (Map application): sets the ServletContext instance;
5. static ActionContext getContext (): obtains the ActionContext instance of the system;
6. Map getParameters (): similar to the getParametersMap method in HttpServletRequest;
7. public Map getSession (): gets the Map object encapsulated with HttpSession;
8. void setSession (Map session): directly input a Map instance and convert the key-value pair in the Map instance to the attribute name and attribute value of the session;

Method 2: (not recommended, troublesome, non-IOC mode, large coupling with Servlet APIs)

Although Struts2 provides ActionContext to access Servlet APIs, such access cannot directly obtain Servlet APIs. to directly access Servlet APIs in actions, Struts2 also provides the following interfaces.

1. ServletContextAware: The Action implementing this interface can directly access the ServletContext instance of the Web application;
2. ServletRequestAware: The Action that implements this interface can directly access the HttpServletRequest instance of the user request object;
3. ServletResponseAware: The Action that implements this interface can directly access the server's response HttpServletResponse instance;

Method 3: (non-IOC mode, recommended)

Struts2 also provides a ServletActionContext. Its static methods include getPageContext (), getRequest (), getResponse (), and getServletContext ().

1. HttpServletRequest request = ServletActionContext. getRequest ();
2. HttpServletResponse response = ServletActionContext. getResponse ();
3. request. getSession (). setAttribute ("username", "admin ");
4. request. setAttribute ("password", "123456 ");

The above are several methods for accessing Servlet API by Action in Struts2. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

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.