• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting session bean values from java classes.  RSS feed

 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web app that I need to keep some persistent data for through a search. What I do now is I pass my data from bean to bean from request to request since I have 2 servlets. Now what I want to do is make the data go into a session bean and replace the code that sets the different beans with code that will just set the session bean value at that point. Here is my questions...

In the servlet I can use the HttpSession interface. This is found in javax.servlet.http. Is it ok for me to use this in my supporting classes to set the session bean values? Or should I only use this when I'm extending the servlet? I would like to just use it in my classes and be done with it, I just want to see if anyone has done this first.

Thanks
Bryce
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bryce Martin:
...

In the servlet I can use the HttpSession interface.[?]


I'm not sure what you mean by this.
You don't want to extend or implement HttpSession yourself.
The container will take care of that for you.

You can get a reference to the session created by the container with request.getSession. Once you have a reference to it, you can bind objects to it.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I can't use request.getSession inside of my helper methods that are in my external classes. Does this mean that I need to return all my values to the servlet and set my session values there? Or is there a way to do it in the methods?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bryce Martin:
...all my values ...


What values?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this is going to work, but I had the idea to pass the (HttpServletRequest request) from my servlet to my helper methods in my external classes. Then I would be able to see that request even when I'm outside the servlet processing. I'm going to try this since I can't think of a better way right now. It would be really interesting if this actually works.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule of thumb, you want to be careful about keeping references to container managed objects around. If your 'external' objects are going to last longer than one request/response cycle, then I'd say it's a bad idea to store references to the request object in them.

Many containers (like Tomcat) pool request objects and recycle them for use in later requests. Keeping references to them in your own objects can sometimes lead to quirky and hard to track down bugs.

What values are you talking about; form parameters, request attributes?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well here is the basic outline of what I do.
I have a servlet that calls a method. In that method I do all my processing needed to build a bean. I return that bean to the servlet, set it as a request.setAttribute("mybean",mybean); so that I can process it in the jsp using jstl after I forward from the servlet.

But I have to do this two times. This is a drill down style program that has some basic order information that doesn't change. Once I go to my search results page the user clicks on a specific order they want. I need to store this information in a session bean so that I know all the time what order the user is looking at since they can drill down to the line details. When they go to line details I display the basic order info as a header. So instead of passing info from one bean instance to another across requests I want to just have a session bean that I can change....

I would like to just put the values in the session bean using my methods instead of returning the bean to the servlet and doing it there. It would be less coding and the servlet could just stick to directing traffic instead of doing bean setting. Is this the right way to think about this?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are form parameters and then resultset values based on those form parameters. I have a list of different order attributes that I need to keep from page to page. Which is servlet to servlet basically. They need to be session scope and not request scope.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After doing some research I think that instead of creating a session bean I could just set a session attribute for each piece of information that I need. Is this a good idea or am I just moving in a backwards direction?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong with creating a bean and binding it to session scope.
You can also bind individual attributes to session.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of reprogramming a lot of my methods, I just set some session variables. Its working very nice now. Thanks for all the help.

Bryce
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!