Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Pass Data From A Servlet To A Helper Class  RSS feed

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • 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.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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?
 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!