• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How To Pass Data From A Servlet To A Helper Class

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a typical servlet that obtains information from users:

How do I pass username and password from this servlet to its helper java class UserDirectory.java?
I know how to pass data to a method in the helper java class (i.e. UserDirectory.aMethod(username, password)). But, I need the information available in the UserDirectory.java.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get your question
Can't you just use a normal java class with setter/getter methods. Create an instance in the servlet and set their values?
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't fullly understand your question but you can use setters/getters to this or simple create a helper class object with a constructor such as HelperClass hc = new HelperClass(username,password); etc.. Hope this helps.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a handy "getParameterMap" method in javax.servlet.ServletRequest that bundles all of the request parameters into a Map collection.
That lets you pass all of the request parameters to your helper class in a single object -really only useful if you have lots of parameters because you don't have to write a lot of setter methods.
Bill
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think maybe your question has to do with the .java file itself, and not an instance of a class.
But the big question is why would you want to do this. You do not want to hardcode values into any class/program/application/procedure/function/method. Anywhere.
You can always make a Helper class that writes to a file, and write all the code and the Parameter information into that file and name if "UserDirectory.java"
But again, I would question why. You obviously have a certain goal, and maybe if you post that goal, we can help better by giving you a better design.
Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic