GeeCON Prague 2014*
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


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: 12792
    
    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
 
GeeCON Prague 2014
 
subject: Good practice to send request and response objects in methods