This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Good practice to send request and response objects in methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Good practice to send request and response objects in methods" Watch "Good practice to send request and response objects in methods" New topic
Author

Good practice to send request and response objects in methods

Dev Anand
Ranch Hand

Joined: Aug 30, 2009
Posts: 41
I got the following code in which i send request n response object to methods.

Is it a good practice to send these objects or shall i write the code here itself.

Thanks in advance






/*--------------------------Code Start------------------------------*/

if (s.equals("UserGroups"))
{
UserGroups(request,response);
}
else if(s.equals("updategroup"))
{
updateGroup(request,response);
}
else if (s.equals("DeleteGroups"))
{
DeleteGroups(request,response);
}
else if(s.equals("UserContacts"))
{
userContact(request,response);
}

/*---------------------------------Code End------------------------------*/


Regards


Dev Anand. P











William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Is it a good practice to send these objects or shall i write the code here itself.


Within your servlet class it is ok, but it is a questionable practice to pass request and response references to other classes because these are container managed objects. Sloppy programming can leave your code hanging on to a request or response longer than it should.

Instead of passing request to another class, use the javax.servlet.ServletRequest getParameterMap method and pass the resulting Map. By working with a Map you will have code that can be tested outside the servlet environment.

Bill
Dev Anand
Ranch Hand

Joined: Aug 30, 2009
Posts: 41
Thank you Bill.

Regards
Dev Anand. P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good practice to send request and response objects in methods
 
Similar Threads
passing an ArrayList from a servlet to JSP
Why is this code not working ?
Design pattern needed
Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?
How to use Ajax in JSF