Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In Struts2, how to get HttpServletRequest and HttpServletReponse

 
Ying Lu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I figured one way to get request and response


HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
 
Dejan Mratinkovic
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proper way to do so would be by implementing:

ServletRequestAware, ServletResponseAware
 
Arpit Gadle
Ranch Hand
Posts: 69
Linux MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic