| 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
|
 |
 |
|
|
subject: In Struts2, how to get HttpServletRequest and HttpServletReponse
|
|
|