aspose file tools*
The moose likes Struts and the fly likes How can Action class will get access to HttpServletRequest  to access data?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How can Action class will get access to HttpServletRequest  to access data??" Watch "How can Action class will get access to HttpServletRequest  to access data??" New topic
Author

How can Action class will get access to HttpServletRequest to access data??

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528


Please help me understand this .I am completely new to struts2 version .

I am referring to tutorials but couldn't get this concept. Posted so that i can get help here .
How can Action class will get access to HttpServletRequest to access data??

class NewAction {
public void String execute() throws Exception {
if ( its-ok() ) {
return "login";
}
else {
return "none";
}}}



If you want something you never had do something which you had never done
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Implement RequestAware interface to get access to the request attributes.
To get the actual request object, you can implement ServletRequestAware.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In general Struts 2 abstracts the request away; the framework populates action properties from request parameters. Assuming the form:the action would just define a "name" property--no need to access the request:You can access request parameters through a map by implementing ParameterAware if really necessary, but it's usually not.

You can access the HttpServletRequest itself by either implementing ServletRequestAware or using the ServletActionContext static utils. It's relatively rare to need to do either of these, and for obvious reasons it's not recommended.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks sonny and David for your replies.

I doesn't want to extend the ActionSuppourt class to get access to the data .I want to go with the ServletRequestAware interface only.

so for this can i do directly like this ??



A single class is sufficient or do i need to use anyother class ??

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You can, but again, it's a bad idea.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
David Newton wrote:You can, but again, it's a bad idea.


Then please tell me whats the correct approach ?? I could not understand them .

Do i need to write a separate class again for implementing this Aware interface ??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The code I provided above is the canonical (normal, usual) way to get at form/request data in Struts 2.

You don't *have* to extend ActionSupport, but you'd have to convince me there's a good reason not to--it provides the validation and localization support needed by nearly any web application.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

As Dave is saying, things are done differently in Struts 2, and for good reasons.

I suggest that you do some more reading on Struts 2 basics.
A good start will be the tutorial and other resources at http://www.struts2.net/.
There is also a free book available at InfoQ - Starting Struts 2.
 
Don't get me started about those stupid light bulbs.
 
subject: How can Action class will get access to HttpServletRequest to access data??