Reputation: 1677
How to get the information which is stored in a session of servlet from a normal java class..
Upvotes: 2
Views: 3914
Reputation: 13455
HttpSession objHttpSession=request.getSession().getAttribute(@argument@);
Upvotes: 0
Reputation: 691635
As with any other class: by passing it as an argument of a constructor or method:
MyJavaClass c1 = new MyJavaClass(session.getAttribute("foo"));
MyOtherJavaClass c2 = new MyOtherJavaClass();
c2.doSomethingWithSession(session);
Upvotes: 2
Reputation: 1499770
Generally I would make information flow from the servlet to the class: the servlet initiates the actions it requires, passing along any information that's needed to perform those actions.
I would try to isolate the other classes from any knowledge of servlets and sessions as far as possible - the servlet should know about its collaborators, rather than the other way round.
(If this doesn't help for your specific situation, please tell us more about what you're trying to do.)
Upvotes: 1