Access Request and Response
In this section, you will learn to develop an application that accesses the request and response object in struts 2 application. To access the request object , use the ActionContext or implement the ServletRequestAware interface and to access the response object, use the ActionContext or implement the ServletResponseAware interface.
Here, in this application, we are going to implement the ServletRequestAware and ServletResponseAware interfaces in our action class.
Accessing the request and response object, you need a struts.xml file.
|<action name="AccessRequest" class="net.roseindia.AccessRequest">
Now, create a simple action "AccessRequest.java "
Here we use the setServletRequest() (for setting the request object) and getServletRequest() (for getting the request object) methods of ServletRequestAware interface. Similarly, we use the setServletResponse() (for setting the response) and getServletResponse() (for getting the response) methods of ServletResponseAware interace.
Finally, create a jsp page for accessing the request and response object.
Open the web-browser and type the "http://localhost:8080/struts2tutorial/roseindia/AccessRequest.action" in the address bar and press the "enter key" it displays the access request and response object as well as accessed date and time.
Output of this application: