Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the HttpServletResponse object in the Action class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to obtain HttpServletResponse objects in the Action class. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

In the execute method of the struts1.x Action class, there are four parameters, two of which are response and request. In Struts2, there are no parameters, so you can't simply get a HttpServletResponse or HttpServletRequest object from the execute method.

However, there are still many ways to get these objects in the Struts2 Action class. Here are four ways to get these objects.

[method 1] use Struts2 Aware interceptor

This approach requires the Action class to implement the corresponding interceptor interface. If we want to get the HttpServletResponse object, we need to implement the org.apache.struts2.interceptor.ServletResponseAware interface as follows:

Package action; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; import org.apache.struts2.interceptor.*; public class MyAction extends ActionSupport implements ServletResponseAware {private javax.servlet.http.HttpServletResponse response; / / get HttpServletResponse object public void setServletResponse (HttpServletResponse response) {this.response = response } public String execute () throws Exception {response.getWriter () .write ("implement ServletResponseAware interface");}}

In the above code, MyAction implements a ServletResponseAware interface and implements the setServletResponse method. If an action class implements the ServletResponseAware interface, Struts2 calls the setServletResponse method before calling the execute method, passing the response parameter into the method. If you want to get objects such as HttpServletRequest, HttpSession, and Cookie, the action class can implement interfaces such as ServletRequestAware, SessionAware, and CookiesAware, respectively. These interfaces are all in the org.apache.struts2.interceptor package.

If you want to get the request parameters, the action class can implement org.apache.struts2.interceptor. ParameterAware interface, but if you only want to determine whether a parameter exists, you can also implement com.opensymphony.xwork2.interceptor. ParameterNameAware interface. This interface has an acceptableParameterName method, which is called once when Struts2 gets a request parameter. The reader can record all the request parameters in this method for later use. This method is defined as follows:

Boolean acceptableParameterName (String parameterName)

[method 2] use RequestAware interceptor

This method is similar to the first method. The action class needs to implement an org.apache.struts2.interceptor.RequestAware interface. The difference is that RequestAware will get a com.opensymphony.xwork2.util.OgnlValueStack object that can get response, request, and other information. The code is as follows:

Package action; import java.util.Map; import org.apache.struts2.*; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; import com.opensymphony.xwork2.util.*; import org.apache.struts2.interceptor.*; public class FirstAction extends ActionSupport implements RequestAware {private Map request; private HttpServletResponse response; public void setRequest (Map request) {this.request = request } public String execute () throws Exception {java.util.Set

< String>

Keys = request.keySet (); / / enumerates all key values. There is actually only one key:struts.valueStack for (String key: keys) System.out.println (key); / / get the OgnlValueStack object OgnlValueStack stack = (OgnlValueStack) request.get ("struts.valueStack"); / / get the HttpServletResponse object response = (HttpServletResponse) stack.getContext () .get (StrutsStatics.HTTP_RESPONSE) Response.getWriter () .write ("implement the RequestAware interface");}}

We can also use StrutsStatics.HTTP_REQUEST and StrutsStatics.PAGE_CONTEXT to get HttpServletRequest and PageContext objects. This method is troublesome and is rarely used. Readers can use it as a reference.

[method 3] use the ActionContext class

This method is relatively simple, and we can get the corresponding object through the get method of the org.apache.struts2.ActionContext class. The code is as follows:

HttpServletResponse response = (HttpServletResponse)

ActionContext.getContext () get (org.apache.struts2.StrutsStatics.HTTP_RESPONSE)

HttpServletRequest request = (HttpServletRequest)

ActionContext.getContext () get (org.apache.struts2.StrutsStatics.HTTP_REQUEST)

[method 4] use the ServletActionContext class

Struts2 provides us with the easiest way to get HttpServletResponse and other objects. This is the org.apache.struts2.ServletActionContext class. We can directly use the getRequest and getResponse methods of the ServletActionContext class to get the HttpServletRequest and HttpServletResponse objects. The code is as follows:

HttpServletResponse response = ServletActionContext.getResponse ()

Response.getWriter () .write (hello world)

From the perspective of these four methods, the first is the simplest, and readers can choose which method to use to obtain these objects according to their own needs and requirements.

On how to get the HttpServletResponse object in the Action class to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report