aspose file tools*
The moose likes Servlets and the fly likes Passing request variables from Servlet to utility class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing request variables from Servlet to utility class" Watch "Passing request variables from Servlet to utility class" New topic
Author

Passing request variables from Servlet to utility class

Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
Hi,
A request reaches my servlet, with some variables set in it by the user via an html form.
I check what the "action" variable says, and forward to the appropriate JSP page. In one case, I want to do some processing with a utility class before forwarding to the JSP page.
I call the class like this -

NewAccountProcessor nap = new CSAccountProcessor();
boolean success = nap.processRequest(req);

However, when I try to read the form variables in req from the CSAccountProcessor() class, I get a null exception.
Is this expected behaviour? Do I need to read the req variables in the servlet and then explicitly pass them as parameters in my function call, instead of passing the whole request object? Or am I just doing something silly somewhere...

thanks.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Could you post some of your code and the exception trace? At first glance, i would assume that you shouldn't have any problems passing the reqeust object as a parameter...


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
Mikey Kelly
Well the request object should get passed to the utility clas where you can get the parameters, but good design would be to create a bean in the servlets, set the values in the bean and then pass the bean to the utility class. Passing request object to utility class scatters your code which can be tough for debugging. Handling all the request and response object at one place is always a better option.

Let me know what you think
More inputs are always welcomed

Regards
Makarand Parab
Javed Mohammed
Greenhorn

Joined: Sep 29, 2005
Posts: 17
I agree with Makarand Parab. Having the utility calss to access request object also defies the MVC pattern. In case if we want to use a different client other than a servlet, then there is a problem in the utility calss.
Mikey Kelly
Greenhorn

Joined: Aug 09, 2005
Posts: 18
Thanks folks,
I'll take a look at the form end of things. I must be going wrong there. Thanks for the bean suggestions too. I'll give that a try.
Mike
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
Personally I like to pass the Map from the request method getParameterMap()
this maps the parameter name to a String[] of values. This causes a little extra work in the utility side but there is a really good reason to do it.
The reason is you can now test your utility code OUTSIDE the servlet environment - so much easier, especially with complex utilities.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing request variables from Servlet to utility class