Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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. 

struts.xml

<action name="AccessRequest" class="net.roseindia.AccessRequest">
     <result>/pages/staticparameter/AccessRequest.jsp</result>
</action>

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.

AccessRequest.java

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 
                 
ServletRequestAware,ServletResponseAware{
  
  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. 

AccessRequest.jsp

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

<html>
  <head>
    <title>Access Request and Response Example! </title>
  </head>
  <body>
  <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()%>
  </body>
</html>

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:

                         

» View all related tutorials
Related Tags: c class text interface application object io servlet interfaces request context int this response action app war tex to ext

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Thanks, it was exactly what I was looking for.

Posted by Marty Diamond on Friday, 12.28.07 @ 04:47am | #43997

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.