Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Struts Struts2 Strutsresources Access Request and Response

Related Tutorials


Share on Google+Share on Google+

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.

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 " "

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.

package net.roseindia;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public class AccessRequest extends ActionSupport implements 
  private HttpServletRequest request;
  private HttpServletResponse response;
  public String execute() throws Exception{
  return SUCCESS;
  public void setServletRequest(HttpServletRequest request){
  this.request = request;

  public HttpServletRequest getServletRequest(){
  return request;

  public void setServletResponse(HttpServletResponse response){
  this.response = response;

  public HttpServletResponse getServletResponse(){
  return response;

Finally, create a jsp page for accessing the request and response object. 


<%taglib prefix="s" uri="/struts-tags" %>
<%@page language="java" import="java.util.*" %>

  <title>Access Request and Response Example! </title>
  <h1><span style="background-color: #FFFFcc">Access Request 
  and Response Example!</span></h1>

  <b>Request: </b><%=request%><br>
  <b>Response: </b><%=response%><br>
  <b>Date: </b><%=new Date()%>

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:



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 12, 2007

Related Tutorials

Discuss: Access Request and Response   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image