• Post Reply Bookmark Topic Watch Topic
  • New Topic

How about passing HttpSession to a standalone class ?  RSS feed

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I need to process lot of business logic separately, instead of doing it in servlet, I want to create a standalone java class handles that. In some of its method, it takes argument of "HttpSession" which has some objects stored in it. for eaxmple

public class MyProcess {

public void processMenu(HttpSession session) {
....
}

}

Is this a common and safe way to do things ?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that will work, it should be approached with caution. By passing the session to a class you tie that class to the servlet environment and limit its potential for reuse. It can be a violation of the principle of 'separation of concerns'. Now, if the class is most definitely useless outside of the servlet environment, or is intedned to work directly on the container classes, its a bit less of an issue or a non-issue.

Personally, I rarely pass such container classes to other classes merely to carry data along. Rather, I pass just the info from the session (or request, or whatever) that the 'helper' class needs to perform its duty.
 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Personally, I rarely pass such container classes to other classes merely to carry data along. Rather, I pass just the info from the session (or request, or whatever) that the 'helper' class needs to perform its duty.


do you mean you first grab out the "data" that is stored in the session, and then pass these data directly to the helper class ??
 
Jayant Raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bear. Only pass the data that is required by the Helper class to do its job. HttpSession is a presentation layer object and it should not get passed into the Business Logic layer.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mean you first grab out the "data" that is stored in the session, and then pass these data directly to the helper class ??


Yes. If there is no reason for the helper class to need the session (or request, or whatever) it's a poor practice to pass it just to serve as a "data carrier".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!