Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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
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

Passing request variables from Servlet to utility class

Mikey Kelly

Joined: Aug 09, 2005
Posts: 18
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...

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

Makarand Parab
Javed Mohammed

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

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.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.
I agree. Here's the link:
subject: Passing request variables from Servlet to utility class
It's not a secret anymore!