File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes In Struts2, how to get HttpServletRequest and HttpServletReponse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "In Struts2, how to get HttpServletRequest and HttpServletReponse" Watch "In Struts2, how to get HttpServletRequest and HttpServletReponse" New topic
Author

In Struts2, how to get HttpServletRequest and HttpServletReponse

Ying Lu
Greenhorn

Joined: Feb 05, 2008
Posts: 4
public class ProcessTestAction extends ActionSupport
{
public String execute() throws Exception
{
return success;
}
}

In the above codes, how to get request, and response? Any clues?




Tried this, but does NOT work:
=================================
public class ProcessTestAction extends ActionSupport
{
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request)
{
this.request = request;
}

public String execute() throws Exception
{
int len = request.getContentLength(); /* request is NULL ! */
return success;
}
}

Thanks a lot!


ly
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Your class needs to implement the ServletRequestAware interface.

It is relatively rare to need to HttpServletRequest within in action, so you should ask yourself first, "Do I really need this?"
Ying Lu
Greenhorn

Joined: Feb 05, 2008
Posts: 4
Ok, I figured one way to get request and response


HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
Dejan Mratinkovic
Ranch Hand

Joined: Nov 20, 2008
Posts: 65
Proper way to do so would be by implementing:

ServletRequestAware, ServletResponseAware
Arpit Gadle
Ranch Hand

Joined: Dec 16, 2008
Posts: 69

Hi Eric,
Including Servlet related object will make it difficult for testing outside servlet container.
But what should one do if a project requires servlet related object to be used.
What is your opinion?

Regards,
Arpit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In Struts2, how to get HttpServletRequest and HttpServletReponse