Home Database Mysql Tutorial strust2 获取HttpServletResponse对象

strust2 获取HttpServletResponse对象

Jun 07, 2016 pm 03:43 PM
httpservletresponse Obtain

【方法1】使用Struts2 Aware拦截器 这种方法需要Action类实现相应的拦截器接口。如我们要获得HttpServletResponse对象,需要实现org.apache.struts2.interceptor.ServletResponseAware接口,代码如下: packageaction; importcom.opensymphony.xwork2.Action

【方法1】使用Struts2 Aware拦截器

  这种方法需要Action类实现相应的拦截器接口。如我们要获得HttpServletResponse对象,需要实现org.apache.struts2.interceptor.ServletResponseAware接口,代码如下:

  packageaction;

importcom.opensymphony.xwork2.ActionSupport;<br> importjavax.servlet.http.*;<br> importorg.apache.struts2.interceptor.*;<br> publicclassMyActionexten<wbr>dsActionSupportimplement<wbr>sServletResponseAware<br> {<br>   privatejavax.servlet.http.HttpServletResponserespo<wbr>nse;<br>   //获得HttpServletResponse对象<br>   publicvoidsetServletResp<wbr>onse(HttpServletResponserespo<wbr>nse)<br>   {<br>     this.response=response;<br>   }  <br>   publicStringexecute()throwsException<br>   {  <br>     response.getWriter().write("实现ServletResponseAware接口");<br>   }<br> }</wbr></wbr></wbr></wbr></wbr>

  在上面的代码中,MyAction实现了一个ServletResponseAware接口,并且实现了setServletResponse方法。如果一个动作类实现了ServletResponseAware接口,Struts2在调用execute方法之前,就会先调用setServletResponse方法,并将response参数传入这个方法。如果想获得HttpServletRequest、HttpSession和Cookie等对象,动作类可以分别实现ServletRequestAware、SessionAware和CookiesAware等接口。这些接口都在org.apache.struts2.interceptor包中。

 如果要获得请求参数,动作类可以实现org.apache.struts2.interceptor. ParameterAware接口,但如果只想判断某个参数是否存在,也可以实现com.opensymphony.xwork2.interceptor. ParameterNameAware接口。这个接口有一个acceptableParameterName方法,当Struts2获得一个请求参数时,就会调用一次。读者可以在这个方法中将所有的请求参数记录下来,以便以后使用。这个方法的定义如下:

boolean acceptableParameterName(String parameterName);

  【方法2】使用RequestAware拦截器

  这种方法和第1种方法类似。动作类需要实现一个org.apache.struts2.interceptor.RequestAware接口。所不同的是RequestAware将获得一个com.opensymphony.xwork2.util.OgnlValueStack对象,这个对象可以获得response、request及其他的一些信息。代码如下所示:

  packageaction;

importjava.util.Map;<br> importorg.apache.struts2.*;<br> importcom.opensymphony.xwork2.ActionSupport;<br> importjavax.servlet.http.*;<br> importcom.opensymphony.xwork2.util.*;<br> importorg.apache.struts2.interceptor.*;<br> publicclassFirstActionex<wbr>tendsActionSupportimplem<wbr>entsRequestAware<br> {<br>   privateMaprequest;<br> privateHttpServletRespon<wbr>seresponse;<br>   publicvoidsetRequest(Maprequest)<br>   {<br>     this.request=request;    <br>   }  <br>   publicStringexecute()throwsException<br>   {  <br>     java.util.Set<string>keys=request.keySet();<br>     //枚举所有的key值。实际上只有一个key:struts.valueStack<br>     for(Stringkey:keys)<br>       System.out.println(key);<br>     //获得OgnlValueStack对象<br>     OgnlValueStackstack=(OgnlValueStack)myRequest.get("struts.valueStack");<br>     //获得HttpServletResponse对象<br>     response= (HttpServletResponse)stack.getContext().get(StrutsStatics.HTTP_RESPONSE);<br>     response.getWriter().write("实现RequestAware接口");<br>   }<br> }</string></wbr></wbr></wbr>

 我们也可以使用StrutsStatics.HTTP_REQUEST、StrutsStatics.PAGE_CONTEXT来获得HttpServletRequest和PageContext对象。这种方法有些麻烦,一般很少用,读者可以作为一个参考。

  【方法3】使用ActionContext类

  这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:

  HttpServletResponse response = (HttpServletResponse)<br> ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);<br> HttpServletRequest request = (HttpServletRequest)<br> ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

  【方法4】使用ServletActionContext类

  Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。这就是org.apache.struts2.ServletActionContext类。我们可以直接使用ServletActionContext类的getRequest、getResponse方法来获得HttpServletRequest、HttpServletResponse对象。代码如下:

  HttpServletResponse response = ServletActionContext.getResponse()<br>   response.getWriter().write("hello world");

  从这四种方法来看,最后一种是最简单的,读者可以根据自己的需要和要求来选择使用哪一种方法来获得这些对象。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get file extension in Python? How to get file extension in Python? Sep 08, 2023 pm 01:53 PM

A file extension in Python is a suffix appended to the end of a file name to indicate the format or type of the file. It usually consists of three or four characters, a file name followed by a period, such as ".txt" or ".py". Operating systems and programs use file extensions to determine what type of file it is and how it should be processed. Recognized as a plain text file. File extensions in Python are crucial when reading or writing files because it establishes the file format and the best way to read and write data. For example, the ".csv" file extension is the extension used when reading CSV files, and the csv module is used to process the files. Algorithm for obtaining file extension in Python. Manipulate file name string in Python.

Use math.Max ​​function to get the maximum value in a set of numbers Use math.Max ​​function to get the maximum value in a set of numbers Jul 24, 2023 pm 01:24 PM

Use the math.Max ​​function to obtain the maximum value in a set of numbers. In mathematics and programming, it is often necessary to find the maximum value in a set of numbers. In Go language, we can use the Max function in the math package to achieve this function. This article will introduce how to use the math.Max ​​function to obtain the maximum value in a set of numbers, and provide corresponding code examples. First, we need to import the math package. In the Go language, you can use the import keyword to import a package, as shown below: import"mat

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Google Authenticator is a tool used to protect the security of user accounts, and its key is important information used to generate dynamic verification codes. If you forget the key of Google Authenticator and can only verify it through the security code, then the editor of this website will bring you a detailed introduction on where to get the Google security code. I hope it can help you. If you want to know more Users please continue reading below! First open the phone settings and enter the settings page. Scroll down the page and find Google. Go to the Google page and click on Google Account. Enter the account page and click View under the verification code. Enter your password or use your fingerprint to verify your identity. Obtain a Google security code and use the security code to verify your Google identity.

How to get the last element of LinkedHashSet in Java? How to get the last element of LinkedHashSet in Java? Aug 27, 2023 pm 08:45 PM

Retrieving the last element from a LinkedHashSet in Java means retrieving the last element in its collection. Although Java has no built-in method to help retrieve the last item in LinkedHashSets, there are several effective techniques that provide flexibility and convenience to efficiently retrieve this last element without breaking the insertion order - a must for Java developers issues effectively addressed in its application. By effectively applying these strategies in their software projects, they can achieve the best solution for this requirement LinkedHashSetLinkedHashSet is an efficient data structure in Java that combines HashSet and

Get the latest updates now: Fix missing latest updates Get the latest updates now: Fix missing latest updates Nov 08, 2023 pm 02:25 PM

If the "Get the latest updates as soon as they become available" option is missing or grayed out, you may be running a Developer Channel Windows 11 build, and this is normal. For others, issues arise after installing the KB5026446 (22621.1778) update. Here's what you can do to get back the "Get the latest updates as soon as they become available" option. How do I get the "Get the latest updates as soon as they're available" option back? Before starting any of the solutions below, make sure to check for the latest Windows 11 updates and install them. 1. Use ViVeTool to go to the Microsoft Update Catalog page and look for the KB5026446 update. Download and reinstall the update on your PC

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to install dual SIM on Realme 12 Pro? How to install dual SIM on Realme 12 Pro? Mar 18, 2024 pm 02:10 PM

Although the general operations of domestic mobile phones are very similar, there are still some differences in some details. For example, different mobile phone models and manufacturers may have different dual-SIM installation methods. Erzhenwo 12Pro, a new mobile phone, also supports dual-SIM dual standby, but how should dual-SIM be installed on this phone? How to install dual SIM on Realme 12Pro? Remember to turn off your phone before installation. Step 1: Find the SIM card tray: Find the SIM card tray of the phone. Usually, in the Realme 12 Pro, the SIM card tray is located on the side or top of the phone. Step 2: Insert the first SIM card. Use a dedicated SIM card pin or a small object to insert it into the slot in the SIM card tray. Then, carefully insert the first SIM card.

C++ program to get the imaginary part of a given complex number C++ program to get the imaginary part of a given complex number Sep 06, 2023 pm 06:05 PM

Modern science relies heavily on the concept of plural numbers, which was first established in the early 17th century by Girolamo Cardano, who introduced it in the 16th century. The formula for complex numbers is a+ib, where a holds the html code and b is a real number. A complex number is said to have two parts: the real part <a> and the imaginary part (<ib>). The value of i or iota is √-1. The plural class in C++ is a class used to represent complex numbers. The complex class in C++ can represent and control several complex number operations. Let's take a look at how to represent and control the display of plural numbers. imag() member function As mentioned above, complex numbers are composed of real part and imaginary part. To display the real part we use real()

See all articles